0

I am trying my hands on socket programming in Java. To get things roll, I tried a very simple server and client code snippet as below

public class Server {

    //initialize socket and input stream
    private Socket socket = null;
    private ServerSocket server = null;
    private DataInputStream in = null;

    public Server(int port) {
        // starts server and waits for a connection
        while (true) {
            try{
                server = new ServerSocket(port);
                System.out.println("Server started");
                System.out.println("Waiting for a client ...");
                socket = server.accept();
                System.out.println("Client accepted");
                // takes input from the client socket
                in = new DataInputStream(
                        new BufferedInputStream(socket.getInputStream()));
                String line = "";
                // reads message from client until "Over" is sent
                while (!line.equals("Over"))
                {
                    try
                    {
                        line = in.readUTF();
                        System.out.println(line);
                    }
                    catch(IOException i)
                    {
                        System.out.println(i);
                    }
                }
                System.out.println("Closing connection");
                // close connection
                socket.shutdownInput();
                socket.shutdownOutput();
                socket.close();
                in.close();
            }
            catch(IOException i){
                System.out.println(i);
            }
        }
    }

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

Client.java looks like below

public class Client {

    // initialize socket and input output streams
    private Socket socket = null;
    private DataInputStream input = null;
    private DataOutputStream out = null;
    // constructor to put ip address and port
    public Client(String address, int port)
    {
        // establish a connection
        try
        {
            socket = new Socket(address, port);
            System.out.println("Connected");
            // takes input from terminal
            input = new DataInputStream(System.in);
            // sends output to the socket
            out = new DataOutputStream(socket.getOutputStream());
        }
        catch(UnknownHostException u)
        {
            System.out.println(u);
        }
        catch(IOException i)
        {
            System.out.println(i);
        }// string to read message from input
        String line = "";
        // keep reading until "Over" is input
        while (!line.equals("Over"))
        {
            try
            {
                line = input.readLine();
                out.writeUTF(line);
            }
            catch(IOException i)
            {
                System.out.println(i);
            }
        }
        // close the connection
        try
        {
            input.close();
            out.close();
            socket.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }
    public static void main(String args[]) {
        Client client = new Client("127.0.0.1", 5000);
    }
}

It works fine for the first connection with the client. As soon as the client sends Over server starts giving exception java.net.BindException: Address already in use.

Even if I am closing the socket, why the port is in use?

Kuldeep Yadav
  • 1,250
  • 2
  • 17
  • 29

2 Answers2

2

The following lines of code is trying to create ServerSocket again (in fact, infinite number of times) on the port, 5000 and that is why you are getting the error.

while (true) {
    try{
        server = new ServerSocket(port);

Do it as follows:

try{    
    server = new ServerSocket(port);    
}catch(IOException i){    
    System.out.println(i);
}   
while (true) {
    try{
Mateus Terra
  • 137
  • 8
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
1

Take Server out of while loop and some more code changes

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {


    //initialize socket and input stream
    private Socket socket = null;
    private ServerSocket server = null;
    private DataInputStream in = null;

    public Server(int port) throws IOException {
        // starts server and waits for a connection
        server = new ServerSocket(port);
        while (true) {
            try{

                System.out.println("Server started");
                System.out.println("Waiting for a client ...");
                socket = server.accept();
                System.out.println("Client accepted");
                // takes input from the client socket
                in = new DataInputStream(
                        new BufferedInputStream(socket.getInputStream()));
                String line = "";
                // reads message from client until "Over" is sent
                while (!line.equals("Over"))
                {
                    try
                    {
                        line = in.readUTF();
                        System.out.println(line);
                    }
                    catch(IOException i)
                    {
                        System.out.println(i);
                    }
                }
                System.out.println("Closing connection");
                // close connection
                socket.close();
                in.close();
            }
            catch(IOException i){
                System.out.println(i);
            }
        }
    }

    public static void main(String args[]) throws IOException {
        new Server(5000);
    }
}
Binu
  • 620
  • 5
  • 14