import javaioBufferedReader;import javaioIOException;import javaioInputStreamReader;import javaioPrintWriter;import javanetServerSocket;import javanetSocket;import javatextSimpleDateFormat;import javautilArrayList;import javautilDate;import javautilList; / 4用socket通讯写出多个客户端和一个服务器端的通讯, 要求客户发送数据后能够回显相同的数据(回显功能)(实用TCP方式)。 /public class Test4Server { // 主入口 public static void main(String[] args) throws IOException { scoketServer(); } // 开启的tcp8888监听端口 public static void scoketServer() throws IOException { ServerSocket server = new ServerSocket(8888); while (true) { // 未连通前线程阻塞,连通后开启一个socket通道线程后继续监听8888端口 Socket socket = serveraccept(); Systemoutprintln(socketgetInetAddress()getHostAddress() + "连接进入"); new SocketThread(socket)start(); } } } // 一个服务器端口中监听多个客服端通道线程class SocketThread extends Thread {// 所有通道写入流的集合 private static List<PrintWriter> list =new ArrayList<PrintWriter>(); private BufferedReader bufferedReader; private PrintWriter printWriter; public SocketThread(Socket socket) throws IOException { thisbufferedReader = new BufferedReader(new InputStreamReader(socket getInputStream())); thisprintWriter = new PrintWriter(socketgetOutputStream()); listadd(printWriter); } @Override public void run() { String string = null; while (true) { try { // 服务器在通道中读到的信息回显给客服端 string = bufferedReaderreadLine(); Systemoutprintln("客服端信息:" + string); for(PrintWriter printWriter:list ){ printWriterwrite("服务器回显:" + string + "\r\n"); printWriterflush(); } } catch (IOException e) { } } }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)