资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
基于TCP协议下的Linux网络编程的服务器端和客户端 客户端:Cl ien t. c/*,/ J. /撰 .i. ,. . 撰/ ,/4、*Name : Linux Client *Wate : 2014-11-10 * Author : marksman * *Aim : Linux Client can send and receive * * message. *%r*/include ttinclude #include ttinclude #includememory. hinclude int main(int argc, char *argv)int socketfd, receive_socket;struct sockaddr in s add,c add;unsigned short portnum = 0x22B8;int len;char buf100;/*Create the socket*/if(socketfd二socket(AF_INET, SOCK_STREAM, 0)0)(printf (Socket create error! n);exit (1);)/*set the address format*/bzero (&s_add, sizeof(struct sockaddr_in);s_add. sin_family = AF_INET;s_add. sin_addr. s_addr = inet_addr (z,127. 0. 0. 1);/change the string to32-bit internet byte.s_add. sin-porthlons(portnum);if (connect(socketfd, (struct sockaddr *)(&s add), sizeof(struct sockaddr)0)printf (Connect failure!n,z):return T;)elseprintf(Connect Success!/);/*Using the loop to send and receive the message.*/whiled)memset (buf, 0, 100);printf (Input message to send: );fgets (buf, 100, stdin);len=send(socketfd, buf, strlen (buf)-1, 0);if(len0)(printf (z,send error! n/z);exit (1);)else(printf (z,Send Success! send is : %s n,buf);memset(buf, 0, 100);len=recv (socketfd, buf, 100, 0);if (len .i. ,. . 撰/ ,/4、*Namc : Linux Server*Date : 2014-11-10Author : marksman*Aim : Linux Server can send and receive* message. Tt is atom system about* Instant Messaging system.*/ttinclude#include#includeincludeincludettincludettinclude int main(int urge, char *argv)int socketfd listen,socketfd connect; struct sockaddr in server addr.client addr;unsigned short portnum = 0x22B8;int len ;char buf100;/*Create the socket*/if(socketfd_listen=socket(AF_TNET, SOCK_STREAM, 0)0)printf (Socket create error! n);exit(1);/*set the address format*/bzero (&,server addr, sizeof (struct sockaddr in);server addr. s i n_fami1y=AF_INET;server_addr. sin_addr. s_addr=htonl(INADDR_ANY);server_addr. sin_port=htons(portnum);if(bind(socketfd listen, (struct sockaddr *)(&server addr), sizeof(struct sockaddr)0)printf (,?bind failure!n,z);return T;printf (bind Success!n/z);if(listen(socketfd_iisten, 5)0)(printf (Listen failure!n,z);return -1;)len=sizeof(struct sockaddr);socketfd connect = accept(socketfd listen, (struct sockaddr *)(&client_addr), &len);if (socketfd_connecl0)(printf(accept fail !n);return -1;)/*Using the loop to send and receive the message.*/while(l)memset (buf, 0, 100);len=recv (socketfd_connect, buf, 100, 0);if(len0)(printf (,zrecv error! nz,);exit (1);else if(0 二二 len)(printf(the client quit!n);break;else(printf (receive message is : %s n,buf);)/memset (buf, 0, 100);/printf(Input message to send : );/fgets(buf, 100, stdin);len=send(socketfd_connect, buf, strlen(buf)-!, 0);if (len0)(printf (Send error! n,z);exit (1);else(printf(Send succeed send : %s n,buf);)printf Cn);close(socketfd_listen);close(socketfd_connect);return 0; 运行过程在一个终端下编译两个文件,运行服务器或者客户端中的一个,然后再打开一个终端, 运行另一个即可。具体操作:$gcc -。server Server, c$gcc -0 client Client, c终端A:$. /server终端B:$ /client
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号