Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

27
Help on connecting 2 computer through the internet with Java?
Post Body

Hi, I'm just trying to connect 2 computers through the internet using Java from different locations (not on the same network) and I've run into some errors..

Here is the code for the server:

public void activateServer() throws IOException {
server = new ServerSocket(5000);
System.out.println("server listening...");
Socket s = server.accept();
System.out.println("server accepted the connection");
InetAddress obj = s.getInetAddress();
System.out.println("request coming from " obj);
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String message = br.readLine();
System.out.println("message is : " message);
br.close();
server.close();
}

and this is the client:

public static void main(String[] args){
        Socket s = null;
        BufferedOutputStream b = null;
        InetAddress obj = null;

        try{
            obj = InetAddress.getByName("192.168.1.123");
            s = new Socket(obj, 5000);
            System.out.println("client connected");
            String message = "Hello there neighbor";
            byte[] arr = message.getBytes();
            b.write(arr);
            b.write(13);
            b.write(10);
            b.flush();
            b.close();
            s.close();

        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }

The IP address is the one I got from running ipconfig in command prompt in the server computer so I put it there, I'm not sure if that's right.. I ran the server first and then the client but the server doesn't find the client and the client times out. Am I doing it wrong? Thanks in advance for the help..

Author
Account Strength
90%
Account Age
5 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,724
Link Karma
1,904
Comment Karma
820
Profile updated: 12 hours ago
Posts updated: 1 week ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago