Answer the question
In order to leave comments, you need to log in
Why is windows not accepting connections?
Now I'm studying network programming. Wrote (copy-pasted and slightly tweaked) the server and client in java. Mint is installed on the laptop (ip 192.168.1.4), on a Windows XP computer (ip 192.168.1.6). I start the server on Linux, then in windows 'java Client 192.168.1.4' and enter commands, everything works. But when I start the server in Windows, and in Linux I write 'java Client 192.168.1.6', the client does not connect. On XP, my firewall is disabled, there is no antivirus, I don’t understand what can interfere?
Client.java
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] ar) {
int serverPort = 5000; // здесь обязательно нужно указать порт к которому привязывается сервер.
//String address = "192.168.1.4"; // это IP-адрес компьютера, где исполняется наша серверная программа.
// Здесь указан адрес того самого компьютера где будет исполняться и клиент.
//System.out.println(ar[0]);
String address = ar[0];
try {
InetAddress ipAddress = InetAddress.getByName(address); // создаем объект который отображает вышеописанный IP-адрес.
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
Socket socket = new Socket(ipAddress, serverPort); // создаем сокет используя IP-адрес и порт сервера.
socket.setReuseAddress(true);
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(); // ждем пока пользователь введет что-то и нажмет кнопку Enter.
//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.print("->");
}
} catch (Exception x) {
x.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
import java.text.*;
import java.util.*;
public class Server
{
public static void main(String[] args)
{
//System.out.println("Hello");
int port = 5000;
try {
ServerSocket myserver = new ServerSocket(port);
System.out.println("Waiting for a client...");
myserver.setReuseAddress(true);
Socket socket = myserver.accept();//Ждем подключение клиента
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String line = null;
System.out.println(socket.getLocalAddress());
while(true)
{
line = in.readUTF();
switch(line)
{
case "data":
System.out.println("Дата");
break;
case "time":
Date d = new Date();
SimpleDateFormat format1 = new SimpleDateFormat("dd.MM.yyyy hh:mm");
System.out.println(format1.format(d));
break;
case "exit":
socket.close();
break;
default:
System.out.println(line +" - Ошибка! Такой комманды нет!");
//System.out.println("->");
}
//System.out.println(line);
out.writeUTF(line);
out.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Answer the question
In order to leave comments, you need to log in
Look with wireshark on the client and on the server to see if there are necessary packets, if a connection is being established, and if a data packet is coming.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question