Java使用Socket简单通讯详解
程序员文章站
2022-03-16 20:42:48
目录java实现基于socket的简单通信 一.serversocket1.使用javafx写的小界面,方便观察客户端连接情况2.创建serversocket并处理客户端连接并显示客户端基本...
java实现基于socket的简单通信
一.serversocket
1.使用javafx写的小界面,方便观察客户端连接情况
textarea ta = new textarea(); scene scene = new scene(new javafx.scene.control.scrollpane(ta), 450, 200); primarystage.settitle("server"); primarystage.setscene(scene); primarystage.show();
2.创建serversocket并处理客户端连接并显示客户端基本信息
两个客户端连接后进行通信,未能实现动态处理。
new thread(() -> { try { serversocket serversocket = new serversocket(8000); platform.runlater(() -> { ta.appendtext(new date() + " : server started at " + "\n"); ta.appendtext(new date() + " : wait to persons to join the chat" + "\n"); }); while (true){ socket person1 = serversocket.accept(); number++; inetaddress inetaddress1 = person1.getinetaddress(); platform.runlater(() -> { ta.appendtext(new date() + ": person" + number + "joined the chat" + "\n"); ta.appendtext(new date() + ": person" + number + "'s host name is " + inetaddress1.gethostname() + "\n"); ta.appendtext(new date() + ": person" + number + "'s host address is " + inetaddress1.gethostaddress() + "\n"); ta.appendtext(new date() + ": wait for person2 " + "\n"); }); socket person2 = serversocket.accept(); number++; inetaddress inetaddress2 = person2.getinetaddress(); platform.runlater(() -> { ta.appendtext(new date() + ": person" + number + "joined the chat" + "\n"); ta.appendtext(new date() + ": person" + number + "'s host name is " + inetaddress2.gethostname() + "\n"); ta.appendtext(new date() + ": person" + number + "'s host address is " + inetaddress2.gethostaddress() + "\n"); ta.appendtext(new date() + ": start the chat " + "\n"); }); new thread(new handlechat(person1,person2)).start(); try { thread.sleep(100); } catch (interruptedexception e) { e.printstacktrace(); } } } catch (ioexception e) { e.printstacktrace(); } }).start();
3.新建handle类处理接收发送两个客户端的消息
只实现了一人一句的效果,没有实现一人连续发送消息对方也能正确接收。
class handlechat implements runnable{ socket person1; socket person2; public handlechat(socket person1,socket person2){ this.person1 = person1; this.person2 = person2; } @override public void run() { try { datainputstream fromperson1 = new datainputstream(person1.getinputstream()); dataoutputstream toperson1 = new dataoutputstream(person1.getoutputstream()); datainputstream fromperson2 = new datainputstream(person2.getinputstream()); dataoutputstream toperson2 = new dataoutputstream(person2.getoutputstream()); while (true){ string passage1 = fromperson1.readutf(); toperson2.writeutf(passage1); string passage2 = fromperson2.readutf(); toperson1.writeutf(passage2); } } catch (ioexception e) { e.printstacktrace(); } } }
思考后将handlechat类中对两个客户端的接收发送消息处理放在两个线程中,就可以实现两个客户端*通信。
new thread(() -> { while (true) { try { string passage2 = fromperson2.readutf(); toperson1.writeutf(passage2); } catch (ioexception e) { e.printstacktrace(); } } }).start(); new thread(() -> { while (true) { try { string passage1 = fromperson1.readutf(); toperson2.writeutf(passage1); } catch (ioexception e) { e.printstacktrace(); } } }).start();
二.socket
1.同样的编写一个客户端界面
borderpane pane = new borderpane(); pane.setpadding(new insets(5)); pane.setstyle("-fx-border-color: green"); pane.setleft(new label("enter a radius: ")); textfield tf = new textfield(); tf.setalignment(pos.baseline_right); pane.setcenter(tf); borderpane mainpane = new borderpane(); textarea ta = new textarea(); mainpane.setcenter(new scrollpane(ta)); mainpane.settop(pane); scene scene = new scene(mainpane,450,200); primarystage.settitle("client"); primarystage.setscene(scene); primarystage.show();
2.创建socket连接客户端并获取输入输出流
try { // 创建一个连接服务器端的socket socket socket = new socket("localhost",8000); // 获得输入输出流 toserver = new dataoutputstream(socket.getoutputstream()); fromserver = new datainputstream(socket.getinputstream()); } catch (ioexception e) { e.printstacktrace(); }
3.添加编辑框监听处理消息发送
// 编辑框事件监听 tf.setonaction(e ->{ string passage = tf.gettext().trim(); tf.clear(); try { toserver.writeutf(passage); toserver.flush(); ta.appendtext("me " + ": " + passage + "\n"); } catch (ioexception e1) { e1.printstacktrace(); } });
4.创建新线程从服务器上接收消息
// 新线程从服务器读取信息 new thread(() -> { while (true) { try { string passage = fromserver.readutf(); ta.appendtext("he : " + passage + "\n"); } catch (ioexception e) { e.printstacktrace(); } } }).start();
三.测试
四.总结
1.原理流程
2.不足之处
只是简单实现了静态两客户端聊天的功能,并且只能够第一个链接上的用户先发送消息,且一人发送消息后只能等待接收另一个人的消息后才能再次发送消息。之后的时间希望能加以改进。
到此这篇关于java使用socket简单通讯详解的文章就介绍到这了,更多相关java使用socket简单通讯内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!