简体   繁体   English

简单的Java多线程套接字应用程序

[英]Simple Java multi-threaded socket application

I have this simple multi-threaded java socket application. 我有这个简单的多线程Java套接字应用程序。 Using classes Client.java 使用类Client.java

public class Client {

private static Socket socket;
private static boolean waitForServer = false;
public static void main(String[] args) throws IOException {
    while(true){
        socket = new Socket("localhost", ServerPortInfo.getPort());
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader bufferedReader = new java.io.BufferedReader(new InputStreamReader(System.in));

        while(true){
            PrintWriter.println(name + " Hello");
                waitForServer = true;

            if (waitForServer){
                BufferedReader inputBufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String inputString = null;
                while((inputString = inputBufferedReader.readLine()) != null){
                    System.out.println(inputString);
                    break;
                }

            }

        }

    }
  }
}

And Server.java 和Server.java

public class Server {

public static ArrayList<ServerThread> connections = new ArrayList<ServerThread>();

public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
    // Init server functionality
    new Server().runServer();


}

// Implementing server functionality
public void runServer() throws IOException{

    ServerSocket serverSocket = new ServerSocket(ServerPortInfo.getPort());
    System.out.println("Server is running... Waiting for connections");
    while (true){
        Socket socket = serverSocket.accept();
        // After connection handle clients in threads
        ServerThread newThread = new ServerThread(socket);
        connections.add(newThread);
        newThread.start();
    }

}
}

And then a Thread that handles that connection... 然后是一个处理该连接的线程...

public class ServerThread extends Thread {

private static Socket socket;
public static boolean alive = true;

ServerThread(Socket socket){
    this.socket = socket;
}

public void run(){
    if (alive) {
        //DO STUFF NOW
}       

}

And now when I have multiple connections. 现在,当我有多个连接时。 It creates a different Thread for every connection. 它为每个连接创建一个不同的线程。 What should i do when a user wants to disconnect from this server? 当用户想要从该服务器断开连接时该怎么办?

Should i kill the thread, socket? 我应该杀死线程,套接字吗?

I tried just saying to the thread alive = false; 我试着只是对线程说alive = false; so the run would just stop running. 因此运行将停止运行。 But this breaks other connections as well. 但这也会破坏其他连接。

EDIT 编辑

ServerThread -> ServerThread->

private Socket socket;

And a function -> 和功能->

void closeConnection() throws IOException{
    this.socket.close();
}

works like a charm. 奇迹般有效。

public static variables are a recipe for disaster (as I'm sure you've just found out the hard way). 公共静态变量是灾难的根源(因为我敢肯定,您已经找到了很难的方法)。 But as @cktang said, close the socket, then kill that thread (not the others). 但是正如@cktang所说,关闭套接字,然后杀死该线程(而不是其他线程)。 -@JoeC -@ JoeC

Changed private static Socket socket; 更改了private static Socket socket; to private Socket socket; private Socket socket; . Closed the socket and then Thread. 关闭套接字,然后关闭线程。

The problem is with 问题出在

public static boolean alive = true;
private static Socket socket;

As it is static, it influences all your ServerThread 's, but each ServerThread (aka connected client) should have its own socket and flag indicating if it's alive or not. 由于它是静态的,因此会影响您所有的ServerThread ,但是每个ServerThread (也称为已连接的客户端)应具有自己的套接字和标志,以指示其是否有效。

Remove the static modifier from both attributes and make the flag 从两个属性中删除static修饰符并进行标记
private volatile boolean alive = true;

At first close the socket a then stop the thread (and remove it from the list). 首先,关闭套接字a,然后停止线程(并将其从列表中删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM