欢迎来到天天文库
浏览记录
ID:56248688
大小:73.00 KB
页数:10页
时间:2020-03-24
《实验08 Java之网络编程.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验七网络编程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;importjav
2、a.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassMessageServer{publicstaticvoidmain(String[]args){try{ServerSocketserver=newServerSocket(3000);Socketclient=server.accept();//启动倾听,等待客户端BufferedReaderin=newBufferedReader(newInputStreamReader(client.getInputStream()));//输入流
3、对象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.
4、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();//获取
5、信息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
6、;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;
7、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.re
此文档下载收益归作者所有