I want to write a code that the client send 10 integers to the server and then the server compute the average of these number after that the server sends the average back to the client I write the server code like this , must i use specific inet address and should i change the server port between the client and server?
import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class UDP_SERVER {
public static final int SERVICE_PORT = 7;
public static final int BUFSIZE = 4096;
byte[] bu = null;
private DatagramSocket socket;
public UDP_SERVER(){
try{
socket = new DatagramSocket( SERVICE_PORT );
}
catch (Exception e){
System.err.println ("Unable to bind port");
}
}
public void serviceClients(){
int sum = 0;
byte[] buffer = new byte[BUFSIZE];
for (;;){
try{
InetAddress addr = InetAddress.getLocalHost() ;
try{
DatagramPacket packet = new DatagramPacket(buffer, BUFSIZE);
socket.receive(packet);
ByteArrayInputStream bin = new ByteArrayInputStream(buffer);
DataInputStream d1 = new DataInputStream((bin));
sum = sum + d1.readInt();
}
catch (IOException ioe){
System.err.println ("Error : " + ioe);
}
double avg = sum / 10;
String a1 = Double.toString(avg);
bu = a1.getBytes();
DatagramPacket Dps = new DatagramPacket(bu, bu.length,addr, SERVICE_PORT);
socket.send(Dps);
}
catch (IOException ex){
System.err.println ("Error : " + ex);
}
}
}
public static void main(String args[]){
UDP_SERVER server = new UDP_SERVER();
server.serviceClients();
}
}
what should I do? and what inet address could I use?