2、h>#include#include#include#include#include#include#include#include"Practical.h"#defineSERVER_IP_STRING"127.1.1.1"#defineSERVER_PORT_NUM1500#defineRECV_BUFFER_SIZE1024intmain(intargc,char*a
3、rgv[]){if(argc<3
4、
5、argc>4)DieWithUserMessage("Parameter(s)","[]");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)东北大学秦皇岛分校计算机与通信工程学院第19页网
6、络编程技术实验报告DieWithSystemMessage("socket()fialed");structsockaddr_inservAddr;memset(&servAddr,0,sizeof(servAddr));servAddr.sin_family=AF_INET;//设置地址簇intrtnVal=inet_pton(AF_INET,servIP,&servAddr.sin_addr.s_addr);//把服务器地址的字符串转换成32位的2进制if(rtnVal==0)DieWithUserMessage("inet_pton
7、()failed","invalidaddressstring");elseif(rtnVal<0)DieWithSystemMessage("inet_pton()failed");servAddr.sin_port=htons(servPort);if(connect(sock,(structsockaddr*)&servAddr,sizeof(servAddr))<0)DieWithSystemMessage("connet()failed");size_techoStringLen=strlen(echoString);ssize
8、_tnumBytes=send(sock,echoString,echoStringLen,0);if(numBytes<0)//接受应答服务器DieWithSystemMessage("send()failed");elseif(numBytes!=echoStringLen)DieWithUserMessage("send()","sentunexpectednumberofbytes");unsignedinttotalBytesRcvd=0;fputs("Received:",stdout);while(totalBytesRcv
9、d