Привет. Я от сюда(Не работает клиент-сервер на Android). У меня проблема при конекте по TCP между двумя компами. Ноуты подключены к одной сети wifi. Обращаюсь к ним по локальным ip-адресам в сети. Когда запускаю клиент и сервер на одном компе, то все работает. Фаервол отключен. Порты открыты. Не знаю в чем проблема. Вот если что код клиента и сервера.
public class client {
public static void main(String[] ar) {
String address = ar[0]; //параметрами получаем адрес и порт
int serverPort = Integer.parseInt(ar[1]);
try {
InetAddress ipAddress = InetAddress.getByName(address);
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?"+ ipAddress);
Socket socket = new Socket(ipAddress, serverPort);
System.out.println("Yes! I just got hold of the program.");
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();
while (true) {
line = keyboard.readLine();
System.out.println("Sending this line to the server...");
out.writeUTF(line);
out.flush(); // заставляем поток закончить передачу данных.
line = in.readUTF();
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}
} catch (Exception x) {
x.printStackTrace();
}
}
////////Сервер
public class Server {
public static void main(String[] ar){
int port = Integer.parseInt(ar[0]);
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Waiting for a client...");
Socket socket = ss.accept();
System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
System.out.println();
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("The dumb client just sent me this line : " + line);
System.out.println("I'm sending it back...");
out.writeUTF(line);
out.flush();
System.out.println("Waiting for the next line...");
System.out.println();
}
} catch(Exception x) { x.printStackTrace(); }
}
}
Попробуй мой отредактированный вариант. Обрати внимание на передаваемые параметры - адрес и порт.
public class Client {
static Socket socket;
static DataInputStream in;
static DataOutputStream out;
public static void main(String[] ar) {
String address = ar[0]; //параметрами получаем адрес и порт
int serverPort = Integer.parseInt(ar[1]);
try {
InetAddress ipAddress = InetAddress.getByName(address);
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?"+ ipAddress);
socket = new Socket(ipAddress, serverPort);
System.out.println("Yes! I just got hold of the program.");
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
in = new DataInputStream(sin);
out = new DataOutputStream(sout);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();
while (true) {
line = keyboard.readLine();
System.out.println("Sending this line to the server...");
out.writeUTF(line);
out.flush(); // заставляем поток закончить передачу данных.
line = in.readUTF();
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}
} catch (Exception x) {
x.printStackTrace();
} finally {
try {
in.close();
out.close();
if(socket!=null) {
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Соединение с портом разорвано разорвано");
}
}
}
public class Server {
static ServerSocket ss;
public static void main(String[] ar){
int port = Integer.parseInt(ar[0]);
try{
ss = new ServerSocket(port);
}catch (IOException e) {
System.out.println("Порт: "+port+" занят");
return;
}
try {
System.out.println("Waiting for a client...");
while(true) {
Socket socket = ss.accept();
System.out.println("Got a client :) ... Finally, someone saw me through all the cover!");
System.out.println();
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
line = in.readUTF();
System.out.println("The dumb client just sent me this line : " + line);
System.out.println("I'm sending it back...");
out.writeUTF(line);
out.flush();
System.out.println("Waiting for the next line...");
System.out.println();
}
} catch(Exception x) {
x.printStackTrace();
} finally {
try {
if(ss!=null) {
ss.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Соединение с портом разорвано разорвано");
}
}
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
(Ранее писал) Начал изучать JSF, скачал запустил GlassFish 4 (админовский порт 4848 открылся нормально) развернул проект от Хорстмана (писал он) и в общем...