一个非常简单的有界面的TCP Socket的通信程序
程序由四个类组成:一个是服务器端;一个是客户端;另外两个是类,用来发送消息和接收消息的。
服务器端TcpServer.java
package TcpChat; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.net.*; public class TcpServer { public static void main(String args[]){ //初始化界面 JFrame jf = new JFrame("服务器端"); JTextField field = new JTextField(30); JTextArea area = new JTextArea(); Cursor cursor = new Cursor(Cursor.TEXT_CURSOR); field.requestFocusInWindow(); area.setEditable(false); area.setFont(new Font("宋体",0,14)); JButton clear = new JButton("清屏"); JButton button = new JButton("发送"); Panel panel = new Panel(); panel.add(field); panel.add(button); panel.add(clear); jf.add(area,"Center"); jf.add(panel,"South"); Toolkit toolkit = jf.getToolkit(); Dimension d = toolkit.getScreenSize(); jf.setBounds(d.width/2-268,d.height/2-163,536,327); jf.setResizable(false); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.validate(); try{ //在端口2008监听 ServerSocket ss = new ServerSocket(2008); Socket s = ss.accept(); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); DataInputStream dis = new DataInputStream(is); final DataOutputStream dos = new DataOutputStream(os); SendMsg say = new SendMsg(dos,field,button,area,clear); ReceiveMsg receiver = new ReceiveMsg(dis,area); button.addActionListener(say); clear.addActionListener(say); field.addKeyListener(say); jf.addFocusListener(say); receiver.start(); }catch(IOException e){ e.printStackTrace(); } } }
客户端TcpClient.java
package TcpChat; import java.awt.*; import javax.swing.*; import java.io.*; import java.net.*; public class TcpClient { public static void main(String args[]){ //初始化界面 JFrame jf = new JFrame("客户端"); JTextField field = new JTextField(30); field.requestFocusInWindow(); JTextArea area = new JTextArea(); area.setEditable(false); area.setFont(new Font("宋体",0,14)); JButton button = new JButton("发送"); JButton clear = new JButton("清屏"); Panel panel = new Panel(); panel.add(field); panel.add(button); panel.add(clear); jf.add(area,"Center"); jf.add(panel,"South"); Toolkit toolkit = jf.getToolkit(); Dimension d = toolkit.getScreenSize(); jf.setBounds(d.width/2-268,d.height/2-163,536,327); jf.setResizable(false); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.validate(); try{ //获取服务器IP和端口号 Socket s = new Socket("127.0.0.1",2008); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); DataInputStream dis = new DataInputStream(is); final DataOutputStream dos = new DataOutputStream(os); SendMsg say = new SendMsg(dos,field,button,area,clear); ReceiveMsg receiver = new ReceiveMsg(dis,area); button.addActionListener(say); clear.addActionListener(say); field.addKeyListener(say); jf.addFocusListener(say); receiver.start(); }catch(IOException e){ e.printStackTrace(); } } }
以上两个基本相似,其差别就在于一个是监听(服务器端),一个是主动连接(客户端),见红色代码。
下面两个是公共类:
发送消息SendMsg.java
package TcpChat; import java.awt.Cursor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JTextArea; import javax.swing.JTextField; import java.io.DataOutputStream; import java.io.IOException; /** * 发送 * */ public class SendMsg extends KeyAdapter implements ActionListener,FocusListener{ private JTextField field; private JTextArea area; private DataOutputStream dos; private JButton button; private JButton clear; public SendMsg(DataOutputStream d,JTextField f,JButton b,JTextArea a,JButton c){ this.dos = d; this.field = f; this.button = b; this.area = a; this.clear = c; } @Override public void keyTyped(KeyEvent e) { if(e.getKeyChar()==(char)KeyEvent.VK_ENTER){ String s = field.getText(); try{ area.append("你说:"+s+"\n"); dos.writeUTF(s+"\n"); if(s.equals("bye")){ dos.close(); area.setText("自己下线,程序退出!"); System.exit(0); } field.setText(""); }catch(IOException ee){ ee.printStackTrace(); } } } public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("发送")){ String s = field.getText(); try{ area.append("你说:"+s+"\n"); dos.writeUTF(s+"\n"); if(s.equals("bye")){ dos.close(); area.setText("自己下线,程序退出!"); System.exit(0); } field.setText(""); }catch(IOException ee){ ee.printStackTrace(); } } if(e.getActionCommand().equals("清屏")){ area.setText(""); } } public void focusGained(FocusEvent e) { Cursor cursor = new Cursor(Cursor.TEXT_CURSOR); field.requestFocusInWindow(); } public void focusLost(FocusEvent e) {} }
接收消息ReceiveMsg.java
package TcpChat; import java.io.DataInputStream; import java.io.IOException; import javax.swing.JTextArea; /** * 接收 * */ public class ReceiveMsg extends Thread{ private DataInputStream dis; private JTextArea area; public ReceiveMsg(DataInputStream d,JTextArea a){ this.dis = d; this.area = a; } public void run(){ String s; while(true){ try{ s = dis.readUTF(); area.append("对方说:"+s); if(s.equals("bye")){ dis.close(); area.setText("对方下线,程序退出!"); break; } }catch(IOException e){ e.printStackTrace(); } } } }
该程序只具有简单的聊天功能,主要是为了分析socket 通信中服务器和客户端的差异,未考虑关闭程序时关闭端口等因素。附上Eclipse3.4下高度的项目~下载后,直接导入即可运行~