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.
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..
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnjava/c...