Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Socket Client returning Connection refused

 
John Lark
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
War B is HTTPS, and we don't want to install the cert on the server.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I'm so happy! And I wish to make this tiny ad happy too:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!