基于TCP的多人聊天小程序

要回家了,心就散了,毕业论文也做不下去了。闲着弄了弄Socket。同时遇到了一些问题。

服务端ChatServer.java

None.gif package  com.neusoft.socket.chat;
None.gif
None.gif
import  java.io. * ;
None.gif
import  java.net. * ;
None.gif
import  java.util. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  ChatServer  dot.gif {
InBlock.gif    
// 存储不同客户端的线程
InBlock.gif
    public static Hashtable peopleList;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String args[]) dot.gif{
InBlock.gif        ServerSocket server 
= null;
InBlock.gif        Socket socket 
= null;
InBlock.gif
InBlock.gif        peopleList 
= new Hashtable();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (truedot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
// 建立服务端
InBlock.gif
                server = new ServerSocket(8099);
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (IOException e) dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
// 开始监听
InBlock.gif
                socket = server.accept();
InBlock.gif                InetAddress address 
= socket.getInetAddress();
InBlock.gif                System.out.println(
"IP:" + address + " come in.");
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (IOException e) dot.gif{
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (socket != nulldot.gif{
InBlock.gif                
// 开启一条线程通讯
InBlock.gif
                new Server_thread(socket).start();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 else dot.gif{
InBlock.gif                
continue;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

服务端线程Server_thread.java
None.gif package  com.neusoft.socket.chat;
None.gif
None.gif
import  java.io.IOException;
None.gif
import  java.io.ObjectInputStream;
None.gif
import  java.io.ObjectOutputStream;
None.gif
import  java.net.Socket;
None.gif
import  java.util.Enumeration;
None.gif
import  java.util.Hashtable;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Server_thread  extends  Thread  dot.gif {
InBlock.gif    
// 客户端机器名
InBlock.gif
    String name = null;
InBlock.gif
InBlock.gif    String keyName 
= null;
InBlock.gif
InBlock.gif    String message 
= null;
InBlock.gif
InBlock.gif    Socket socket 
= null;
InBlock.gif
InBlock.gif    ObjectOutputStream out 
= null;
InBlock.gif
InBlock.gif    ObjectInputStream in 
= null;
InBlock.gif
InBlock.gif    Hashtable peopleList 
= null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Server_thread(Socket t) dot.gif{
InBlock.gif        socket 
= t;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            
// 取得输入输出流对象
InBlock.gif
            in = new ObjectInputStream(socket.getInputStream());
InBlock.gif            out 
= new ObjectOutputStream(socket.getOutputStream());
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (IOException e) dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void run() dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while (truedot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif{
InBlock.gif                
// 读取客户端消息
InBlock.gif
                message = (String) in.readObject();
InBlock.gif                
// 把客户端线程存储到HASHTABLE
InBlock.gif
                name = socket.getInetAddress().toString();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (!ChatServer.peopleList.containsKey(name)) dot.gif{
InBlock.gif                    ChatServer.peopleList.put(name, 
this);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
// 遍历HASHTABLE,把消息发送到每一个客户端,实现多人聊天
InBlock.gif
                Enumeration enum1 = ChatServer.peopleList.keys();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
while (enum1.hasMoreElements()) dot.gif{
InBlock.gif                    keyName 
= (String) enum1.nextElement();
InBlock.gif                    ((Server_thread) ChatServer.peopleList.get(keyName)).out
InBlock.gif                            .writeObject(keyName 
+ "says:" + message);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (ClassNotFoundException e) dot.gif{
InBlock.gif                e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (IOException ee) dot.gif{
InBlock.gif                ee.printStackTrace();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

客户端:Client.java
None.gif package  com.neusoft.socket.chat;
None.gif
None.gif
import  java.awt. * ;
None.gif
import  java.awt.event. * ;
None.gif
import  java.io. * ;
None.gif
import  java.net. * ;
None.gif
import  javax.swing. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Client  extends  JFrame  dot.gif {
InBlock.gif    
private Container container;
InBlock.gif
InBlock.gif    
private JTextField enterField;
InBlock.gif
InBlock.gif    
private JTextArea displayArea;
InBlock.gif
InBlock.gif    
private ObjectOutputStream output;
InBlock.gif
InBlock.gif    
private ObjectInputStream input;
InBlock.gif
InBlock.gif    
private String message = "";
InBlock.gif
InBlock.gif    
private String chatServer;
InBlock.gif
InBlock.gif    
private Socket client;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Client(String host) dot.gif{
InBlock.gif        
//初始化聊天窗口
InBlock.gif
        super("my Client");
InBlock.gif        chatServer 
= host;
InBlock.gif        container 
= getContentPane();
InBlock.gif        enterField 
= new JTextField();
InBlock.gif        enterField.setEnabled(
false);
ExpandedSubBlockStart.gifContractedSubBlock.gif        enterField.addActionListener(
new ActionListener() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public void actionPerformed(ActionEvent event) dot.gif{
InBlock.gif                sendData(event.getActionCommand());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
);
InBlock.gif        container.add(enterField, BorderLayout.SOUTH);
InBlock.gif        displayArea 
= new JTextArea();
InBlock.gif        container.add(
new JScrollPane(displayArea), BorderLayout.CENTER);
InBlock.gif        setSize(
500300);
InBlock.gif        setLocation(
200300);
InBlock.gif        setVisible(
true);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// 执行客户端
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public void runClient() dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
InBlock.gif            connectToServer();
InBlock.gif            getStreams();
InBlock.gif            processConnection();
InBlock.gif            closeConnection();
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (EOFException eofException) dot.gif{
InBlock.gif            System.out.println(
"Server
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值