我有一个TCP-Client和一个用Java编写的TCP-Server。服务器正在等待来自客户端的指令,但也应该能够向客户端发送指令。 我可以让客户端向服务器发送内容并等待回复。但是,我不能像以前那样在没有发送内容的情况下等待消息。
TCP客户端
public class TCPClient {
static DataOutputStream toServer;
static BufferedReader fromServer;
static Socket socket;
public static void main(String[] args) throws Exception {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("*************************");
System.out.println("* Client *");
System.out.println("*************************");
System.out.println("INSTRUCTION | EFFECT");
System.out.println("aktiv | ready to do something");
System.out.println("exit | disconnect");
System.out.println();
System.out.print("Please enter the IP-Address of the Server: ");
String ip = input.readLine();
System.out.println();
try {
socket = new Socket(ip, 9999);
} catch (Exception e) {
System.out.println("Can not connect to Server!");
}
toServer = new DataOutputStream(socket.getOutputStream()); // Datastream FROM Server
fromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (sendRequest()) {
receiveResponse();
}
socket.close();
toServer.close();
fromServer.close();
}
private static boolean sendRequest() throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line;
boolean holdTheLine = true; // Connection exists
System.out.print("> ");
line = input.readLine();
switch (line) {
case "aktiv":
toServer.writeBytes("active" + '\n');
break;
case "exit":
holdTheLine = false;
break;
default:
break;
}
return holdTheLine;
}
private static void receiveResponse() throws IOException {
System.out.println("Server: " + fromServer.readLine() + '\n');
}
}
TCP-服务器
public class TCPServer {
static boolean connected = true;
public static void main(String[] args) throws Exception {
System.out.println("********************************");
System.out.println("* Server *");
System.out.println("********************************");
System.out.println("INSTRUCTION | EFFECT");
System.out.println("ok | send an ok to client");
ServerSocket listenSocket = new ServerSocket(9999);
while (true) {
final Socket client = listenSocket.accept();
Thread newClientThread = new Thread(new Runnable() {
@Override
public void run() {
multithreadedServer(client);
}
});
newClientThread.start();
}
}
public static void multithreadedServer(Socket client) {
String line;
final BufferedReader fromClient;
final DataOutputStream toClient;
Thread cmdForClient;
try {
fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
toClient = new DataOutputStream(client.getOutputStream());
while (connected) {
cmdForClient = new Thread(new Runnable() {
@Override
public void run() {
try {
String line = fromClient.readLine();
System.out.println("Client: " + line);
if (line.equals("exit")) {
connected = false;
}
} catch (Exception e) {
System.out.println(e);
}
}
});
cmdForClient.start();
final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
String reply = input.readLine();
if (reply.equals("ok")) {
toClient.writeBytes("OK." + '\n');
}
} catch (Exception e) {
System.out.println(e);
}
}
fromClient.close();
toClient.close();
client.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
答案 0 :(得分:0)
客户端 - 服务器方案的典型操作是客户端向服务器发送请求。但是,在对等应用程序中,两个端点都可以充当客户端和服务器。唯一的区别是打开连接的端点。在您的情况下,问题是只有“服务器”使用接收器线程。在客户端启动接收器线程,您的问题应该得到解决。您几乎可以只从客户端的服务器重用您的线程代码。在打开与服务器的连接后,只需将套接字传递给接收线程。
编辑:
在您的客户中:
var pageLoadsPerHour = [Int]()
for hour in jsonContent {
if let pageLoads = (hour as? NSDictionary)?["m_page_loads"] as? Int {
pageLoadsPerHour.append(pageLoads)
}
}
其中socket是服务器的套接字。您可能需要更新多线程服务器以获取客户端操作的任何细节或差异,但原则应该相同。