1.基于TCP的通信(面向连接的通信)
服务器代码实现:
#include <myhead.h>
#define IP "192.168.126.91"
#define PORT 9999
int main(int argc, const char *argv[])
{//1、创建套接字int sfd=-1;if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror("socket error");return -1;}printf("sfd=%d\n",sfd);//2、绑定//将服务器的地址信息结构体绑定到sfd文件描述符中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 error");return -1;}//设置监听if(listen(sfd,128)==-1){perror("listen error");return -1;}//accept连接//创建连接之后的客户端地址信息结构体struct sockaddr_in cin;socklen_t socklen=sizeof(cin);int newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);if(newfd==-1){perror("accept error");return -1;}printf("[%s:%d]连接成功\n",inet_ntoa(cin.sin_addr)\,ntohs(cin.sin_port));//进行数据的收发char rbuf[128]="";while(1){//接收客户端的消息bzero(rbuf,sizeof(rbuf));int res=recv(newfd,rbuf,sizeof(rbuf),0);if(res==0||strcmp(rbuf,"quit")==0){printf("客户端下线\n");break;}printf("[%s:%d]:发送的消息为%s\n",inet_ntoa(cin.sin_addr)\,ntohs(cin.sin_port),rbuf);//发送消息到客户端strcat(rbuf,"*_*");send(newfd,rbuf,sizeof(rbuf),0);}//关闭文件描述符close(sfd);close(newfd);return 0;
}
客户端的代码实现:
#include <myhead.h>
#define IP "192.168.126.91"
#define PORT 9999
int main(int argc, const char *argv[])
{int cfd=-1;if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror("socket error");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(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1){perror("connect error");return -1;}char wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;send(cfd,wbuf,strlen(wbuf),0);if(strcmp("quit",wbuf)==0){printf("客户端下线\n");break;}//接收的数据bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf),0);printf("收到的消息是:%s\n",wbuf);}return 0;
}
运行结果:
2.基于UDP的通信(面向无连接的通信)
服务器端代码实现:
#include <myhead.h>
#define PORT 3333
#define IP "192.168.126.91"
int main(int argc, const char *argv[])
{//创建套接字int sfd=-1;if((sfd=socket(AF_INET,SOCK_DGRAM,0))==-1){perror("socket error");return -1;}printf("sfd=%d\n",sfd);//绑定端口号和ipstruct 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 error");return -1;}//创建对端地址信息结构体struct sockaddr_in cin;socklen_t socklen=sizeof(cin);char rbuf[128]="";while(1){bzero(rbuf,sizeof(rbuf));if(recvfrom(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,&socklen)==-1){perror("recvfrom error");return -1;}if(strcmp(rbuf,"quit")==0){printf("客户端下线\n");break;}printf("收到的消息是:%s\n",rbuf);//给客户端回一个消息//bzero(rbuf,sizeof(rbuf));strcat(rbuf,"*_*");if(sendto(sfd,rbuf,sizeof(rbuf),0,(struct sockaddr*)&cin,socklen)==-1){perror("sendto error");return -1;} }close(sfd);return 0;
}
客户端代码实现:
#include <myhead.h>
#define PORT 3333
#define IP "192.168.126.91"
int main(int argc, const char *argv[])
{//创建套接字int cfd=-1;if((cfd=socket(AF_INET,SOCK_DGRAM,0))==-1){perror("socket error");return -1;}printf("cfd=%d\n",cfd);//创建对端地址信息结构体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 wbuf[128]="";while(1){bzero(wbuf,sizeof(wbuf));printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin);wbuf[strlen(wbuf)-1]=0;if(sendto(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&cin,socklen)==-1){perror("sendto error");return -1;}if(strcmp(wbuf,"quit")==0){printf("客户端下线\n");break;}bzero(wbuf,sizeof(wbuf));if(recvfrom(cfd,wbuf,sizeof(wbuf),0,(struct sockaddr*)&cin,&socklen)==-1){perror("recvfrom error");return -1;}printf("收到服务器的消息为%s\n",wbuf);}close(cfd);return 0;
}
运行结果: