符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
本文小编为大家详细介绍“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(); } } }); } }
读到这里,这篇“Java网络编程如何实现多线程聊天”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注创新互联行业资讯频道。