Reputation: 249
I'm trying to make something similar to a client server chat system, with mutliple clients
Server side code
public class Server{
private Socket socket=null;
private ServerSocket serversocket=null;
public Server(int port){
Values val = new Values();
while(true){
try{
serversocket=new ServerSocket(port);
System.out.println("Server started\nWaiting for clients ...");
socket=serversocket.accept();
System.out.println("Client accepted");
ProcessRequest pRequest= new ProcessRequest(socket, val);
Thread t = new Thread(pRequest);
t.start();
}
catch(Exception e){
System.out.println("Socket creation exception: "+e);
break;
}
}
Now when i run the server on any port to listen for connections, it is throwing exception
Server started
Waiting for clients ...
Client accepted
Socket creation exception: java.net.BindException: Address already in use (Bind failed)
But i'm able to send and receive message between the client and server without any problem.
It's showing the error but the thread starts correctly and processes the request as it should.
So, why is this exception occuring, and how to fix the same?
Class using the thread --
class ProcessRequest implements Runnable{
private DataInputStream inp=null;
private DataOutputStream oup=null;
private Socket socket=null;
private Values val=null;
private String ip;
ProcessRequest(Socket s, Values v){
socket=s;
val=v;
ip=(((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/","");
}
public void run(){
try{
inp=new DataInputStream(socket.getInputStream());
oup=new DataOutputStream(socket.getOutputStream());
}
catch(Exception e){
System.out.println(e);
}
String line = "";
while (!line.equalsIgnoreCase("exit")){
try{
line = inp.readUTF();
// System.out.println(line);
String[] tokens=line.split(" ");
if(tokens[0].equalsIgnoreCase("put")){
val.setValue(ip, tokens[1], tokens[2]);
}
else if(tokens[0].equalsIgnoreCase("get")){
String value=val.getValue(ip, tokens[1]);
oup.writeUTF(value);
}
}
catch(IOException i){
System.out.println(i);
return;
}
}
try{
inp.close();
oup.close();
socket.close();
}
catch(IOException i){
System.out.println(i);
return;
}
}
Upvotes: 2
Views: 882
Reputation: 19158
The ServerSocket needs only to be created for once (here in this case) and bound to the IP and Port.
What you're doing here is creating multiple ServerSockets and binding to the same IP and port. Creating different ServerSockets and binding to the same set of IP and Port combination will obviously throw this exception.
So, to make it work, please remove the ServerSocket creation line from the loop.
public Server(int port){
Values val = new Values();
// Add your ServerSocket code here instead of the loop
serversocket = new ServerSocket(port);
System.out.println("Server started\nWaiting for clients ...");
while(true) {
try {
socket=serversocket.accept();
System.out.println("Client accepted");
ProcessRequest pRequest = new ProcessRequest(socket, val);
Thread t = new Thread(pRequest);
// And so on...
}
Upvotes: 4