欢迎来到天天文库
浏览记录
ID:39474356
大小:49.00 KB
页数:6页
时间:2019-07-04
《基于TCP协议下的Linux网络编程的服务器端和客户端》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基于TCP协议下的Linux网络编程的服务器端和客户端客户端:Client.c/***************************************************Name:LinuxClient**Date:2014-11-10**Author:marksman**Aim:LinuxClientcansendandreceive**message.***************************************************/#include#include2、socket.h>#include#include#include#includeintmain(intargc,char*argv[]){intsocketfd,receive_socket;structsockaddr_ins_add,c_add;unsignedshortportnum=0x22B8;intlen;charbuf[100];/**Createthesocket*/if((socketfd=socket(AF_INET,S3、OCK_STREAM,0))<0){printf("Socketcreateerror!");exit(1);}/**settheaddressformat*/bzero(&s_add,sizeof(structsockaddr_in));s_add.sin_family=AF_INET;s_add.sin_addr.s_addr=inet_addr("127.0.0.1");//changethestringto32-bitinternetbyte.s_add.sin_port=htons(portnum);if(conn4、ect(socketfd,(structsockaddr*)(&s_add),sizeof(structsockaddr))<0){printf("Connectfailure!");return-1;}elseprintf("ConnectSuccess!");/**Usingthelooptosendandreceivethemessage.*/while(1){memset(buf,' ',100);printf("Inputmessagetosend:");fgets(buf,100,stdin);len=se5、nd(socketfd,buf,strlen(buf)-1,0);if(len<0){printf("senderror!");exit(1);}else{printf("SendSuccess!sendis:%s",buf);}memset(buf,' ',100);len=recv(socketfd,buf,100,0);if(len<0){printf("recverror!");exit(1);}elseif(0==len){printf("clientquit!");exit(1);}else{pri6、ntf("receivemessageis:%s",buf);}printf("--------------------------");}close(socketfd);}服务器端:Server.c/***************************************************Name:LinuxServer**Date:2014-11-10**Author:marksman**Aim:LinuxServercansendandreceive**message.Itisatomsystemabo7、ut**InstantMessagingsystem.***************************************************/#include#include#include#include#include#include#includeintmain(intargc,char*argv[]){intsocketfd_listen,soc8、ketfd_connect;structsockaddr_inserver_addr,client_addr;unsignedshortportnum=0x22B8;intlen;charbuf[100];/**Createthesocket*/if((sock
2、socket.h>#include#include#include#includeintmain(intargc,char*argv[]){intsocketfd,receive_socket;structsockaddr_ins_add,c_add;unsignedshortportnum=0x22B8;intlen;charbuf[100];/**Createthesocket*/if((socketfd=socket(AF_INET,S
3、OCK_STREAM,0))<0){printf("Socketcreateerror!");exit(1);}/**settheaddressformat*/bzero(&s_add,sizeof(structsockaddr_in));s_add.sin_family=AF_INET;s_add.sin_addr.s_addr=inet_addr("127.0.0.1");//changethestringto32-bitinternetbyte.s_add.sin_port=htons(portnum);if(conn
4、ect(socketfd,(structsockaddr*)(&s_add),sizeof(structsockaddr))<0){printf("Connectfailure!");return-1;}elseprintf("ConnectSuccess!");/**Usingthelooptosendandreceivethemessage.*/while(1){memset(buf,' ',100);printf("Inputmessagetosend:");fgets(buf,100,stdin);len=se
5、nd(socketfd,buf,strlen(buf)-1,0);if(len<0){printf("senderror!");exit(1);}else{printf("SendSuccess!sendis:%s",buf);}memset(buf,' ',100);len=recv(socketfd,buf,100,0);if(len<0){printf("recverror!");exit(1);}elseif(0==len){printf("clientquit!");exit(1);}else{pri
6、ntf("receivemessageis:%s",buf);}printf("--------------------------");}close(socketfd);}服务器端:Server.c/***************************************************Name:LinuxServer**Date:2014-11-10**Author:marksman**Aim:LinuxServercansendandreceive**message.Itisatomsystemabo
7、ut**InstantMessagingsystem.***************************************************/#include#include#include#include#include#include#includeintmain(intargc,char*argv[]){intsocketfd_listen,soc
8、ketfd_connect;structsockaddr_inserver_addr,client_addr;unsignedshortportnum=0x22B8;intlen;charbuf[100];/**Createthesocket*/if((sock
此文档下载收益归作者所有