Skip to content
Snippets Groups Projects
Commit a1a231d1 authored by Dimitri Tayo Fongang Wembe's avatar Dimitri Tayo Fongang Wembe
Browse files

init UDP files

parent bc593ef6
No related branches found
No related tags found
No related merge requests found
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
class UDPClient {
public static void main(String args[]) throws Exception {
InetAddress localhost = InetAddress.getLocalHost();
String hostname = localhost.getHostName();
/* BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in)); */
DatagramSocket clientSocket = new DatagramSocket();
InetAddress ipAddress = InetAddress.getByName(hostname);
//byte[] sendData = new byte[1024];
//byte[] receiveData = new byte[1024];
byte[] data;
DatagramPacket sendPacket;
//String sentence = inFromUser.readLine();
//sendData = sentence.getBytes("UTF-8");
for(int i = 0; i <= 1000000; i++) {
data = ByteBuffer.allocate(4).putInt(i).array();
sendPacket = new DatagramPacket(data,
data.length, ipAddress, 2345);
clientSocket.send(sendPacket);
}
/* DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, ipAddress, 2345); */
//clientSocket.send(sendPacket);
/* DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length); */
/* clientSocket.receive(receivePacket);
int rcvLen = receivePacket.getLength();
String modifiedSentence =
new String(receivePacket.getData(), 0, rcvLen, "UTF-8");
System.out.println("FROM SERVER: " + modifiedSentence); */
clientSocket.close();
}
}
\ No newline at end of file
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
class UDPServer {
public static void main(String args[]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(2345);
//byte[] receiveData = new byte[1024];
//byte[] sendData = new byte[1024];
byte[] data = new byte[4];
int i = 0;
while(true) {
DatagramPacket receivePacket =
new DatagramPacket(data, data.length);
serverSocket.receive(receivePacket);
int rcvLen = receivePacket.getLength();
int inData = ByteBuffer.wrap(receivePacket.getData(), 0, rcvLen).getInt();
/* if(inData != i) {
System.out.println("Error");
} */
// Gilt nur zum Testen ob der Server tatsächlich die Zahlen erhält
// Also hier print nur die ersten 99 Zahlen :-)
/* if(i < 100)
System.out.println(inData); */
if(i != inData)
System.out.println(inData);
/* String sentence = new String(receivePacket.getData(), 0, rcvLen,
"UTF-8");
InetAddress ipAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes("UTF-8");
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, ipAddress, port);
serverSocket.send(sendPacket); */
i++;
if(i == 1000000)
System.out.println("end");
if(i > 1000000) {
i = 0;
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment