aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Connection Refused Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Connection Refused Error" Watch "Connection Refused Error" New topic
Author

Connection Refused Error

Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
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 ]

a beginner in java
Tal Nathan Tal Nathan
Greenhorn

Joined: Feb 26, 2006
Posts: 13
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

Joined: Nov 27, 2002
Posts: 124
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

Joined: Feb 26, 2006
Posts: 13
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

Joined: Nov 27, 2002
Posts: 124
Thanks for your help, now it works.

 
jQuery in Action, 2nd edition
 
subject: Connection Refused Error