Server Code

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author efron
 */
public class Server {

	public static void main(String[] args) {
		try {
			ServerSocket serverSocket = new ServerSocket(Integer.parseInt("49999"));
			System.out.println(serverSocket.getLocalPort());
			while (true) {
				System.out.println("Waiting for Client...(port = " + 49999 + ")");
				Socket acceptSocket = serverSocket.accept();
				
				System.out.println("Connected to: " + acceptSocket.getRemoteSocketAddress());

				DataInputStream in = new DataInputStream(acceptSocket.getInputStream());
				DataOutputStream out = new DataOutputStream(acceptSocket.getOutputStream());
				System.out.println(in.readUTF());
				out.writeUTF("You have been connected to the server" + acceptSocket.getLocalSocketAddress());
				acceptSocket.close();
			}
		} catch (Exception e) {
		}
	}
}

Client Code

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author efron
 */
public class Client {
	public static void main(String[] args){
		//If I put localhost or 127.0.0.1 as the servername, it works
		String serverName = "123.123.123.123";		//put my own ip address in servername, got it from http://www.whatismyip.com/
		int portNumber = Integer.parseInt("49999");

		try {
			System.out.println("Connecting to server " + serverName + " on port " + portNumber);
			Socket client = new Socket(InetAddress.getByName(serverName), portNumber);
			System.out.println("Connected to server!");

			DataInputStream in = new DataInputStream(client.getInputStream());
			DataOutputStream out = new DataOutputStream(client.getOutputStream());

			out.writeUTF("Hi Server! I'm " + client.getLocalSocketAddress());
			System.out.println("Message from Server: " + in.readUTF());

			client.close();
		} catch (IOException e) {
			System.out.println("IOException: " + e.getMessage());
		}

	}
}

I run the server, and the server will be waiting at port 49999.
Then I run the client. But the Client gives an IOException, Connection refused.
Can anybody tell me whats wrong.

I asked somebody to do the same thing that I do here(I give him my code and ask him to run it) with Ubuntu 10.10 and it works just fine, but in my Ubuntu 11.10, I keep trying various ways to write the code that means the same thing a thousand times and it still doesnt work.

Can anybody think of how this might have happened?

Please help. Thanks

P.S. The ip address is changed to 123.123.123.123 but when I run it its my ip address. And when my friend run it its his ip address

hope you are running the client and server on the same machine. If so make the ip address as 127.0.0.1 and try again.

hope you are running the client and server on the same machine. If so make the ip address as 127.0.0.1 and try again.

Do u even read the information I give you?
I said it works if I use the loop address 127.0.0.1 or if I put localhost. The problem is it doesnt work if I use the external ip of my computer. But when my friend uses his own external ip it works.

Do u even read the information I give you?

Yes i do read.

I said it works if I use the loop address 127.0.0.1 or if I put localhost

You never mentioned that you have tried loop address or localhost.Atleast i cant find that word in your post.

Anyway you are saying that it works in your friend's computer but not in yours.
So clearly the problem is not with the code. So check you have used correct IP address and try with different port numbers (sometimes those port numbers are being already used by another processes.)
As you have said that you are using ubuntu , try the netstat command and check the server status.

Im sure I have correct IP Address, I tried with different port numbers > 1024. I am also sure its not used by another process.

I am not sure how exactly to use netstat, but when I run the server and run
netstat | grep 49999

I got nothing. So how do I check the server status?

And I have that in my code, where I hardcoded my serverName

//If I put localhost or 127.0.0.1 as the servername, it works
		String serverName = "123.123.123.123";		//put my own ip address in servername, got it from http://www.whatismyip.com/

When creating the ServerSocket, make sure you are binding to the same host and port which the client is trying to connect to. Look into the different types of constructors offered by ServerSocket class and use the one which also allows you to specify the IP to bind to.

If I bind the server socket, I would bind it to 127.0.0.1
In which case I have tried that, does not make any difference.

Anyway I'll give you guys the error message of the client when trying to connect to server. the error is made from this line:

Socket client = new Socket(InetAddress.getByName(serverName), portNumber);

java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at tester2.Client.main(Client.java:22)

> If I bind the server socket, I would bind it to 127.0.0.1

That anyways wouldn't work because in that case it won't be accessible by external clients (given that this is a loopback address).

Your ServerSocket creation looks OK. The problem might be of unreachable host/port due to blocking of communication by your ISP. Go to http://canyouseeme.org/ which shows your IP address and asks you to input the port number of your running application. If after clicking the "check" button you can see an output of the form: "I can see your service on X.X.X.X on port (XXXX)", it means that your service is visible to others. If the test times out, then it is a problem with your firewall/ISP settings.

> If I bind the server socket, I would bind it to 127.0.0.1

That anyways wouldn't work because in that case it won't be accessible by external clients (given that this is a loopback address).

Your ServerSocket creation looks OK. The problem might be of unreachable host/port due to blocking of communication by your ISP. Go to http://canyouseeme.org/ which shows your IP address and asks you to input the port number of your running application. If after clicking the "check" button you can see an output of the form: "I can see your service on X.X.X.X on port (XXXX)", it means that your service is visible to others. If the test times out, then it is a problem with your firewall/ISP settings.

Wow thanks for cornering the problem to firewall/ISP settings. The connection times out.
I ran the server again. and then at canyouseemee.org it says that the connection times out when listening at my IP at port 49999 which is the port I bind it to.

I am still new with these networking stuff, can you tell me what I should do now? I am using ubuntu 11.10 and I am not really familiar with the firewall settings. and if it's my ISP problem then nothing I could do really.

Unfortunately, I don't have a Ubuntu installation handy to help you out.

I'd suggest you to either ask your friend to help you out with the firewall configuration (assuming he had tweaked his own) or search around for "ubuntu firewall config" which brings up quite a few articles that might help you pin down the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.