您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 广告经营 > 网络实验6Socket编程实现聊天程序
1南昌航空大学实验报告课程名称:计算机网络实验名称:Socket编程实现聊天程序班级:090451班学生姓名:杨望学号:09045131指导教师评定:签名:一、实验目的:1、掌握网络应用程序的开发方法;2、掌握Client/Server结构软件的设计与开发方法3、掌握Socket机制的工作原理。二、实验题目:使用Win32Socket函数实现聊天程序:能相互对发文本消息。三、服务端与客户端连接工作流程图:四、收发消息工作流程图:开始监听有新客户端请求连接服务器提示错误信息不允许建立连接允许允许连接?更新用户信息表2五、实验代码:1、服务器端:packageyuchen;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintStream;importjava.net.ServerSocket;importjava.net.Socket;importjava.util.StringTokenizer;importjava.util.Vector;publicclassChatServer{staticintport=5566;//端口号staticVectorClientclients=newVectorClient(10);//存储连接客户信息staticServerSocketserver=null;//建立服务器socketstaticSocketsocket=null;//套接字连接publicChatServer(){try{System.out.println(Serverstart...);server=newServerSocket(port);//初始化服务器套接字while(true){socket=server.accept();//等待连接System.out.println(socket.getInetAddress()+连接\n);//得到客户机地址Clientclient=newClient(socket);监听发送给客户端B是用户B在上线?用户B在忙碌?用户B在隐身?是是用户B在离线?发送给服务器否向客户端B发送消息3clients.add(client);//增加客户线程到向量中client.start();//启动线程notifyChatRoom();//监视聊天室连接变化}}catch(Exceptionex){ex.printStackTrace();//输出出错信息}}publicstaticvoidnotifyChatRoom(){//监视客户端线程StringBuffernewUser=newStringBuffer(newUser);for(inti=0;iclients.size();i++){Clientc=(Client)clients.elementAt(i);newUser.append(:+c.name);//客户端姓名字符串}sendClients(newUser);//发送信息到客户端}publicstaticvoidsendClients(StringBuffermessage){for(inti=0;iclients.size();i++){Clientclient=(Client)clients.elementAt(i);//分别得到每个客户端的连接client.send(message);//发送信息}}publicvoidcloseAll(){//关闭所有连接while(clients.size()0){//遍历整个VectorClientclient=(Client)clients.firstElement();//得到一个客户端try{client.socket.close();}catch(IOExceptionex){ex.printStackTrace();//输出错误信息}clients.removeElement(client);//移出客户端}}publicstaticvoiddisconnect(Clientc){//断开客户端try{System.err.println(c.ip+断开连接\n);}catch(Exceptionex){ex.printStackTrace();}clients.removeElement(c);c.socket=null;}publicstaticvoidmain(String[]args){newChatServer();}classClientextendsThread{Socketsocket;//连接端口Stringname;//用户姓名Stringip;//客户端ip地址BufferedReaderreader;//输入流PrintStreamps;//输出流publicClient(Sockets){socket=s;try{reader=newBufferedReader(newInputStreamReader(s.getInputStream()));ps=newPrintStream(s.getOutputStream());//得到输出流Stringinfo=reader.readLine();//读取接收到的信息StringTokenizerstinfo=newStringTokenizer(info,:);//分解字符串Stringhead=stinfo.nextToken();//获取关键字System.out.println(stinfo.toString());System.out.println(head);4if(stinfo.hasMoreTokens()){name=stinfo.nextToken();//获取用户名}if(stinfo.hasMoreTokens()){ip=stinfo.nextToken();//获取IP地址}}catch(IOExceptionex){ex.printStackTrace();}System.out.println(name);System.out.println(ip);}publicvoidsend(StringBuffermsg){ps.println(msg);//输出信息ps.flush();}publicvoidrun(){while(true){Stringline=null;try{line=reader.readLine();System.out.println(line:+line);}catch(IOExceptionex){ex.printStackTrace();//输出错误信息ChatServer.disconnect(this);//断开连接ChatServer.notifyChatRoom();//更新信息return;}if(line==null){//客户离开ChatServer.disconnect(this);ChatServer.notifyChatRoom();return;}StringTokenizerst=newStringTokenizer(line,:);//分解字符串Stringkeyword=st.nextToken();if(keyword.equals(MSG)){//发送来的聊天信息StringBuffermsg=newStringBuffer(MSG:);msg.append(name);//在信息上增加用户名msg.append(st.nextToken(\0\n));ChatServer.sendClients(msg);//发送聊天语句到各个客户端System.out.println(msg);}elseif(keyword.equals(quit)){//退出命令ChatServer.disconnect(this);//断开连接ChatServer.notifyChatRoom();//刷新信息}}}}}2、客户端:packageyuchen;importjava.awt.BorderLayout;importjava.awt.Button;importjava.awt.Color;importjava.awt.Label;importjava.awt.TextArea;importjava.awt.TextField;importjava.awt.event.ActionEvent;5importjava.awt.event.ActionListener;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintStream;importjava.net.InetAddress;importjava.net.Socket;importjava.util.StringTokenizer;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPanel;publicclassChatClientextendsJFrameimplementsActionListener,RunnableTextFieldtfName=newTextField(15);//姓名输入文本域ButtonbtConnect=newButton(连接);//连接按钮ButtonbtDisconnect=newButton(断开连接);//断开连接按钮TextAreatfChat=newTextArea(8,27);//显示聊天信息文本域ButtonbtSend=newButton(发送);TextFieldtfMessage=newTextField(30);//聊天输入java.awt.Listlist1=newjava.awt.List(9);//显示在线用户信息Socketsocket=null;//连接端口PrintStreamps=null;//输出流Listenlisten=null;classListenextendsThread{BufferedReaderreader;PrintStreamps;Stringcname;Socketsocket;ChatClientchatClient;publicListen(ChatClientclient,Stringname,Socketsocket){try{this.chatClient=client;this.socket=socket;this.cname=name;reader=newBufferedReader(newInputStreamReader(socket.getInputStream()));ps=newPrintStream(socket.getOutputStream());}catch(IOExceptione){e.printStackTrace();}}publicvoidrun(){while(true){Stringline=null;try{line=reader.readLine();//读取数据流System.out.println(客户端:+line);}catch(IOExceptionex){ex.printStackTrace();ps.println(quit);;//断开连接6return;}StringTokenizerstinfo=newStringTokenizer(line,:);//分解字符串Stringkeyword=stinfo.nextToken();if(keyword.equals(MSG)){chatClient.
本文标题:网络实验6Socket编程实现聊天程序
链接地址:https://www.777doc.com/doc-5648172 .html