I've created a Java server with sockets, just how do print to ALL sockets?

大城市里の小女人 提交于 2019-12-04 03:43:49

First you need to keep track of all connected clients:

final List<SocketThread> clients = new ArrayList<>();

while (isListening){
    SocketThread client = new SocketThread(server.accept()).start();
    clients.add(client);
}

Having such list if one client receives "print2all Hi" it simply iterates over all clients and sends message to each of them. To do this you'll most likely have to expose some method on SocketThread that will access client socket. This means you'll have to change out variable to field.

Alternative approach is to keep a list of client sockets. But this breaks encapsulation badly. Also you might run into nasty IO/thread-safety issues if sockets are exposed directly. Better hide them behind some API (like SocketThread method) and do the synchronization properly inside.

A full implementation of what you are looking.

Server

package tcpserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TCPServer {

    private int serverPort = 25565;
    private ServerSocket serverSocket;
    private List<ConnectionService> connections = new ArrayList<ConnectionService>();

    public TCPServer() {
        try {
            serverSocket = new ServerSocket(serverPort);
            System.out.println("Waiting...");
            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("Connected: " + socket);
                ConnectionService service = new ConnectionService(socket);
                service.start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        new TCPServer();
    }

    class ConnectionService extends Thread {

        private Socket socket;
        private BufferedReader inputReader;
        private PrintWriter outputWriter;
        //private String username;

        public ConnectionService(Socket socket) {
            this.socket = socket;
            try {
                inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                outputWriter = new PrintWriter(socket.getOutputStream(), true);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }

        @Override
        public void run() {
            while (true) {
                try {
                    String receivedMessage = inputReader.readLine();
                    System.out.println(receivedMessage);
                    StringTokenizer stoken = new StringTokenizer(receivedMessage);
                    String fargument = stoken.nextToken();
                    if (fargument.equals("print2all")) {
                        this.sendToAnyone(stoken.nextToken());
                    }
                } catch (IOException ex) {
                    Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NullPointerException e) {
                    System.out.println(e.getMessage());
                } finally {
                    outputWriter.close();
                }

            }
        }

        protected void sendMessage(String message) {
            outputWriter.println(message);
        }

        private void sendToAnyone(String message) {

            for (ConnectionService connection : connections) {
                connection.sendMessage(message);
            }
        }
    }
}

Client

package tcpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class tcpClient extends javax.swing.JFrame {

    private Socket socket;
    private BufferedReader inputReader;
    private PrintWriter outputWriter;

    public tcpClient() {
        connectToServer();
    }

    private void connectToServer() {
        try {
            socket = new Socket(InetAddress.getByName("localhost"), 25565);
            inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            outputWriter = new PrintWriter(socket.getOutputStream(), true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        new Thread() {
            @Override
            public void run() {
                receiveData();
            }
        }.start();
    }

    private void receiveData() {
        try {
            while (true) {
                System.out.println(inputReader.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void sendData(String messageToSend) {
        outputWriter.println(messageToSend);
    }

    public void closeSocket() {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                tcpClient client = new tcpClient();
                client.sendData("print2all Hi");
                client.closeSocket();
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!