[英]Java ServerSocket won't send or receive messages from client
I have these two classes Client.java and Server.java , each containing a main
method. 我有这两个类Client.java和Server.java ,每个类都包含一个
main
方法。 The idea is very simple and the code in them is very similar. 这个想法非常简单,并且其中的代码非常相似。 The objective I'm trying to achieve is opening a server that listens on a specific port using
ServerSocket
, then open a client and connect to the server using Socket
so that I can finally send messages from client to server and reverse until I close the connection. 我要实现的目标是打开一个服务器 ,该服务器使用
ServerSocket
侦听特定端口,然后打开客户端并使用Socket
连接到服务器,以便最终将消息从客户端发送到服务器,然后反向发送直到关闭连接。
So far, I tried the easiest example, opened server on port 4444, then on the same PC I've opened a client to connect to the address 127.0.0.1 (localhost), on port 4444. Everything runs smooth, the connection is established successfully every time, excepting it won't send any message, either from server to client or client to server. 到目前为止,我试过最简单的例子,打开端口 4444 的服务器 ,然后在同一台PC上,我开了一个客户端连接到地址 127.0.0.1(本地主机),在端口 4444一切运行平稳,建立连接每次都成功,除非它不会从服务器到客户端或从客户端到服务器发送任何消息。
The final question is, what should I do to be able to exchange messages between server and client? 最后一个问题是,我应该怎么做才能在服务器和客户端之间交换消息? What should I modify?
我应该修改什么?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
private static Socket client;
private static BufferedReader in, console;
private static PrintWriter out;
private static String serverAddress;
private static int port;
public static void main(String[] args) {
try {
console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Connect to server address (example 192.168.10.11): ");
serverAddress = console.readLine();
System.out.print("Port: ");
port = Integer.parseInt(console.readLine());
System.out.println("\nTrying to connect...");
client = new Socket(serverAddress, port);
System.out.println("Connected to " + client.getRemoteSocketAddress());
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
out.write("Connected!!!!!!");
Thread input = new Thread() {
public void run() {
String tmp;
try {
while((tmp = in.readLine()) != null) {
System.out.println("Server: " + tmp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread output = new Thread() {
public void run() {
String tmp;
try {
while((tmp = console.readLine()) != null) {
out.print(tmp);
}
} catch(IOException e) {
e.printStackTrace();
}
}
};
input.start();
output.start();
while(true) {
if(!input.isAlive() && !output.isAlive()) {
client.close();
in.close();
console.close();
out.close();
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static ServerSocket serverSocket;
private static Socket server;
private static BufferedReader in, console;
private static PrintWriter out;
private static int port;
public static void main(String[] args) {
try {
console = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Open server on port (example 1234): ");
port = Integer.parseInt(console.readLine());
System.out.println("Server open");
serverSocket = new ServerSocket(port);
System.out.println("Waiting for a client to connect...\n");
server = serverSocket.accept();
System.out.println("Client " + server.getRemoteSocketAddress() + " connected!");
in = new BufferedReader(new InputStreamReader(server.getInputStream()));
out = new PrintWriter(server.getOutputStream(), true);
Thread input = new Thread() {
public void run() {
String tmp;
try {
while((tmp = in.readLine()) != null) {
System.out.println("Client: " + tmp);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread output = new Thread() {
public void run() {
String tmp;
try {
while((tmp = console.readLine()) != null) {
out.print(tmp);
}
} catch(IOException e) {
e.printStackTrace();
}
}
};
input.start();
output.start();
while(true) {
if(!input.isAlive() && !output.isAlive()) {
serverSocket.close();
server.close();
in.close();
console.close();
out.close();
break;
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
You are reading lines but you aren't wring lines. 您正在阅读台词,但没有拧线。 Instead of
out.write()
and out.print()
you should use out.println()
. 取而代之的
out.write()
和out.print()
你应该使用out.println()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.