• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Socket Client returning Connection refused

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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){}
}

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
War B is HTTPS, and we don't want to install the cert on the server.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic