2

My Datagram simple client server program is giving error.Client takes information from server and server also gets information from client.

Here is my Server:

package udp;

import java.net.*;

public class DatagramServer implements Runnable {

DatagramPacket pack;
DatagramSocket sock;

DatagramServer() {
    new Thread(this).start();
}

public void run() {
    try {
        while (true) {
            recieve();
            send();
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

public void send() throws Exception {
    byte data[] = "This is a \n datagram packet".getBytes();
    InetAddress add = InetAddress.getByName("localhost");
    pack = new DatagramPacket(data, data.length, add, 8000);
    sock = new DatagramSocket();
    sock.send(pack);
    sock.close();
}

public void recieve() throws Exception {

    byte[] buffer = new byte[65536];
    DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
    sock = new DatagramSocket(8000);
    sock.receive(incoming);
    byte[] data = incoming.getData();
    String s = new String(data, 0, incoming.getLength());
}

public static void main(String[] args) {
    new DatagramServer();
}
}

And here is my Client :

package udp;

import java.net.*;

public class DatagramClient {

DatagramPacket pack;
DatagramSocket sock;

DatagramClient() {
    Thread t1 = new Thread() {
        public void run() {
            while (true) {
                try {
                    receive();
                    Thread.sleep(5000);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    };
    t1.start();
    Thread t2 = new Thread() {
        public void run() {
            while (true) {
                try {
                    send();
                    Thread.sleep(5000);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
    };
    t2.start();
}

public void receive() throws Exception {
    byte data[] = new byte[1000];
    pack = new DatagramPacket(data, data.length);
    sock = new DatagramSocket(8000);
    sock.receive(pack);
    System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
    System.out.println("Port::" + pack.getPort());
    sock.close();
}

public void send() throws Exception {

    String s = "KOLI SDF DFEF XFFFSS";
    byte[] b = new byte[1000];
    b = s.getBytes();
    DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
    sock= new DatagramSocket(8000);
    sock.send(dp);
}

public static void main(String[] args) {
    new DatagramClient();
}
}

I'm getting errors like:

java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bind
java.net.BindException: Address already in use: Cannot bindBUILD STOPPED  (total time: 4 seconds)

What should i do?

2 Answers 2

1

Your client and server has some issues such as open same port 8000 in different threads so I have fixed those within the following code.

Client

package udp;

import java.net.*;

public class DatagramClient {

    DatagramPacket pack;
    DatagramSocket sock;

    DatagramClient() {
        Thread t1 = new Thread() {
            public void run() {
                while (true) {
                    try {
                        receive();
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            }
        };
        t1.start();
    }

    public void receive() throws Exception {
        byte data[] = new byte[1000];
        pack = new DatagramPacket(data, data.length);
        sock = new DatagramSocket(8000);
        sock.receive(pack);
        System.out.println("Data::" + new String(pack.getData(), "UTF-8"));
        System.out.println("Port::" + pack.getPort());
        sock.close();
    }

    public void send() throws Exception {

        String s = "KOLI SDF DFEF XFFFSS";
        byte[] b = new byte[1000];
        b = s.getBytes();
        DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress.getByName("localhost"), 8000);
        sock= new DatagramSocket(8000);
        sock.send(dp);
    }

    public static void main(String[] args) {
        new DatagramClient();
    }
}

Server

package udp;

import java.net.*;

public class DatagramServer implements Runnable {

    DatagramPacket pack;
    DatagramSocket sock;

    DatagramServer() {
        new Thread(this).start();
    }

    public void run() {
        try {
            while (true) {
                //recieve();
                send();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void send() throws Exception {
        byte data[] = "This is a \n datagram packet".getBytes();
        InetAddress add = InetAddress.getByName("localhost");
        pack = new DatagramPacket(data, data.length, add, 8000);
        sock = new DatagramSocket();
        sock.send(pack);
        sock.close();
    }

    public void recieve() throws Exception {

        byte[] buffer = new byte[65536];
        DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
        sock = new DatagramSocket(8000);
        sock.receive(incoming);
        byte[] data = incoming.getData();
        String s = new String(data, 0, incoming.getLength());
    }

    public static void main(String[] args) {
        new DatagramServer();
    }
}
5
  • Can you please give an example in your answer? i'm totally new here and where i should make change? server or client?
    – IAmBlake
    Commented Nov 18, 2016 at 12:41
  • but server doesn't receive anything
    – IAmBlake
    Commented Nov 18, 2016 at 13:13
  • Check the client console it receives from server. Server only producing udp packets according to given code. Commented Nov 18, 2016 at 13:14
  • yeah client receives, but server doesn't..i also need that
    – IAmBlake
    Commented Nov 18, 2016 at 13:16
  • Can you please show me in your solution? thanks in advance
    – IAmBlake
    Commented Nov 18, 2016 at 13:27
0

The problem is your server code is trying to bind to the 8000 port each time receive () called. And receive is being called repeatedly from within the while(true) loop. So it tries to bind to the same port again and again.

To fix it remove the UDP socket instantiation i.e

sock = new DatagramSocket(8000);

From the receive method and put it before your while loop begins.

3
  • how can my Server both receive and send data to Client?
    – IAmBlake
    Commented Nov 18, 2016 at 13:34
  • Not sure what you are asking, your server code already does that. Commented Nov 18, 2016 at 13:39
  • In your server send method remove this - sock = new DatagramSocket();. Your server should use the same socket to respond as the one it received. On the client side your socket should connect the server's address and port, send data and wait until it received a response from the server. Commented Nov 18, 2016 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.