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); } }