-1

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?

N_K
  • 3
  • 2
  • Do you get any details with the error other than "can't bind port"? Could you copy and paste the full error message please – Joni Aug 09 '20 at 15:49
  • Unable to bind port Exception in thread "main" java.lang.NullPointerException at UDP_SERVER.serviceClients(UDP_SERVER.java:28) at UDP_SERVER.main(UDP_SERVER.java:54) Java Result: 1 – N_K Aug 09 '20 at 15:58
  • That's not the real error. Replace your `System.err.println("Unable to bind port")` with `e.printStackTrace()` to see the real error. Also, can you tell us which operating system you use, since the error could depend on that? – Joni Aug 09 '20 at 16:03
  • java.net.BindException: Address already in use: Cannot bind Exception in thread "main" java.lang.NullPointerException at UDP_SERVER.serviceClients(UDP_SERVER.java:28) at UDP_SERVER.main(UDP_SERVER.java:54) Java Result: 1 – N_K Aug 09 '20 at 16:07
  • I use windows 10 – N_K Aug 09 '20 at 16:07

1 Answers1

0

"BindException: Address already in use" means that another process is using the port you're trying to use. Could there be another copy of your program running, that you forgot to stop?

As a "quick fix", try using another port, for example:

public static final int SERVICE_PORT = 7777;

If the port 7 is not being used by your program, there are Windows tools with which you can find which program is using it - see How can you find out which process is listening on a port on Windows?

Joni
  • 101,441
  • 12
  • 123
  • 178