Сделал Клиент (Client.class) и Сервер (Server.class). При запуске клиента вводится Ip и порт и он поключается к Серверу. Когда всё делается на одном компе, всё работает, я ввожу Ip: 127.0.0.1 и порт: 6666 и клиент начинает посылать сообщения Серверу, а Сервер их принимает и выводит. Всё ок. Но я решил скинуть Server.clas другу. Он его запустил и сказал мне свой ип. Я ввёл его ип в Клиенте и порт: 6666. Но не мог к нему подключиться. И он ко мне тоже. Ничего, что у меня Linux, а у него Windows?
В чём может быть проблема?
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);
}
}