2

I have opened a server socket port in a new thread and connected the client to that port.i wish to close the client socket after it has completed its work.(and i know there is some problem in my while loop of the run() method but i am unable to understand it. when i run my server application to open a port and listen to a connection it gives an error saying java.net.BindException: Address already in use: JVM_Bind

please tell me what i am doing wrong here in detail and give a theoratical solution for it in detail . It would be really appriciated.please forgive me if its a dumb question

Jshirwani

server

import java.io.*;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.*;

public class TryThreads extends Thread 
{
    private int Portnumber;
    private static String inputLine;

    public TryThreads(int portNumber)

    {

        Portnumber = portNumber; 

        setDaemon(false);

    }

    public static void main(String[] args)

    {

        //create three threads

        Thread first = new TryThreads(63400);

        Thread second = new TryThreads(63401);


        first.start();

        second.start();

        //third.start();

        System.out.println("ending main");

        return;


    }

    public void run()

    {
        //System.out.println("one socket port opened");

        try

        {

            System.out.println("one socket port opened");

            System.out.println("one socket port opened");

            while (true)

            {

                ServerSocket serverSocket = new ServerSocket(Portnumber);

                System.out.println("ending main2");



            //System.out.println("one socket port opened");

            Socket clientSocket = serverSocket.accept();

            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            while((inputLine = bufferedReader.readLine()) != null)

                 System.out.println(inputLine);

            }




        }

        catch(IOException e)

        {

            System.out.println(e);

        }

    }

}

client

import java.io.*;
import java.net.Socket;
public class client 
{

    private static PrintWriter printWriter;
    public static void main(String[] args) 
    {
        BufferedReader in = null;

        try
        {
            Socket socket = new Socket("localhost",63400);
            printWriter = new PrintWriter(socket.getOutputStream(),true);
            printWriter.println("Hello Socket");
            printWriter.println("EYYYYYAAAAAAAA!!!!");

            socket.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
morgano
  • 17,210
  • 10
  • 45
  • 56
user3085866
  • 125
  • 2
  • 5
  • 17

1 Answers1

2

You need to change the loop so that you create the server socket outside the loop. You can open only one socket per port. After you have it opened you can accept as many connections on that socket as you want.

ServerSocket serverSocket = new ServerSocket(Portnumber);
while (true) {
    System.out.println("ending main2");

    //System.out.println("one socket port opened");

    Socket clientSocket = serverSocket.
        BufferedReader bufferedReader = new BufferedReader(new 
            InputStreamReader(clientSocket.getInputStream()));

    while((inputLine = bufferedReader.readLine()) != null)
        System.out.println(inputLine);

}

Update with clarifications:

The exception you describe means that there is already a connection opened on that port, it can be from your application (you are opening a socket with the same port in a loop) or it can be from a different application which is just listening on a port (any service running, bittorrent, skype, ...)

java.net.BindException - description:

Signals that an error occurred while attempting to bind a socket to a local address and port. Typically, the port is in use, or the requested local address could not be assigned.

Other reasons for that exception could include that you don't have high enough permissions, however this is unlikely since you use such a high port number.

If you can'T guarantee, that the port you want to use will be open, than you should have a loop and as long as you'll keep getting the java.net.BindException, you should keep trying the following ports (port + 1). This way you will eventually find an empty one which you are allowed to use. You can also not specify any port than a random available port will be chosen for you.

peter
  • 14,348
  • 9
  • 62
  • 96
  • after i open server socket outside the loop and inside the try block it still gives the same error. secondly i do not understand what you mean by "you can open only one socket per port" – user3085866 Feb 22 '14 at 12:09
  • I have clarified my answer – peter Feb 22 '14 at 12:42
  • if the port that had a client connected to it is still open ,shouldnt it be still open to accept new clients (or the same client again) after the client has been closed ? – user3085866 Feb 22 '14 at 12:49
  • You're mixing the `ServerSocket` (opens a port, starts using this port, keeps this port, is listening to this port, accepts incoming client connections on this port, ...) with the `Socket` (handles a specific connection between the Server and some client). Take a look at [What is the difference between Socket and ServerSocket?](http://stackoverflow.com/questions/2004531/what-is-the-difference-between-socket-and-serversocket) for more information about the difference. – peter Feb 22 '14 at 12:55
  • so it means that i have to create one server socket and multiple sockets in my server code? – user3085866 Feb 22 '14 at 12:57
  • here is what i understood correct me if i am wrong , a server socket has a port open for a socket of the server side ,the client socket connects to the socket of the server(the socket that was connected to a port of the server) ,basically a server listens for a client via its own socket that is connected to its port? am i right? – user3085866 Feb 22 '14 at 13:05
  • Yes, but you are creating multiple socket instances with `Socket clientSocket = serverSocket.accept();` - I just noticed that you should also close your `BufferedReader` – peter Feb 22 '14 at 13:31
  • where should i put Socket clientSocket = serverSocket.accept(); in my code then? and how to close BufferedReader and why? – user3085866 Feb 22 '14 at 13:36