• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Connection Refused Error

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have implemented simple Client and Server using Java Socket API.

The client socket was created as:



The server socket was created as:


I got a "Connection Refused" Error in the client program. My machine running windows XP professional, serving as both client and server (I started the server first and then run the client program attempting to connect to the server). Can anyone help me?

Thanks in advance
[ April 02, 2006: Message edited by: Mellihoney Michael ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you post more of your code so we can get a bigger picture?

Another simple test is to telnet into your server socket and see if a connection can be established.

telnet localhost <port>

I cant see the rest of your code but if after your call client = socket.accept(); and dont do anything afterwards then your application will terminate after it exists the main method and close the port. I cant see what your code is doing so this is just one blind assumption.
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, Tal Nathan.

Here is the detail code.

Client side code:




I tried to startup the server and telnet the port, it is successful. But when I used the client side program to access the port, it is refused.

Could you please check the problem for me? Thanks.
 
Tal Nathan Tal Nathan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, you had two problems in your code.
Firstly every time you want to send data accross a socket you need to call the flush(); method;
Ie after every out.println(); call out.flush(); this ensures that all data in the buffer is sent accross.

Secondly, on your server code you keep on using system.out.println to display the status and you are printing out in.readLine();

this will cause the server to hang since every time you call in.readLine() it waits for the client to send data.

Below is the code with modifications, note I added out.flush and I used a variable String clientData = in.readLine() so that you only call in.readLine once.

I know you are doing this to test but when you get the hang of it you need to learn how to use sockets and threads together to allow multiple connections.

Ie, every time you call socket.accept() you should launch a new thread or call a thread from a thread pool to handle the request.
Hope this helps, good luck!.




import java.io.*;
import java.net.*;

public class server {

public static void main(String[] args) throws IOException {
listenSocket();
}

public static void listenSocket(){
ServerSocket server = null;
Socket client = null;
BufferedReader in = null;
PrintWriter out = null;
String line;

try{
server = new ServerSocket(20001);
System.out.println("starting local server on port 20001...");
}
catch (IOException e){
System.out.println("Could not listen on port 20001");
}

try{
client = server.accept();
}
catch (IOException e) {
System.out.println("Accept failed: 8123");
}

try{
//Establish a input stream from client socket
// * for reading data from client port
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
//Establish a output stream to client socket
// * for writing data to client port
out = new PrintWriter(client.getOutputStream(), true);
}
catch (IOException e) {
System.out.println("Accept failed: 8123");;
}

// while(true)
{
try{
System.out.println("* starting to cope with client data *");
String clientData = in.readLine();
System.out.println("receive data from client (" + clientData + ")");
//line = in.readLine();
System.out.println("send data to client (" + clientData + ")");
out.println("This is a message from server");
out.flush();
}
catch (IOException e) {
System.out.println("Read failed");
e.printStackTrace();
}
}
}


}





import java.io.*;
import java.net.*;


class Client{

public static void main(String args[]){
try {
Socket socket = new Socket("localhost", 20001);
System.out.println("connecting to server localhost on port *20001*...");

//ready to send data to server
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("This is the message from a client");
out.flush();
//ready to receive data from server
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(in.readLine());

}
catch (UnknownHostException e){
System.out.println("Unknown host");
}
catch (IOException e){
e.printStackTrace();
}
}
}
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help, now it works.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic