基于java socket实现 聊天小程序
服务器
packagedemo04; importjava.awt.BorderLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.event.WindowAdapter; importjava.awt.event.WindowEvent; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.net.ServerSocket; importjava.net.Socket; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JOptionPane; importjavax.swing.JPanel; importjavax.swing.JScrollPane; importjavax.swing.JTextArea; importjavax.swing.JTextField; importjavax.swing.ScrollPaneConstants; publicclassownServer{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub newServer().launch(); } } classServer{ JFrameframe; JTextAreataArea; JTextFieldtfField; JButtonsbButton; DataInputStreamread; DataOutputStreamwrite; ServerSocketscServerSocket; Socketsocket; publicvoidlaunch(){ CreateUI(); CreateNetWork(); newServerWrite().start(); newServerRead().start(); } publicvoidCreateUI(){ frame=newJFrame("MMServer"); JPanelPanle=newJPanel(); taArea=newJTextArea(15,25); taArea.setLineWrap(true); taArea.setWrapStyleWord(true); taArea.setEditable(true); JScrollPaneqScroller=newJScrollPane(taArea); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); tfField=newJTextField(20); sbButton=newJButton("发送"); Panle.add(qScroller); Panle.add(tfField); Panle.add(sbButton); frame.getContentPane().add(BorderLayout.CENTER,Panle); frame.setSize(400,400); frame.setVisible(true); } publicvoidclose() { try { write.close(); read.close(); socket.close(); scServerSocket.close(); } catch(Exceptione) { System.exit(-1); } } publicvoidCreateNetWork(){ try{ scServerSocket=newServerSocket(7780); socket=scServerSocket.accept(); read=newDataInputStream(socket.getInputStream()); write=newDataOutputStream(socket.getOutputStream()); System.out.print("网络连接"); }catch(Exceptione){ //TODO:handleexception e.printStackTrace(); } } classServerReadextendsThread{ publicvoidrun(){ while(true){ try{ Stringmessage=read.readUTF(); taArea.append("对方:"+message+"\n"); }catch(Exceptione1){ //TODO:handleexception JOptionPane.showMessageDialog(taArea,"提示:服务端已经断开连接"); //e.printStackTrace(); //System.exit(-1); return; } } } } classServerWriteextendsThread{ publicvoidrun(){ tfField.addActionListener(newServerListen()); sbButton.addActionListener(newServerListen()); } } classServerListenimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ try{ Stringstr=tfField.getText(); tfField.setText(""); taArea.append("我:"+str+"\n"); write.writeUTF(str); }catch(Exceptione2){ //TODO:handleexception e2.printStackTrace(); } } } }
客户端
packagedemo04; importjava.awt.BorderLayout; importjava.awt.event.ActionEvent; importjava.awt.event.ActionListener; importjava.awt.event.WindowAdapter; importjava.awt.event.WindowEvent; importjava.io.DataInputStream; importjava.io.DataOutputStream; importjava.net.Socket; importjavax.swing.JButton; importjavax.swing.JFrame; importjavax.swing.JOptionPane; importjavax.swing.JPanel; importjavax.swing.JScrollPane; importjavax.swing.JTextArea; importjavax.swing.JTextField; importjavax.swing.ScrollPaneConstants; publicclassownClient{ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub newClient().launch(); } } classClient{ JTextAreataArea; JTextFieldtfField; JButtonsbButton; DataInputStreamread=null; DataOutputStreamwrite=null; Socketsocket; publicvoidlaunch(){ CreateUI(); CreatNetWork(); newClientRead().start();; newClientWrite().start();; } publicvoidCreateUI(){ JFrameframe=newJFrame("MMClient"); JPanelPanle=newJPanel(); taArea=newJTextArea(15,25); taArea.setLineWrap(true); taArea.setWrapStyleWord(true); taArea.setEditable(true); JScrollPaneqScroller=newJScrollPane(taArea); qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); tfField=newJTextField(20); sbButton=newJButton("发送"); Panle.add(qScroller); Panle.add(tfField); Panle.add(sbButton); frame.getContentPane().add(BorderLayout.CENTER,Panle); frame.setSize(400,400); frame.setVisible(true); } publicvoidCreatNetWork(){ //TODOAuto-generatedmethodstub try{ socket=newSocket("127.0.0.1",7780); write=newDataOutputStream(socket.getOutputStream()); read=newDataInputStream(socket.getInputStream()); System.out.print("已建立网络连接"); }catch(Exceptione){ //TODO:handleexception e.printStackTrace(); } } classClientReadextendsThread{ publicvoidrun(){ while(true){ try{ Stringmessage=read.readUTF(); taArea.append("对方:"+message+"\n"); }catch(Exceptione1){ //TODO:handleexception JOptionPane.showMessageDialog(taArea,"提示:客户端已经断开连接"); //e.printStackTrace(); //System.exit(-1); return; } } } } classClientWriteextendsThread{ publicvoidrun(){ tfField.addActionListener(newClientListen()); sbButton.addActionListener(newClientListen()); } } classClientListenimplementsActionListener{ @Override publicvoidactionPerformed(ActionEvente){ try{ Stringstr=tfField.getText(); tfField.setText(""); taArea.append("我:"+str+"\n"); write.writeUTF(str); }catch(Exceptione2){ //TODO:handleexception e2.printStackTrace(); } } } }
总结
以上所述是小编给大家介绍的基于javasocket实现聊天小程序,希望对大家有所帮助!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。