Java基于Socket实现简单的多线程回显服务器功能示例
程序员文章站
2024-02-25 17:05:51
本文实例讲述了java基于socket实现简单的多线程回显服务器功能。分享给大家供大家参考,具体如下:
需要两个类,一个是echoserver,代表服务器。另外一个是ec...
本文实例讲述了java基于socket实现简单的多线程回显服务器功能。分享给大家供大家参考,具体如下:
需要两个类,一个是echoserver,代表服务器。另外一个是echoserverclient,代表客户端。代码如下:
package interview; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.serversocket; import java.net.socket; public class echoserver { public static void main(string []args) throws ioexception{ serversocket server = new serversocket(6789); while(true){ socket client = server.accept(); clienthandler handler = new clienthandler(client); new thread(handler).start(); } } public static class clienthandler implements runnable{ private socket client; @override public void run() { inputstreamreader isr = null; try { isr = new inputstreamreader(client.getinputstream()); bufferedreader br = new bufferedreader(isr); printwriter pw = new printwriter(client.getoutputstream()); string msg = br.readline(); system.out.println("收到" + client.getinetaddress() + "发送的" + msg); pw.println("收到了你发的" + msg); pw.flush(); } catch (ioexception e) { e.printstacktrace(); } } public clienthandler(socket client){ this.client = client; } } }
下面是客户端代码:
package interview; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket; import java.net.unknownhostexception; import java.util.scanner; public class echoserverclient { public static void main(string []args) throws unknownhostexception, ioexception{ socket client = new socket("127.0.0.1", 6789); scanner sc = new scanner(system.in); system.out.print("请输入要发送的内容:"); string msg = sc.nextline(); sc.close(); printwriter pw = new printwriter(client.getoutputstream()); pw.println(msg); pw.flush(); inputstreamreader isr = new inputstreamreader(client.getinputstream()); bufferedreader br = new bufferedreader(isr); system.out.println("服务器返回:" + br.readline()); client.close(); } }
nio多路复用套接字方法如下:
package interview; import java.io.ioexception; import java.net.inetsocketaddress; import java.nio.bytebuffer; import java.nio.charbuffer; import java.nio.channels.selectionkey; import java.nio.channels.selector; import java.nio.channels.serversocketchannel; import java.nio.channels.socketchannel; import java.nio.charset.charactercodingexception; import java.nio.charset.charset; import java.nio.charset.charsetdecoder; import java.nio.charset.charsetencoder; import java.util.iterator; public class echoservernio { private static serversocketchannel serverchannel = null; private static selector selector = null;// 多路复用选择器 private static bytebuffer buffer = null; // 缓冲区 public static void main(string []args) throws ioexception{ init(); listen(); } static void init() throws ioexception{ serverchannel = serversocketchannel.open(); buffer = bytebuffer.allocate(1024); serverchannel.socket().bind(new inetsocketaddress(6789)); serverchannel.configureblocking(false); selector = selector.open(); serverchannel.register(selector, selectionkey.op_accept); } static void listen() throws ioexception{ while(true){ if(selector.select(5000) != 0){ iterator<selectionkey> it = selector.selectedkeys().iterator(); while(it.hasnext()){ selectionkey key = it.next(); it.remove(); handlekey(key); } } } } static void handlekey(selectionkey key) throws ioexception{ socketchannel channel = null; if(key.isacceptable()){ serversocketchannel serverchannel = (serversocketchannel)key.channel(); channel = serverchannel.accept(); channel.configureblocking(false); channel.register(selector, selectionkey.op_read); }else if(key.isreadable()){ channel = (socketchannel)key.channel(); buffer.clear(); if(channel.read(buffer) > 0){ buffer.flip(); charbuffer charbuffer = charsethelper.decode(buffer); string msg = charbuffer.tostring(); system.out.println("收到" + channel.getremoteaddress() + "的消息:" + msg); channel.write(charsethelper.encode(charbuffer.wrap("received your msg:" + msg))); } } } public static class charsethelper{ private static final string utf_8 = "utf-8"; private static charsetencoder encoder = charset.forname(utf_8).newencoder(); private static charsetdecoder decoder = charset.forname(utf_8).newdecoder(); private charsethelper() { } public static bytebuffer encode(charbuffer in) throws charactercodingexception{ return encoder.encode(in); } public static charbuffer decode(bytebuffer in) throws charactercodingexception{ return decoder.decode(in); } } }
更多关于java相关内容感兴趣的读者可查看本站专题:《java socket编程技巧总结》、《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。