gpt4 book ai didi

java - TCP线程服务器/客户端

转载 作者:可可西里 更新时间:2023-11-01 02:47:04 24 4
gpt4 key购买 nike

我正在尝试制作一个接受多个客户端的线程 TCP 服务器。它只接受一个客户。关于如何使服务器接受多个客户端的任何想法?

到目前为止,这是我尝试过的:

我更改了服务器代码。我同时运行了服务器和客户端,但似乎只有一个线程在工作。我应该更改端口还是其他什么?

服务器代码:

package tcpecho;

import java.io.*;
import java.net.*;


class TCPserver implements Runnable {
private static String clientMSG;
private static String serverRespond;
private static ServerSocket MySocket=null;
private static Socket client;
private static Socket connectionSocket = null;
private BufferedReader inFromClient = null;
public TCPserver(Socket client){
TCPserver.client=client;}

public static void main(String[] args) {

try {
MySocket = new ServerSocket(6880);
System.out.println("TCP server is listining");
} catch (IOException e) {

System.err.println("TCP server is not listining : "+MySocket.getLocalPort());
}

try {
connectionSocket = MySocket.accept();
(new Thread(new TCPserver(connectionSocket))).start();

System.out.println("Is connected now : "+connectionSocket.getInetAddress());

} catch (IOException e) {

System.err.print("Accept failed");
}
}



public void socketProcess( )
{

try {
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
} catch (IOException e) {
System.err.println("Message from client is not reachable");
}

DataOutputStream outToClient = null;
try {
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
} catch (IOException e) {
System.err.println("Message to client is not reachable");

}

try {
clientMSG = inFromClient.readLine();
} catch (IOException e) {
System.err.println("Message from client is not readable");
}

serverRespond = clientMSG +connectionSocket.getInetAddress()+ '\n';

try {
outToClient.writeBytes(serverRespond);
} catch (IOException e) {
System.err.println("Message to client is not out");
}

try {
MySocket.close();
} catch (IOException e1) {

System.err.println("Unable to close the socket");
}

}



@Override
public void run() {
socketProcess();

}

这是我的客户端代码:

package task3;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

class Client {
public static void main(String argv[]) throws Exception {

String str;
String strFromServer;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket client = null;
// for(int i=0;i<10;i++){
int port = 6880;
try {
client = new Socket("192.168.56.1", port);
} catch (Exception e) {
System.err.println("Couldn't connect to the port" + port);
System.exit(1);
}

DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("Write a message to the server :");
str = inFromUser.readLine();

outToServer.writeBytes(str + '\n');
strFromServer = inFromServer.readLine();
System.out.println("Respond from server: " + strFromServer);
client.close();
}
}

最佳答案

在你这样做之后

connectionSocket = MySocket.accept();

您需要将此 connectionSocket 实例传递给一个新线程并在该线程上调用 start

此外,将所有connectionSocket 处理逻辑移动到Thread 类的run 方法
(这个逻辑目前在上面提到的这一行之后的 main 方法中)。

要实现您自己的Thread,您可以扩展Thread 或实现Runnable

查看此链接了解更多详情:Java Threads

关于java - TCP线程服务器/客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com