Made Client (Client.class) and Server (Server.class). When the client starts, the IP and the port are entered and it connects to the Server. When everything is done on one computer, everything works, I enter Ip: 127.0.0.1 and port: 6666 and the client starts sending messages to the Server, and the Server accepts them and outputs them. All OK. But I decided to throw Server.clas to a friend. He launched it and told me his un. I entered his un in the Client and the port: 6666. But I could not connect to it. And he is to me too. Nothing that I have Linux, and he has Windows?

What could be the problem?

Server.java:

import java.net .*; import java.io .*; public class Server { public static void main(String[] args) { int port = 6666; try { ServerSocket ss = new ServerSocket(port); System.out.println("Waiting for client..."); Socket socket = ss.accept(); print("Client opened...\n"); InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream(); DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout); String line = null; while (true) { line = in .readUTF(); System.out.println("Waiting for the next line..."); System.out.println("Client: " + line); print("\n"); } } catch (Exception x) { print("Error: Client is closed!\n"); } } static void print(String s) { System.out.print(s); } } 

Client.java:

 import java.net .*; import java.io .*; public class Client { public static void main(String[] args) { int serverPort = 0; String address = null; try { BufferedReader keyboard = new BufferedReader(new InputStreamReader(System. in )); print("Type address of the server: "); address = keyboard.readLine(); print("Type port of the server: "); serverPort = Integer.parseInt(keyboard.readLine()); InetAddress ipAddress = InetAddress.getByName(address); Socket socket = new Socket(ipAddress, serverPort); InputStream sin = socket.getInputStream(); OutputStream sout = socket.getOutputStream(); DataInputStream in = new DataInputStream(sin); DataOutputStream out = new DataOutputStream(sout); String line = null; print("Type the line: "); while (true) { line = keyboard.readLine(); System.out.println("Sending this line to the server..."); out.writeUTF(line); out.flush(); System.out.println(); print("Type the next line: "); } } catch (Exception e) { System.out.println("Error: Server don't found!"); } } static void print(String s) { System.out.print(s); } } 
  • rights to the external connection for the port register, maybe this is the case - cuts the firewall - vv2cc

1 answer 1

Windows and Linux is not important.

The problem is that both of you are probably under NAT. You exchanged local IPs, and they mean nothing behind the nearest router.

You need either a static IP or "tricky" network stuff, usually associated with configuring the router.

  • Thank. Maybe you need to enter not the IP address, but the computer name? For example, I now have ip-83-149-3-98.nwgsm.ru. 2ip.ru - Mencey
  • google: what is my ip - jmu
  • If your computer pings by name from a friend, you can start a server on it. In this case, the exchange must go, even if the client is under NAT. Quickly looked, nslookup gives the address, but ping does not go (traceroute too). Of course, this can only mean that ICMP on your router is closed. If you have TCP open (and LISTEN) ports, test the connection with telnet. - avp
  • And better not telnet, and netcat ( ru.wikipedia.org/wiki/Netcat ). Can work as a client and server. There is a port and under Windows - alexlz