Linux程序设计实验报告

{ pktlen = recv (ns, buf, sizeof (buf), 0); if (pktlen == 0) break; printf (\line: %s\\n\buf); printf (\a line: \ gets(buf3); /*并不是send把ns的发送缓冲中的数据传到连接的另一端的,而是协议传的,send仅仅是把buf中的数据copy到ns的发送缓冲区的剩余空间里 返回实际copy的字节数*/ send (ns, buf3,sizeof(buf3), 0); } close(ns); close(s); } TCPCilent端 #include <> #include <> #include <> #include #define PORT 7000 #define HOST_ADDR \ int main () { struct sockaddr_in server; int s, ns; int pktlen, buflen; char buf1[256], buf2[256]; s=socket(AF_INET, SOCK_STREAM, 0); = AF_INET; = htons(PORT); = inet_addr (HOST_ADDR); //connect第一个参数是client的socket描述符,第二个参数是server的socket地址,第三个为地址长度 if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) { perror(\ return; } //进行网络I/O for (;;) { printf (\a line: \ gets (buf1);//从stdin流中读取字符串,直至接受到换行符 buflen = strlen (buf1); if (buflen == 0) break; send(s, buf1, buflen + 1, 0); recv(s, buf2, sizeof (buf2), 0); printf(\line: %s\\n\buf2); } close(s); return 0; } Server和client通信截图: Server端: Client端:

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4