本文实例讲述了Java编程实现基于TCP协议的Socket聊天室。分享给大家供大家参考,具体如下:
这里使用Socket套接字进行编程,完成的是基于TCP可靠服务实现服务器与客户端的双通信。
Server服务器端:
package com.han;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.*;import java.net.*;import javax.swing.JDialog;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;/** * 本程序实现了一个TCP程序的服务器编程部分。 * 使用Socket套接字进行编程,完成的是基于TCP可靠服务实现与客户端的双通信。 * 客户端的编程见本包中的类Client * @author HAN * */@SuppressWarnings("serial")public class Server extends JDialog{ private BufferedReader reader; private PrintWriter writer; private ServerSocket server; private Socket socket; private JTextArea ta=new JTextArea(); private JScrollPane sp=new JScrollPane(ta); private JTextField tf=new JTextField(); Container cc; public Server(String title) { setTitle(title); addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); //撤销dialog一切相关资源 System.exit(0); //正常退出程序 } }); cc=getContentPane(); setLayout(null); ta.setLineWrap(true); ta.setEditable(false); sp.setBounds(0,0,300,342); tf.setBounds(0,342,300,25); cc.add(sp); cc.add(tf); tf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ try { writer=new PrintWriter(socket.getOutputStream(),true); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } writer.println(tf.getText()); ta.append("User1:"+tf.getText()+'/n'); tf.setText(""); } }); } void getserver(){ try{ server=new ServerSocket(8998); ta.append("服务器套接字已经创建成功/n"); while(true){ ta.append("等待客户机的连接/n"); socket=server.accept(); ta.append("客户机已连接/n"); reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); getClientMessage(); } }catch(Exception e){ e.printStackTrace(); } } private void getClientMessage(){ try { while(true){ String news=reader.readLine(); if(news!=null){ ta.append("User2:"+news+"/n"); }else{ ta.append("User2(客户端) 已断开链接/n"); break; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ if(reader!=null){ reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Server user1=new Server("User1"); user1.setBounds(0,0,300,400); user1.setResizable(false); user1.setVisible(true); user1.getserver(); }}
Client客户端:
package com.han;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import javax.swing.*;/** * 本程序实现了一个TCP程序的客户端编程部分。 * 使用Socket套接字进行编程,完成的是基于TCP可靠服务实现与服务器的双通信。 * 服务器的编程见本包中的类Server * 可以在不同的平台与不同的机器上运行,只是要把代码中写的IP地址修改为运行服务器程序Server的机器的IP地址。 * @author HAN * */@SuppressWarnings("serial")public class Client extends JDialog{ private BufferedReader reader; private PrintWriter writer; private Socket socket; private JTextArea ta=new JTextArea(); private JScrollPane sp=new JScrollPane(ta); private JTextField tf=new JTextField(); Container cc; public Client(String title) { setTitle(title); addWindowListener(new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); //撤销dialog一切相关资源 System.exit(0); //正常退出程序 } }); cc=getContentPane(); setLayout(null); ta.setLineWrap(true); ta.setEditable(false); sp.setBounds(0,0,300,342); tf.setBounds(0,342,300,25); cc.add(sp); cc.add(tf); tf.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ writer.println(tf.getText()); ta.append("User2:"+tf.getText()+'/n'); tf.setText(""); } }); } private void connect(){ ta.append("尝试连接/n"); try { socket=new Socket("192.168.1.3",8998); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { writer=new PrintWriter(socket.getOutputStream(),true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ta.append("完成连接/n"); } private void getClientMessage(){ try { reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); while(true){ String news=reader.readLine(); if(news!=null){ ta.append("User1:"+news+"/n"); }else{ ta.append("User1(服务器) 已断开链接,等待服务器重连之时,重启User2(客户端)进行通信/n"); break; } } } catch (IOException e) { // TODO Auto-generated catch block ta.append("User1(服务器) 已断开链接,等待服务器重连之时,重启User2(客户端)进行通信/n"); e.printStackTrace(); } try{ if(reader!=null){ reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(socket!=null){ try { socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { Client user2=new Client("User2"); user2.setBounds(0,0,300,400); user2.setVisible(true); user2.setResizable(false); user2.connect(); user2.getClientMessage(); }}
希望本文所述对大家java程序设计有所帮助。
新闻热点
疑难解答
图片精选