tags:

views:

204

answers:

4

I'm writing my first java client/server program which just establishes a connection with the server sends it a sentence and the server sends the sentence back all capitalized. This is actually an example straight out of the book, and it works well and fine when I'm running the client and server on the same machine and using localhost for the server address. But when I put the client program on a different computer, it times out and never makes a connection with the server. I'm not sure why this is and its kind of lame making a your first client/server program and not actually be able to use it on two different machines. Here is the client code:

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

public class TCPClient {
    public static void main(String argv[]) throws Exception {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

        Socket clientSocket = new Socket("localhost", 6789);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        sentence = inFromUser.readLine();
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();
        System.out.println(modifiedSentence);
        clientSocket.close();
    }
}

Here is the server code:

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

public class TCPServer {
    public static void main(String args[]) throws Exception {
        String clientSentence;
        String capitalizedSentence;
        ServerSocket welcomeSocket = new ServerSocket(6789);

        while(true) {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
        }
    }
}

The only thing I change when I run it on two different machines is the client program makes its socket with the IP address of the machine with the server program (which I got from whatismyipaddress.com). Thanks a lot for any help.

Update: I am indeed on a campus and it seems that its probably not allowing me to use that random port. Any suggestions on finding out what port I can use and or a port that is more than likely allowed?

+3  A: 

It's probably a firewall issue. Make sure you port forward the port you want to connect to on the server side. localhost maps directly to an ip and also moves through your network stack. You're changing some text in your code but the way your program is working is fundamentally the same.

Chris H
try testing connection to your server with telnet <ip> <port> from a client machine. If you get "connection refused" or timeout, that's a firewall problem
Yoni Roit
Just remembered I'm at school, so I have no router and like I said before no firewall either.
Anton
Not sure how to use telnet but ping worked with the ip address.
Anton
If you're at school, you almost certainly have a router. Ping won't help much; it verifies that the machine is reachable, but not if your port is open. You could try using your browser to connect to the remote machine, you should get something back like "GET / HTTP 1.1". Or you could try something like curl.
TMN
A: 

With new Socket(String, int) you have to give a hostname and not an IP address.

tangens
Is there a way to make a socket with an IP address and port?
Anton
This random tutorial says its ok to pass an IP in the string for the Socket constructor. http://www.java2s.com/Tutorial/Java/0320__Network/CreateSocketbyIPaddressandportnumber.htm
Anton
`Socket` allows either the hostname or the textual representation of the IP address; it boils down to INetAddress.getByName: http://java.sun.com/javase/6/docs/api/java/net/InetAddress.html#getByName%28java.lang.String%29
Michael Brewer-Davis
Sorry, I'm wrong. I just looked at the javadoc of `Socket( String, int )` and it talks about "hostname". But giving a textual representation of the IP address is fine, too.
tangens
A: 

If you got your IP address from an external web site (http://whatismyipaddress.com/), you have your external IP address. If your server is on the same local network, you may need an internal IP address instead. Local IP addresses look like 10.X.X.X, 172.X.X.X, or 192.168.X.X.

Try the suggestions on this page to find what your machine thinks its IP address is.

Michael Brewer-Davis
(Note that not all 172.X.X.X addresses are local.)
Michael Brewer-Davis
I got it from ipconfig as well it was it was the same.
Anton
A: 

Instead of using the IP address from whatismyipaddress.com, what if you just get the IP address directly from the machine and plug that in? whatismyipaddress.com will give you the address of your router (I'm assuming you're on a home network). I don't think port forwarding will work since your request will come from within the network, not outside.

Neal
I did ipconfig as well and it gave the same address as whatismyipaddress.com
Anton
Are you on a campus network or something? They may have their switches/routers configured to block certain ports, even from within the network. If you can ping the other machine while this doesn't work , that port may be blocked.
Neal
Yes I am on a campus I'm guessing I should try it with port 80 and see if I have any luck?
Anton