惠州学院杨洪英网络通信编程实验报告

上传人:第*** 文档编号:57514827 上传时间:2018-10-22 格式:PDF 页数:28 大小:359.01KB
返回 下载 相关 举报
惠州学院杨洪英网络通信编程实验报告_第1页
第1页 / 共28页
惠州学院杨洪英网络通信编程实验报告_第2页
第2页 / 共28页
惠州学院杨洪英网络通信编程实验报告_第3页
第3页 / 共28页
惠州学院杨洪英网络通信编程实验报告_第4页
第4页 / 共28页
惠州学院杨洪英网络通信编程实验报告_第5页
第5页 / 共28页
点击查看更多>>
资源描述

《惠州学院杨洪英网络通信编程实验报告》由会员分享,可在线阅读,更多相关《惠州学院杨洪英网络通信编程实验报告(28页珍藏版)》请在金锄头文库上搜索。

1、网络通信编程课程论文1、面向面向 socket 编程编程1.1.实验目的:1.1.1理解 SOCKET 的基本原理; 1.1.2学会面向 SOCKET 编程的代码编写及理解各语句内容; 1.1.3掌握面向 SOCKET 编程的具体应用。1.2.实验要求:1.2.1建立两个独立的面向 SOCKET 编程的 JAVA 工程,用于服务器和客户端系 统; 1.2.2建立 SOCKET 的 JAVA 类,并能正确运行且能实现 SOCKET 通信;1.2.3对 JAVA 类的功能进行拓广,使 SOCKET 用于某一具体的应用。1.3.实验内容1.3.1.建立服务器端工程和三个 JAVA 类,类程序的原代码

2、为: import java.io.*; import .*; import java.util.*; public class ChatServer public static void main(String args) ServerSocket server=null; Socket you=null; Hashtable peopleList; peopleList=new Hashtable(); while(true) try server=new ServerSocket(6666); catch(IOException e1) System.out.println(“正在监听“

3、); try you=server.accept(); InetAddress address=you.getInetAddress(); System.out.println(“用户的 IP:“+address); catch (IOException e) if(you!=null) Server_threadpeopleThread=new Server_thread(you,peopleList); peopleThread.start(); else continue; class Server_thread extends Thread String name=null,sex=n

4、ull; Socket socket=null; File file=null; DataOutputStream out=null; DataInputStreamin=null; Hashtable peopleList=null; Server_thread(Socket t,Hashtable list) peopleList=list; socket=t; try in=new DataInputStream(socket.getInputStream(); out=new DataOutputStream(socket.getOutputStream(); catch (IOExc

5、eption e) public void run()while(true) String s=null; try s=in.readUTF(); if(s.startsWith(“姓名:“) name=s.substring(s.indexOf(“:“)+1,s.indexOf(“性别“); sex=s.substring(s.lastIndexOf(“:“)+1);boolean boo=peopleList.containsKey(name); if(boo=false) peopleList.put(name,this); out.writeUTF(“可以聊天:“); Enumerat

6、ion enu=peopleList.elements(); while(enu.hasMoreElements() Server_thread th=(Server_thread)enu.nextElement(); th.out.writeUTF(“聊天者:“+name+“性别“+sex);if(th!=this) out.writeUTF(“聊天者:“+th.name+“性别“+th.sex); else out.writeUTF(“不可以聊天:“); else if(s.startsWith(“公共聊天内容:“) String message=s.substring(s.indexOf

7、(“:“)+1); Enumeration enu=peopleList.elements(); while(enu.hasMoreElements() (Server_thread)enu.nextElement().out.writeUTF(“聊天内容:“+message); else if(s.startsWith(“用户离开:“) Enumeration enu=peopleList.elements(); while(enu.hasMoreElements() try Server_thread th=(Server_thread)enu.nextElement(); if(th!=

8、this catch(IOException eee) peopleList.remove(name); socket.close(); System.out.println(name+“用户离开了“); break; else if(s.startsWith(“私人聊天内容:“) String 悄悄话=s.substring(s.indexOf(“:“)+1,s.indexOf(“#“); String toPeople=s.substring(s.indexOf(“#“)+1);Server_thread toThread=(Server_thread)peopleList.get(toP

9、eople); if(toThread!=null) toThread.out.writeUTF(“私人聊天内容:“+悄悄话); else out.writeUTF(“私人聊天内容:“+toPeople+“已经离线“); catch(IOException ee) Enumeration enu=peopleList.elements(); while(enu.hasMoreElements() try Server_thread th=(Server_thread)enu.nextElement(); if(th!=this catch(IOException eee) peopleList

10、.remove(name); try socket.close(); catch(IOException eee) System.out.println(name+“用户离开了“); break; 1.3.2.建立客户端工程和 JAVA 类,类程序的原代码为: import java.awt.*; import java.io.*; import .*; import java.applet.*; import java.util.Hashtable;public class ClientChat extends Applet implements Runnable Socketsocket=

11、null; DataInputStream in=null; DataOutputStream out=null; InputNameTextField 用户提交昵称界面=null; ChatArea 用户聊天界面=null; Hashtable listTable; Label 提示条; Panel north, center; Thread thread; public void init() int width=getSize().width;int height=getSize().height; listTable=new Hashtable(); setLayout(new Bor

12、derLayout(); 用户提交昵称界面=newInputNameTextField(listTable); int h=用户提交昵称界面.getSize().height; 用户聊天界面=new ChatArea(“,listTable,width,height-(h+5); 用户聊天界面.setVisible(false); 提示条=new Label(“正在连接到服务器,请稍等.“,Label.CENTER); 提示条.setForeground(Color.red); north=new Panel(new FlowLayout(FlowLayout.LEFT); center=ne

13、w Panel(); north.add(用户提交昵称界面); north.add(提示条); center.add(用户聊天界面); add(north,BorderLayout.NORTH); add(center,BorderLayout.CENTER); validate(); public void start() if(socket!=null in.close(); out.close(); 用户聊天界面.setVisible(false); catch(Exception ee) try socket = new Socket(this.getCodeBase().getHos

14、t(), 6666); in=new DataInputStream(socket.getInputStream(); out=new DataOutputStream(socket.getOutputStream(); catch (IOException ee) 提示条.setText(“连接失败“); if(socket!=null) InetAddress address=socket.getInetAddress(); 提示条.setText(“连接:“+address+“成功“); 用户提交昵称界面.setSocketConnection(socket,in,out); north

15、.validate(); if(thread=null) thread=new Thread(this); thread.start(); public void stop() try socket.close(); thread=null; catch(IOException e) this.showStatus(e.toString(); public void run() while(thread!=null) if(用户提交昵称界面.get 能否聊天()=true) 用户聊天界面.setVisible(true); 用户聊天界面.setName(用户提交昵称界面.getName(); 用户聊天界面.setSocketConnection(socket,in,out); 提示条.setText(“祝聊天愉快!“); center.validate(); break; try Thread.sleep(100); catch(Exception e) 1.3.3.调试使以上两个工程能正常运行,使服务端和客户端能正常通信 登陆界面:聊天界面:1.4.实验心得其实,简单的分析一下,就可以看出客户和服务通讯的主要通道就是 Socket 本身,而服务器 通过 accept 方法就是同意和客户建立通讯

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号