您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > ……实验08-Java之网络编程
实验七网络编程1.实验目的(1)掌握Socket通信。(2)掌握UDP通信2.实验内容实验题1利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。客户端向服务器端发送Time命令,服务器端接受到该字符串后将服务器端当前时间返回给客户端;客户端向服务器端发送Exit命令,服务器端向客户端返回“Bye”后退出。[基本要求]编写完整程序。服务器端:packageedu.cn.jp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassMessageServer{publicstaticvoidmain(String[]args){try{ServerSocketserver=newServerSocket(3000);Socketclient=server.accept();//启动倾听,等待客户端BufferedReaderin=newBufferedReader(newInputStreamReader(client.getInputStream()));//输入流对象PrintWriterout=newPrintWriter(client.getOutputStream());//输出流对象BufferedReaderuserin=newBufferedReader(newInputStreamReader(System.in));//实现键盘输入//启动接受和发送线程newsendThread(out,userin,server).start();newreciveThread(in).start();}catch(IOExceptione){e.printStackTrace();}}}packageedu.cn.jp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassreciveThreadextendsThread{BufferedReaderin;//服务器端接受线程publicreciveThread(BufferedReaderin){this.in=in;}@Overridepublicvoidrun(){try{while(true){Stringinfo=in.readLine();//获取信息if(info.equals(exit)){break;}while(info!=null){System.out.println(info);info=in.readLine();if(info.equals(exit)){System.out.println(info);break;}}break;}}catch(IOExceptione){e.printStackTrace();}}}packageedu.cn.jp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;importjava.text.SimpleDateFormat;importjava.util.Date;publicclasssendThreadextendsThread{PrintWriterout;BufferedReaderuserin;ServerSocketserver;publicsendThread(PrintWriterout,BufferedReaderuserin,ServerSocketserver){this.out=out;this.userin=userin;this.server=server;}@Overridepublicvoidrun(){SimpleDateFormatsf=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss.sss);Stringstr=null;try{out.println(sf.format(newDate()));out.flush();while(true){str=userin.readLine();out.println(Server:+\n+str);//服务器端从键盘输入out.flush();if(str.equals(Bybe)){break;}}userin.close();server.close();}catch(IOExceptione){e.printStackTrace();}}}客户端:packageedu.cn.jp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.InetAddress;importjava.net.Socket;publicclassclent{publicstaticvoidmain(String[]args){try{Socketserver=newSocket(InetAddress.getLocalHost(),3000);//创建套接字BufferedReaderin=newBufferedReader(newInputStreamReader(server.getInputStream()));PrintWriterout=newPrintWriter(server.getOutputStream());BufferedReaderuserin=newBufferedReader(newInputStreamReader(System.in));newsendThread(out,userin).start();newreciveThread(server,in,out,userin).start();}catch(IOExceptione){e.printStackTrace();}}}packageedu.cn.jp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassreciveThreadextendsThread{BufferedReaderin;PrintWriterout;BufferedReaderuserin;Socketclient;publicreciveThread(Socketclient,BufferedReaderin,PrintWriterout,BufferedReaderuserin){this.in=in;this.client=client;this.out=out;this.userin=userin;}@Overridepublicvoidrun(){try{while(true){Stringinfo=in.readLine();if(info.equals(Bybe)){break;}while(info!=null){System.out.println(info);info=in.readLine();if(info.equals(Bybe)){System.out.println(info);break;}}break;}client.close();in.close();out.close();userin.close();}catch(IOExceptione){e.printStackTrace();}}}packageedu.cn.jp;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.PrintWriter;importjava.text.SimpleDateFormat;importjava.util.Date;publicclasssendThreadextendsThread{PrintWriterout;BufferedReaderuserin;publicsendThread(PrintWriterout,BufferedReaderuserin){this.out=out;this.userin=userin;}@Overridepublicvoidrun(){SimpleDateFormatsf=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss.sss);Stringstr=null;try{out.println(sf.format(newDate()));out.flush();while(true){str=userin.readLine();out.println(client:+\n+str);out.flush();}}catch(IOExceptione){e.printStackTrace();}}}实验题2编写一数据报通信程序,实现简单的聊天功能。图3.9聊天程序界面[基本要求]两人一组编写完整程序。“聊天内容”和“输入文本”分别为当前聊天的历史信息和当前要传送出去的聊天文本。“确定”、“清空”、“退出”三个按钮分别实现发送当前聊天文本、清空当前聊天文本和退出系统的功能。packagecn.edu.udp.jp;importjava.awt.BorderLayout;publicclassGui_UdpextendsJFrame{privateJPanelcontentPane;privateJTextFieldtextField;privatestaticJTextAreatextArea;publicstaticvoidreceive()throwsException{DatagramSocketds=newDatagramSocket(7000);byte[]buf=newbyte[1024];DatagramPacketdp=newDatagramPacket(buf,1024);ds.receive(dp);Stringip=dp.getAddress().getHostAddress();if(!InetAddress.getLocalHost().getHostAddress().equals(ip)){聊天内容输入文本确定清空退出textArea.append(接收UDP包:\n+newString(buf,0,dp.getLength())+\n);}ds.close();}publicvoidsend(StringsendStr)throwsException{InetAddressaddress=InetAddress.getByName(127.0.0.1);byte[]buf=sendStr.getBytes();DatagramPacketdp=newDatagramPacket(buf,buf.length,address,7000);DatagramSocketds=newDatagramSocket();ds.send(dp);ds.close()
本文标题:……实验08-Java之网络编程
链接地址:https://www.777doc.com/doc-5303845 .html