西邮计算机学院java实验报告.doc
《西邮计算机学院java实验报告.doc》由会员分享,可在线阅读,更多相关《西邮计算机学院java实验报告.doc(25页珍藏版)》请在沃文网上搜索。
1、网络程序设计报告书系部名称:计算机学院专业班级:学生姓名:学生学号:一、实验目的1掌握TCP/IP体系结构中端口、套接字、TCP协议概念;2掌握TCP SOCKET的ServerSocket和Socket;3掌握TCP SOCKET技术中多线程技术。二、实验内容1 学习TCP/IP体系结构,理解什么是SAP、端口、套接字的概念;掌握TCP传输模式和netstat命令的用途;2 学习为TCP服务的 Socket和ServerSocket类的使用,掌握TCP连接的方法,服务器接收客户端连接请求的方法,创建输入/输出流的方法,传输数据的方法,以及关闭流和套接字,注意可能会出现的异常操作;3 完成以下
2、各内容程序,截存运行结果图,并提交实验报告。三、实验内容1在Windows下使用netstat命令,查看本地计算机开放端口。获得/help,分别测试-s -e r -a n及其组合。2简单的单线程,一问一答式通信的TCP Server和TCP Client,注意在编译源代码时有个提示“某API已经过时”,找到该API类或者方法。在运行程序时,注意通信双方是否随时输入,如果不能,考虑可能是什么原因造成的。位置2的语句不能放在位置1,因为main方法是个静态的方法,所以除非定义为静态的。位置2不能放在位置三处,因为代码块是封闭的 ,第二个try里面将会看不到,另一个try里面的定义。2以下代码是个
3、非常经典的用户自定义一问一答协议,在这里只需要修改服务器端程序即可,添加了GreetingProtocol.java(自定义协议),调试并修改协议使程序可以正常运行。/服务器端import java.io.*;import .*;public class MyServerpublic static void main(String args)/TCP通信,作为服务器ServerSocket serverSocket = null; /首先建立服务器tryserverSocket = new ServerSocket(1080);/端口号唯一标是本进程System.out.println(Ser
4、ver is Listening Now);catch(IOException e)System.err.println(Cant listen on port);System.exit(1);/接受客户端连接Socket clientSocket = null;tryclientSocket = serverSocket.accept(); /connectSystem.out.println(已经连接上!);catch(IOException e)System.err.println(Accept failed);System.exit(1);tryPrintStream out = ne
5、w PrintStream(new BufferedOutputStream(clientSocket.getOutputStream(), 1024) ,false);DataInputStream in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream();String inputLine, outputLine;GreetingProtocol greeting = new GreetingProtocol();/自定义协议outputLine = greeting.processInput
6、(null);/输出操作out.println(Welcome to My Chat Server+ +outputLine);out.flush();/立即将数据从输出缓存提交给网络发送/输入操作while(inputLine = in.readLine() != null)outputLine = greeting.processInput(inputLine); /协议处理out.println(outputLine);out.flush();if(outputLine.equals(bye) break;out.close();/流关闭in.close();clientSocket.c
7、lose();/套接字关闭serverSocket.close();catch(IOException e)System.err.println(Protocol Stream Error);/协议/自定义协议import java.io.*;import .*;public class GreetingProtocolprivate static final int WAITING = 0;private static final int SENDKNOCK = 1;private static final int SENDCLUE = 2;private static final int
8、ANOTHER = 3;private static final int NUMJOKES = 5;private int state = WAITING;private int currentJoke = 0;private String clues = Turnip, Little Old Lady, Atch, Lamp, Godness;private String answers = Turnip the heat, its cold in here!,I did not know you could come!,Bless you,Lamp is bright,Godness is
9、 happy;public String processInput(String theInput)String theOutput = null;/System.out.println(state);if(state = WAITING)theOutput = Knock! Knock!.;state = SENDKNOCK;else if( state = SENDKNOCK)if(theInput.equalsIgnoreCase(Whos there?)theOutput = cluescurrentJoke;state = SENDCLUE;elsetheOutput = You a
10、re supposed to say Whos there? ! Try again.;else if( state = SENDCLUE)if(theInput.equalsIgnoreCase(cluescurrentJoke + Who?)theOutput = answerscurrentJoke + Want another?(y/n);state = ANOTHER;elsetheOutput = You are supposed to say + cluescurrentJoke + Who? ! Try again. Knock! Knock!;else if( state =
11、 ANOTHER)if(theInput.equalsIgnoreCase(y)theOutput = Knock! Knock!;if(currentJoke = (NUMJOKES - 1)currentJoke = 0;elsecurrentJoke +;state = SENDKNOCK;elsetheOutput = bye;state = WAITING;return theOutput;/客户端import java.io.*;import .*;public class Client public static void main(String args)Socket Clie
12、ntSocket = null;try ClientSocket = new Socket(localhost,1080);PrintStream out = new PrintStream(new BufferedOutputStream(ClientSocket.getOutputStream(),1024),false);DataInputStream in = new DataInputStream(new BufferedInputStream(ClientSocket.getInputStream();DataInputStream stdIn = new DataInputStr
13、eam(System.in);String inputLine, outputLine;while(inputLine = in.readLine() != null)System.out.println(server:+inputLine);outputLine = stdIn.readLine();if(outputLine.equals(bye) break;out.println(outputLine);out.flush();out.close();/流关闭in.close();ClientSocket.close();/套接字关闭 catch (UnknownHostExcepti
14、on e) System.out.println(无法连接到服务器!);e.printStackTrace(); catch (IOException e) System.out.println(无法获得I/O流!);e.printStackTrace();运行结果如下:3探测目标计算机开放的TCP 端口import java.io.*;import .*;public class ScanPortpublic static void main(String args) throws IOExceptionSocket comSocket = null;for(int i=0;i1024; i
15、+)try/建立socket连接comSocket = new Socket(localhost, i);/发出连接请求System.out.println(Can get I/O for the Connection, Port: + i);catch(UnknownHostException e)System.err.println(Cant find the Server host);/System.exit(0);catch(IOException e)System.err.println(Cant get I/O for the Connection, Port: + i);/Sys
16、tem.exit(0);trycomSocket.close();catch(Exception e)运行的结果如下:自作实验:1自行编写一个一问一答的场景,例如,帐号登录场景,A:用户名?B:user。A:密码?B:123456,应该将用户名和密码保存在服务器端的文件中,用于核对登录是否正确import java.io.*;import .*;public class project_01 /这是个协议private boolean nextStep = false;static String outputString = null;static int is_register = 0;st
17、atic int next = 1;static int next2 =1;static int checked =1;static String flage = y;static String flage1 = y;public static boolean check(String inputString)int readLine;String writeLine = ;FileReader F_out;try F_out = new FileReader(d:/User.txt);while(readLine = F_out.read()!=-1)writeLine +=(char)re
18、adLine; if(writeLine.equals(inputString)return true;elsereturn false; catch (FileNotFoundException e) return false; catch (IOException e) return false;public static boolean check1(String inputString)int readLine;String writeLine = ;FileReader F_out;try F_out = new FileReader(d:/passwd.txt);while(rea
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
10 积分
下载 | 加入VIP,下载更划算! |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 学院 java 实验 报告
