TCP通信模型
TCP服务器端
#include<myhead.h>#define PORT 8888
#define IP "192.168.125.130"int main(int argc, const char *argv[])
{//创建文件描述符int sfd = -1;if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1){perror("socket error");return -1;}int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1){ perror("setsockopt error");return -1; } //定义结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(PORT);sin.sin_addr.s_addr = inet_addr(IP);//链接if(bind(sfd,(struct sockaddr*) &sin,sizeof(sin)) == -1){perror("bind");return -1;}if(listen(sfd,128)==-1){perror("listen");return -1;}//定义接受客服端的地址结构体struct sockaddr_in cin;socklen_t socklen = sizeof(cin);int newfd = -1;if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen)) == -1){perror("accept");return -1;}//收发数据char buf[128]="";while(1){bzero(buf,sizeof(buf));int res=recv(newfd,buf,sizeof(buf),0);if(res == 0){printf("客服端下线\n");break;}printf("%s\n",buf);strcat(buf,"*_*");send(newfd,buf,sizeof(buf),0);}close(sfd);close(newfd);return 0;
}
TCP客户端
#include<myhead.h>#define PORT 8888
#define IP "192.168.125.130"
#define PORT1 6666
#define IP1 "192.168.125.130"
int main(int argc, const char *argv[])
{//创建文件描述符int sfd = -1;if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1){perror("socket error");return -1;}int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))==-1){ perror("setsockopt error");return -1; } //定义结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(PORT1);sin.sin_addr.s_addr = inet_addr(IP1);//链接if(bind(sfd,(struct sockaddr*) &sin,sizeof(sin)) == -1){perror("bind");return -1;}//定义接受客服端的地址结构体struct sockaddr_in cin;cin.sin_family = AF_INET;cin.sin_port = htons(PORT);cin.sin_addr.s_addr = inet_addr(IP);if(connect(sfd,(struct sockaddr*)&cin,sizeof(sin)) == -1){perror("connect");return -1;}//收发数据char buf[128]="";while(1){bzero(buf,sizeof(buf));fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]=0;send(sfd,buf,sizeof(buf),0);bzero(buf,sizeof(buf));int res=recv(sfd,buf,sizeof(buf),0);if(res == 0){printf("服务器下线\n");break;}printf("%s\n",buf);}close(sfd);return 0;
}
效果图:
UDP通信模型
UDP服务器:
#include<myhead.h>#define PORT 8888
#define IP "192.168.125.130"int main(int argc, const char *argv[])
{int sfd = -1;if((sfd = socket(AF_INET,SOCK_DGRAM,0)) == -1){perror("socket");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(PORT);sin.sin_addr.s_addr = inet_addr(IP);if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("bind");return -1;}struct sockaddr_in cin;socklen_t socklen = sizeof(cin);char buf[128]="";while(1){bzero(buf,sizeof(buf));int res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&socklen);if(res == 0){printf("客户端下线\n");break;}printf("发来的消息为%s\n",buf);strcpy(buf,"*_*");sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin));}return 0;
}
UDP客户端:
#include<myhead.h>#define PORT 8888
#define IP "192.168.125.130"
#define PORT1 6666
#define IP1 "192.168.125.130"
int main(int argc, const char *argv[])
{int sfd = -1;if((sfd = socket(AF_INET,SOCK_DGRAM,0)) == -1){perror("socket");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(PORT1);sin.sin_addr.s_addr = inet_addr(IP1);if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("bind");return -1;}struct sockaddr_in cin;cin.sin_family = AF_INET;cin.sin_port = htons(PORT);cin.sin_addr.s_addr = inet_addr(IP);socklen_t socklen = sizeof(cin);char buf[128]="";while(1){bzero(buf,sizeof(buf));fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]=0;sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin));int res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&socklen);if(res == 0){printf("服务器下线\n");break;}printf("传回来的消息为%s\n",buf);}return 0;
}
效果图: