Java网络编程实现多线程聊天
程序员文章站
2022-03-08 18:52:27
本文实例为大家分享了java网络编程实现多线程聊天的具体代码,供大家参考,具体内容如下聊天程序如果是单线程,会导致没人只能说一句,并且说了以后,必须等到另一个人的回复,才能说第二句。收发都在主线程中,...
本文实例为大家分享了java网络编程实现多线程聊天的具体代码,供大家参考,具体内容如下
聊天程序如果是单线程,会导致没人只能说一句,并且说了以后,必须等到另一个人的回复,才能说第二句。收发都在主线程中,不能同时进行。
解决方法:
将收发放到两个不同的线程
1. sendthread 发送消息线程
2. recievethread 接受消息线程
3. server一旦接受到连接,就启动收发两个线程
4. client 一旦建立了连接,就启动收发两个线程
多线程聊天
1 sendthread
package socket; import java.io.dataoutputstream; import java.io.ioexception; import java.io.outputstream; import java.net.socket; import java.util.scanner; public class sendthread extends thread { private socket s; public sendthread(socket s) { this.s = s; } @override public void run() { try { outputstream os = s.getoutputstream(); dataoutputstream dos = new dataoutputstream(os); while(true){ scanner sc = new scanner(system.in); string str = sc.next(); dos.writeutf(str); } } catch (ioexception e) { e.printstacktrace(); } } }
2 recievethread
package socket; import java.io.datainputstream; import java.io.ioexception; import java.io.inputstream; import java.net.socket; public class recievethread extends thread { private socket s; public recievethread(socket s) { this.s = s; } @override public void run() { inputstream is = null; try { is = s.getinputstream(); datainputstream dis = new datainputstream(is); while(true){ string msg = dis.readutf(); system.out.println(msg); } } catch (ioexception e) { e.printstacktrace(); } } }
3 server
package socket; import java.io.ioexception; import java.net.serversocket; import java.net.socket; public class server { public static void main(string[] args) { try { serversocket ss = new serversocket(8888); system.out.println("监听端口号:8888"); socket s = ss.accept(); new sendthread(s).start(); new recievethread(s).start(); } catch (ioexception e) { e.printstacktrace(); } } }
4 client
package socket; import java.io.ioexception; import java.net.socket; import java.net.unknownhostexception; public class client { public static void main(string[] args) { try { socket s = new socket("127.0.0.1", 8888); new sendthread(s).start(); new recievethread(s).start(); } catch (unknownhostexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } }
简单对话框
server
package socket; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.datainputstream; import java.io.dataoutputstream; import java.net.serversocket; import java.net.socket; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextarea; import javax.swing.jtextfield; public class guiserver { public static void main(string[] args) throws exception { jframe f = new jframe(); f.settitle("server"); f.setsize(400, 300); f.setlocation(100, 200); f.setlayout(null); jbutton b = new jbutton("send"); b.setbounds(10, 10, 80, 30); f.add(b); final jtextfield tf = new jtextfield(); tf.setbounds(10, 110, 80, 30); f.add(tf); final jtextarea ta = new jtextarea(); ta.setbounds(110,10, 200, 300); f.add(ta); f.setdefaultcloseoperation(jframe.exit_on_close); f.setvisible(true); serversocket ss = new serversocket(8888); system.out.println("listenning on port:8888"); final socket s = ss.accept(); new thread() { public void run() { while (true) { try { datainputstream dis = new datainputstream( s.getinputstream()); string text = dis.readutf(); ta.append(text+"\r\n"); } catch (exception e) { e.printstacktrace(); } } } }.start(); b.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string text = tf.gettext(); ta.append(text+"\r\n"); try { dataoutputstream dos = new dataoutputstream( s.getoutputstream()); dos.writeutf(text); } catch (exception ex) { ex.printstacktrace(); } } }); } }
client
package socket; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.datainputstream; import java.io.dataoutputstream; import java.net.socket; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtextarea; import javax.swing.jtextfield; public class guiclient { public static void main(string[] args) throws exception { jframe f = new jframe(); f.settitle("client"); f.setsize(400, 300); f.setlocation(600, 200); f.setlayout(null); jbutton b = new jbutton("send"); b.setbounds(10, 10, 80, 30); f.add(b); final jtextfield tf = new jtextfield(); tf.setbounds(10, 110, 80, 30); f.add(tf); final jtextarea ta = new jtextarea(); ta.setbounds(110,10, 200, 300); f.add(ta); f.setdefaultcloseoperation(jframe.exit_on_close); f.setvisible(true); final socket s = new socket("127.0.0.1", 8888); new thread() { public void run() { while (true) { try { datainputstream dis = new datainputstream( s.getinputstream()); string text = dis.readutf(); ta.append(text+"\r\n"); } catch (exception e) { e.printstacktrace(); } } } }.start(); b.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string text = tf.gettext(); ta.append(text+"\r\n"); try { dataoutputstream dos = new dataoutputstream( s.getoutputstream()); dos.writeutf(text); } catch (exception ex) { ex.printstacktrace(); } } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C++实现聊天程序
下一篇: Java网络编程TCP实现文件上传功能