欢迎来到天天文库
浏览记录
ID:37525762
大小:1.01 MB
页数:21页
时间:2019-05-24
《东北大学秦皇岛分校网络编程技术实验》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、网络编程技术实验报告学号:姓名:提交日期:成绩:简短评语:教师签字:东北大学秦皇岛分校计算机工程系【实验编号】实验一【实验名称】基于C/S结构的网络通信程序的实现【实验目的】(1)熟悉编程环境。(2)获得初步的网络编程实践技能。【实验内容】(1)Windows/Linux中开发环境的设置。(2)编写基于C/S结构的网络通信程序。【实验程序】一、客户端/*TCPEchoClient*//*4110320-yangtongyong*//*测试服务器IP地址为127.1.1.1端口号5000*/#include#include#include2、g.h>#include#include#include#include#include#include"Practical.h"#defineSERVER_IP_STRING"127.1.1.1"#defineSERVER_PORT_NUM1500#defineRECV_BUFFER_SIZE1024intmain(intargc,char*argv[]){if(argc<33、4、argc>4)DieWithUserMessage("Parameter(s)"5、,"[]");char*servIP=argv[1];char*echoString=argv[2];in_port_tservPort=(argc==4)?atoi(argv[3]):7;intsock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(sock<0)DieWithSystemMessage("socket()fialed");structsockaddr_inservAddr;memset(&servAddr,0,sizeof(servAddr));serv6、Addr.sin_family=AF_INET;//设置地址簇intrtnVal=inet_pton(AF_INET,servIP,&servAddr.sin_addr.s_addr);//把服务器地址的字符串转换成32位的2进制if(rtnVal==0)DieWithUserMessage("inet_pton()failed","invalidaddressstring");elseif(rtnVal<0)DieWithSystemMessage("inet_pton()failed");servAddr.sin_port=htons(servPort);if(connect(so7、ck,(structsockaddr*)&servAddr,sizeof(servAddr))<0)DieWithSystemMessage("connet()failed");size_techoStringLen=strlen(echoString);ssize_tnumBytes=send(sock,echoString,echoStringLen,0);if(numBytes<0)//接受应答服务器DieWithSystemMessage("send()failed");elseif(numBytes!=echoStringLen)DieWithUserMessage("sen8、d()","sentunexpectednumberofbytes");unsignedinttotalBytesRcvd=0;fputs("Received:",stdout);while(totalBytesRcvd9、osedprematurely");totalBytesRcvd+=numBytes;buffer[numBytes]=' ';fputs(buffer,stdout);}fputc('',stdout);close(sock);exit(0);}二、服务器/*TCPEchoServer.c*//*Author:yangtongyong*/#include#include"Practical.h"#include
2、g.h>#include#include#include#include#include#include"Practical.h"#defineSERVER_IP_STRING"127.1.1.1"#defineSERVER_PORT_NUM1500#defineRECV_BUFFER_SIZE1024intmain(intargc,char*argv[]){if(argc<3
3、
4、argc>4)DieWithUserMessage("Parameter(s)"
5、,"[]");char*servIP=argv[1];char*echoString=argv[2];in_port_tservPort=(argc==4)?atoi(argv[3]):7;intsock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(sock<0)DieWithSystemMessage("socket()fialed");structsockaddr_inservAddr;memset(&servAddr,0,sizeof(servAddr));serv
6、Addr.sin_family=AF_INET;//设置地址簇intrtnVal=inet_pton(AF_INET,servIP,&servAddr.sin_addr.s_addr);//把服务器地址的字符串转换成32位的2进制if(rtnVal==0)DieWithUserMessage("inet_pton()failed","invalidaddressstring");elseif(rtnVal<0)DieWithSystemMessage("inet_pton()failed");servAddr.sin_port=htons(servPort);if(connect(so
7、ck,(structsockaddr*)&servAddr,sizeof(servAddr))<0)DieWithSystemMessage("connet()failed");size_techoStringLen=strlen(echoString);ssize_tnumBytes=send(sock,echoString,echoStringLen,0);if(numBytes<0)//接受应答服务器DieWithSystemMessage("send()failed");elseif(numBytes!=echoStringLen)DieWithUserMessage("sen
8、d()","sentunexpectednumberofbytes");unsignedinttotalBytesRcvd=0;fputs("Received:",stdout);while(totalBytesRcvd9、osedprematurely");totalBytesRcvd+=numBytes;buffer[numBytes]=' ';fputs(buffer,stdout);}fputc('',stdout);close(sock);exit(0);}二、服务器/*TCPEchoServer.c*//*Author:yangtongyong*/#include#include"Practical.h"#include
9、osedprematurely");totalBytesRcvd+=numBytes;buffer[numBytes]=' ';fputs(buffer,stdout);}fputc('',stdout);close(sock);exit(0);}二、服务器/*TCPEchoServer.c*//*Author:yangtongyong*/#include#include"Practical.h"#include
此文档下载收益归作者所有