E
E
einbleiben2014-07-24 10:59:52
Java
einbleiben, 2014-07-24 10:59:52

How can I use Java.net.* to connect two computers on a network?

Good afternoon, ladies and gentlemen. I have two computers with Internet access. On the first, following the manuals, I write the following code:

import java.net.*;
import java.io.*;
 
public class Lied {
       
        public static void main (String args[]) throws IOException{
                try(ServerSocket serverSocket = new ServerSocket(11111)){
                        while(true){
                                try (Socket socket = serverSocket.accept()){
                                        serveClient(socket);
                                }
        }
}
}
 
private static void serveClient(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
       
        while(true){
                int request = inputStream.read();
                if (request == -1) {
                        break;
                }
                outputStream.write(request*request);
                System.out.println("Получили число: " + request);
                outputStream.flush();
        }
}
}

The program receives a number through port 11111 and, squaring it, sends it back. Using an online service, I found out the name of the computer on which the program above is written. On the second computer I run:
import java.net.*;
import java.io.*;
public class inet{
        public static void main(String args[]) throws Exception{
                try(Socket socket = new Socket("93-81-119-229.broadband.corbina.ru",11111)){
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(5);
                        outputStream.flush();
                       
                        InputStream inputStream = socket.getInputStream();
                        int response = inputStream.read();
                       
                        System.out.println(response);
                }
        }
}

Here "93-81-119-229.broadband.corbina.ru" is the hostname of the first computer. For some reason that I don't understand, the second program tries to connect for a while and throws a "Connection time out" exception. What am I doing wrong? I also ask you to indicate the necessary literature that can help with the development of the topic of networks. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
timatecue, 2014-07-24
@timatecue

This host / ip is pinged, but it is not possible to connect to it on port 80/11111.
Perhaps the matter is in firewalls, routers, forwarded ports, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question