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); } } } 
2
  • all right,so what should i do to open multiple ports of the same server? Commented Feb 22, 2014 at 12:00
  • Forget what I said. Your loop tries to open a server socket on the same port several times. See zahorak's answer. Commented Feb 22, 2014 at 12:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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"
I have clarified my answer
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 ?
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? for more information about the difference.
so it means that i have to create one server socket and multiple sockets in my server code?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.