| Author |
Socket Client returning Connection refused
|
John Lark
Ranch Hand
Joined: Feb 08, 2010
Posts: 63
|
|
I Keep getting a connection refused... its all on localhost I cant figure out what I am doing wrong!? When Client tries to connect it looks like srvr.accept() is getting called, and its performing some desired functionality unable to read the output stream however...
My Client:
//Create Socket Connection to Server
Socket skt = new Socket("localhost", 855);
//Retrieve Input Stream from Socket
InputStreamReader in = new InputStreamReader(skt.getInputStream());
//Setup Buffer Reader and wait for Input from Server
BufferedReader br = new BufferedReader(in);
//Create Strings for Buffer Input
String next = "";
String response = "";
//read first line of respone from Server
response = br.readLine();
//Use Loop to read through entire buffer and not just the first line
while((next = br.readLine()) != null){
response += next;
}
//Close Buffer
br.close();
in.close();
skt.close();
My Server:
srvr = new ServerSocket(855);
while(true){
try{
//Listen for Client Connections
skt = srvr.accept();
}
catch(Exception e){}
}
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
I would strongly suggest you don't ignore exceptions. Especially in network programming, where exceptions are normal occurrences to be handled appropriately. So this code:
just won't do. At least replace it by something which tells you what happened and where:
|
 |
John Lark
Ranch Hand
Joined: Feb 08, 2010
Posts: 63
|
|
Fair enough! I found my problem I ran a netstat -a and realized that the port that my Socket Server was supposed to be listening on wasn't there. I had miss-understood when the Socket Server was getting created, so my Client was trying to talk to it too soon!
Thanks,
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
In real life the client and server would be different programs running on different machines. It's natural that you would test on localhost, but do you not have separate programs for the client and the server?
|
 |
John Lark
Ranch Hand
Joined: Feb 08, 2010
Posts: 63
|
|
|
I am running kind of a weird rig. I have two different pieces to my application on the same machine running in different war files, I needed War A to tell War B to reset his database connection. So in this situation it will all run on one machine.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
So why doesn't War A just send an HTTP request to War B to do that? Running a socket server inside a web application strikes me as an undesirable thing to do.
|
 |
John Lark
Ranch Hand
Joined: Feb 08, 2010
Posts: 63
|
|
|
War B is HTTPS, and we don't want to install the cert on the server.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 13842
|
|
|
Seems like a pretty heavy-duty workaround for that. But anyway... War B should be starting your server when it starts up and terminating it when it shuts down. Preferably using a ServletContextListener. Although it sounds like you're still in the early stages of testing and haven't got so far as to implement it in a WAR yet.
|
 |
 |
|
|
subject: Socket Client returning Connection refused
|
|
|