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
19、dLine = F_out.read()!=-1)writeLine +=(char)readLine; if(writeLine.equals(inputString)return true;elsereturn false; catch (FileNotFoundException e) return false; catch (IOException e) return false;public static boolean user_in_file(String inputString)boolean flage = false;FileWriter F_in;if(flage =fa
20、lse)try F_in = new FileWriter(d:/User.txt);F_in.write(inputString);F_in.flush();flage = true;return flage; catch (IOException e) return false;else return flage;public static boolean passwd_in_file(String inputString)boolean flage = false;FileWriter F_in;if(flage =false)try F_in = new FileWriter(d:/p
21、asswd.txt);F_in.write(inputString);F_in.flush();flage = true;return flage; catch (IOException e) return false;else return flage;public static String request(String inputString)if(!check(inputString)&checked=1)if(is_register =0)outputString = 对不起您还没有注册,是否要进行注册(y/n):;is_register = 1;return outputStrin
22、g;else if(is_register =1)if(next2 =1) if(inputString.equalsIgnoreCase(flage)outputString = exit;return outputString;elseoutputString = 请输入用户名:;next2 =2;return outputString;else if(next2=2)user_in_file(inputString);outputString = 请输入密码:;next2 =3;return outputString;else if(next2 =3)passwd_in_file(inp
23、utString);outputString =注册成功,请输入随意的字符进行下一步操作。;next2 =0;return outputString;checked=0;elseif(next =1)outputString = 是否要进行登录(y/n);next =2;return outputString;else if(next=2) if(inputString.equalsIgnoreCase(flage) outputString = exit;return outputString;elseoutputString = 用户名:;next =3;return outputStri
24、ng;else if(next =3)if(check(inputString)outputString =密码:;next =4;return outputString;elseoutputString = 用户名错误,请重新输入:;next=2;return outputString;else if(next =4)if(check1(inputString)outputString = 登录成功;next=0;return outputString;return outputString;public static void main(String args) ServerSocket
25、serverSocket= null;/建立服务器端的ServerSockettry serverSocket = new ServerSocket(10);System.out.println(服务器已启动,等待响应); catch (IOException e) System.out.println(端口被占用!);/进行接收,并且做出相应的I/O流try Socket ClientSocket = serverSocket.accept();System.out.println(欢迎进入!);BufferedReader in = new BufferedReader(new Input
26、StreamReader(ClientSocket.getInputStream();PrintWriter out = new PrintWriter(new BufferedOutputStream(ClientSocket.getOutputStream(), 1024) ,false);/设置一个注册程序 吧用户名和密码保存在本地的文件当中String inputLine,outputLine;out.println(欢迎来到这里,是否进入登录系统(y/n);out.flush();while(inputLine = in.readLine() != null) /接受网络数据outp
27、utLine = request(inputLine);out.println(outputLine); /向网络发送数据out.flush();if(outputString.equals(exit) break;out.close();in.close();serverSocket.close();ClientSocket.close(); catch (IOException e) System.out.println(无法接收客户端的连接!);/客户端import java.io.*;import .*;public class MyClientpublic static void m
28、ain(String args) throws IOExceptionSocket comSocket = null;PrintWriter out = null;/DataInputStream in = null;BufferedReader in = null;try/建立socket连接comSocket = new Socket(localhost, 10);/相指定服务器的端口发出连接请求,注意服务器开放的TCP端口号 /分别对应服务器端的O/I流in = new BufferedReader(new InputStreamReader(comSocket.getInputStre
29、am();out = new PrintWriter(comSocket.getOutputStream();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);System.exit(0); BufferedReader stdIn = new BufferedReader(new InputStreamReader(Sys
30、tem.in);String fromUser, fromServer;while(fromServer = in.readLine() != null)System.out.println(fromServer);fromUser = stdIn.readLine();if(fromUser != null)out.println(fromUser);out.flush();out.close();in.close();stdIn.close();comSocket.close();截图如下:本地磁盘:2将2题中的场景,模仿实验编写成为自定义协议/这是一个自定义的户口查询程序,一问一答的协议
31、public class MyProtocal private static int next_name = 0;private static int next_adress = 0;private static int next_persons = 0;private static int next_home = 0;private static final int anthor = 0;private static final int y = 1;private static final int n = 0;private static boolean is_over = false;pr
32、ivate static int c_next = 0;private String names = 张三, 李四, 王五 ;private String adress = 杭州, 山西, 北京 ;private String num_persons = 四口人, 三口人, 五口人 ;private String state_home = 良好, 一般, 很好 ;public String Protocal(String inputString) String outputString = null;if (!is_over) if (c_next = 0) if (inputString.e
33、qualsIgnoreCase(y) outputString = 请输入你要问的项目:;c_next = 1;return outputString; else outputString = 谢谢合作;c_next = 7;return outputString; else if (c_next = 1) if (inputString.equals(姓名) outputString = namesnext_name;c_next = 2;return outputString; else c_next = 1;outputString = 您应该输入姓名,请在输入;return outpu
34、tString; else if (c_next = 2) if (inputString.equals(地址) outputString = adressnext_adress;c_next = 3;return outputString; else c_next = 2;outputString = 您应该输入地址,请在输入;return outputString; else if (c_next = 3) if (inputString.equals(几口人) outputString = adressnext_persons;c_next = 4;return outputString
35、; else c_next = 3;outputString = 您应该输入几口人,请在输入;return outputString; else if (c_next = 4) if (inputString.equals(家庭情况) outputString = adressnext_home;is_over = true;c_next = 5;return outputString; else c_next = 4;outputString = 您应该输入家庭情况,请在输入;return outputString; else if (c_next = 5) outputString = 您
36、已经调查完了,是否继续调查别人(y/n);c_next = 6;return outputString; else if (c_next = 6) if (inputString.equals(y) outputString = 请输入你要问的项目:;next_name+;next_adress+;next_home+;next_persons+;c_next = 1;return outputString; else outputString = 谢谢合作;c_next = 7;return outputString; else if (c_next = 7) is_over = true;
37、return outputString;3.设计利用TCP Socket探测指定网络中计算机开放的所有TCP端口import java.io.*;import .*;public class ScanPortpublic static void main(String args) throws IOExceptionSocket comSocket = null;byte bs = new byte (byte)192,(byte)168,(byte)1,2;InetAddress localAddress = InetAddress.getByAddress(bs);for(int i=0;i100; i+)try/建立socket连接comSocket = new Socket(localAddress, i);/发出连接请求System.out.pr