套接字编程.docx_第1页
套接字编程.docx_第2页
套接字编程.docx_第3页
套接字编程.docx_第4页
套接字编程.docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

专业: 姓名: 学号: 日期: 地点: 实验报告课程名称: 计算机网络 指导老师: 成绩:_实验名称: 实验类型: 同组学生姓名:_一、实验目的和要求(必填)二、实验内容和原理(必填)三、主要仪器设备(必填)四、操作方法和实验步骤五、实验数据记录和处理六、实验结果与分析(必填)七、讨论、心得装 订 线一、实验目的和要求开始使用套接字编程逐步熟悉Winsock完成一个简单的Web server二、实验内容和原理 阅读教材和PPT学习老师提供的winsock编程资料安装相应的开发环境三、主要仪器和设备Java四、操作方法和实验步骤Stage 1 The echoer Simply sends back & displays the contents of the received data stream from the client Using telnet to test your echoer. Run cmd telnet your_ip your_port type in random characters & check quitjava程序:import java.io.*; import .*; public class TCPServer public static void main(String argv) throws Exception System.out.println(start); String clientSentence; /读写语句 String serverSentence; /发送语句 ServerSocket welcomeSocket = new ServerSocket(6789); /建立套接字,端口6789 while(true) Socket connectionSocket = welcomeSocket.accept(); /建立发送套接字 BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream(); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream(); outToClient.writeBytes(Server has started!rn); clientSentence = inFromClient.readLine(); outToClient.writeBytes(Have received: ); serverSentence = clientSentence + n; outToClient.writeBytes(serverSentence); /send back the message connectionSocket.close(); /关闭套接字 对套接字运行测试:Stage 2Paser Functionality1. Check whether the message is a HTTP GET request message.2. If not, send back Bad request string.3. If so, find out the of the file the client is requesting.4. Check if the file, , exists.5. If not, send back File not existed string6. If so, send back1. File found string2. File size: stringJava程序:import java.io.*; import .*; public class WEBServer public static void main(String argv) throws Exception String clientSentence; String capitalizedSentence=null; String request=new String(GET); long size; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream(); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream(); clientSentence = inFromClient.readLine(); String word = clientSentence.split( ); if(request.equals(word0) String name = word1.split(/); File file = new File(name1); if(file.exists() size = file.length(); capitalizedSentence = File found+File size:+size; else capitalizedSentence = (File not existed); else capitalizedSentence = Bad request; outToClient.writeBytes(capitalizedSentence); connectionSocket.close(); 输入:http:/localhost:6789/test.html输入http:/localhost:6789/tst.htmlStage3 the responderWeb Server Functionality1. Check whether the message is a GET message.2. If not, send back Bad request string.3. If so, find out the of the file the client is requesting.4. Check if the file, , exists.5. If not, send back File not existed string6. If so, send back an HTTP Response with the fileJava 程序:import java.io.*; import .*; public class WEBServer public static void main(String argv) throws Exception String clientSentence; String capitalizedSentence=null; String request=new String(GET); long size; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream(); DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream(); clientSentence = inFromClient.readLine(); String word = clientSentence.split( ); if(request.equals(word0) String name = word1.split(/); File file = new File(name1); if(file.exists() FileInputStream content = new FileInputStream(file); byte data = new bytecontent.available(); content.read(data); content.close(); FileOutputStream content1 = new FileOutputStream(file); content1.write(data); content1.close(); size = file.length(); capitalizedSentence = HTTP/1.0 200 Document Followsrn+Content-Length:+size+rnrn+new String (data); else capitalizedSentence = (File n

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论