这是我的代码:
服务器:
package com.murplyx.server; public class Main { public static void main(String args[]) { new EchoServer(9000); } }+
package com.murplyx.server; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.ServerSocket; import java.Socket; public class EchoServer { private ServerSocket server; public EchoServer(int port) { try { server = new ServerSocket(port); while (true) { Socket socket = server.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println(in.readLine() + " | MOD"); socket.close(); } } catch(Exception err) { err.printStackTrace(); } } }客户:
package com.murplyx.client; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.Socket; public class Main { public static void main(String args[]) { try { while (true) { Socket socket = new Socket("localhost", 9000); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); out.println(input.readLine()); System.out.println(in.readLine()); socket.close(); } } catch (Exception err) { System.out.println(err); } } }它可以正常工作,除了我想要服务器向客户端发送消息+| MOD时,我希望服务器将其发送给所有客户端,我该怎么做?
It works all as it should, except that I want when the server sends the "message" + " | MOD" to the client, I want the server to send that to all clients, how can I do that?
我是Java的新手,但不是编码所以如果我做了一些可以做得更容易或更好的错误的东西,请帮助我。
I am new to Java, but not to coding so please help me if I've done some wrong stuff that can be done easier or better.
请帮助。
非常感谢。
推荐答案你能做的就是将客户端套接字保存在一个数组中,然后使用for循环发送到每个套接字。
What you can do is save the client sockets in an array, and then use a for loop to send to each socket.
首先,声明你的clientSocket数组;请注意,5只是用于测试的任意大小。另外,声明一个计数器int。
First, declare your clientSocket array; note that 5 is just an arbitrary size used for testing. Also, declare a counter int.
public Socket clientSocket[] = new Socket[5]; public int intLastSocket = 0; // this should be placed where you're waiting to accept connections while (true) { printTCP("Ready to accept welcome socket"); clientSocket[intLastSocket] = welcomeSocket.accept(); intLastSocket++; } // on the server, call this to send. s is a reference to the server object public void sendToAllTCP(TCPServer s, String message) { for (Socket z : s.clientSocket) { if (z != null) { PrintStream outToClient = null; try { outToClient = new PrintStream(z.getOutputStream()); outToClient.println(message); } catch (IOException e) { TCPServer.printTCP("Caught an IO exception trying " + "to send to TCP connections"); e.printStackTrace(); } } } }IN你的代码:
package com.murplyx.server;
package com.murplyx.server;
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.ServerSocket; import java.Socket; public class EchoServer { private ServerSocket server; // use the clientSocket Array to save each new connection public Socket clientSocket[] = new Socket[5]; public EchoServer(int port) { try { server = new ServerSocket(port); // this should be placed where you're waiting to accept connections while (true) { System.out.println("Ready to accept welcome socket"); clientSocket[intLastSocket] = server.accept(); intLastSocket++; //send your message here, note that putting //it here will send it each time u add a new connection sendToAllTCP(/*the message you want to send */); } } catch(Exception err) { err.printStackTrace(); } } public void sendToAllTCP(String message) { // this is an enchanced for loop, i don't know if it's in other languages // but in java it's supposed to let you loop through //each object in any iterable list // such as array, arraylist, linkedlist, etc for (Socket z : clientSocket) { if (z != null) { //basically this chunk of code declares output and input streams //for each socket in your array of saved sockets PrintStream outToClient = null; try { outToClient = new PrintStream(z.getOutputStream()); outToClient.println(message); } catch (IOException e) { System.out.println("Caught an IO exception trying " + "to send to TCP connections"); e.printStackTrace(); } } } } }根据您要发送消息的时间,您可以使用控制台和 sys.in 发送消息。例如,如果您从 sys.in 中读取一行并且 .equals(sendMsg),那么您可以拨打 sendToAllTCP(你的消息)
Depending on when you want to send your message, you can use the console and sys.in to send it. For example, if you read a line from sys.in and it .equals("sendMsg"), then you can call sendToAllTCP(yourmessage)
更多推荐
Java EchoTCPServer
发布评论