一、思维导图
二、TCP通信
//服务器
#include<myhead.h>#define SER_PORT 8888 //服务器端口号
#define SER_IP "192.168.117.103" //服务器IP
int main(int argc, const char *argv[])
{//1、创建一个套接字int sfd = -1;sfd = socket(AF_INET,SOCK_STREAM,0);//参数1:表示创建的是网络通信的套接字//参数2:表示使用的是TCP通信协议//参数3:参数2指定了协议,参数3填0即可if(sfd == -1){perror("socket error");return -1;}printf("%d success sfd = %d\n",__LINE__,sfd); //3//设置端口号快速重用int reuse = 1;if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1){perror("setsockopt error");return -1;}printf("端口号快速重用成功\n");//2、绑定IP和端口号//2.1填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //IP地址//2.2绑定if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1){perror("bind error");return -1;}printf("%d bind success\n",__LINE__);//3、将套接字设置成被监听状态if(listen(sfd,128) == -1){perror("listen error");return -1;}//4、阻塞等待客户端的连接请求int newfd = -1;//定义结构体变量接受客户端地址信息结构体struct sockaddr_in cin;//接收客户端信息结构体socklen_t addrlen = sizeof(cin);//接收客户端结构体大小if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) == -1){perror("accept error");return -1;}printf("[%s %d]请求连接\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));//5、收发数据char rbuf[128] = "";//接收客户端发送的信息while(1){//将容器清空bzero(rbuf,sizeof(rbuf));//memset(rbuf,0,sizeof(rbuf));//从套接字中读取数据int res = recv(newfd,rbuf,sizeof(rbuf)-1,0);if(res == 0){printf("客户端已下线\n");break;}printf("[%s %d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);//加个收到再回回去strcat(rbuf," <Got it>!");send(newfd,rbuf,strlen(rbuf),0);printf("回复成功\n"); }//6、关闭服务器close(newfd);close(sfd);return 0;
}
//客户端
#include<myhead.h>
#define SER_PORT 8888
#define SER_IP "192.168.117.103"
#define CLI_PORT 9999
#define CLI_IP "192.168.117.103"int main(int argc, const char *argv[])
{//1、创建用于连接的客户端套接字int cfd = socket(AF_INET,SOCK_STREAM,0);if(cfd == -1){perror("socket error");return -1;}printf("socket sucess cfd = %d\n",cfd); //3//设置端口号快速重用int reuse = 1;if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1){perror("socket error");return -1;}printf("端口号快速重用成功\n");//2、绑定端口号和IP地址(非必须)//2.1填充客户端地址信息结构体struct sockaddr_in cin;cin.sin_family = AF_INET;cin.sin_port = htons(CLI_PORT);cin.sin_addr.s_addr = inet_addr(CLI_IP);//2.2、绑定端口号和IPif(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、连接服务器//3.1、填充要连接服务器的地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET; //地址族sin.sin_port = htons(SER_PORT); //服务器端口号sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器IP地址//3.2、连接服务器if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("connect error");return -1;}printf("连接成功\n");//4、收发数据char wbuf[128] = "";while(1){printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin); //从终端上获取一个字符串wbuf[strlen(wbuf)-1] = '\0'; //将换行换成 '\0'//判断输入的字符串值if(strcmp(wbuf,"quit") == 0){break;}//将数据发送给服务器send(cfd,wbuf,strlen(wbuf),0);//将字符数据清空bzero(wbuf,sizeof(wbuf));recv(cfd,wbuf,sizeof(wbuf)-1,0);printf("收到的服务器消息为:%s\n",wbuf);}//5、关闭套接字close(cfd);return 0;
}
客户端连接服务器
三、UDP通信
//服务器
#include<myhead.h>
#define SER_PORT 6666
#define SER_IP "192.168.117.103"int main(int argc, const char *argv[])
{//1、创建用于通信的套接字文件描述符int sfd = socket(AF_INET,SOCK_DGRAM,0);if(sfd == -1){perror("socket error");return -1;}printf("socket success sfd = %d\n",sfd); //3//设置端口号快速重用int reuse = 1;if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1){perror("setsockopt error");return -1;}printf("端口号快速重用成功\n");//2、绑定IP地址和端口号//2.1、填充地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);//2.2、绑定if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1){perror("bind error");return -1;}//3、收发数据char rbuf[128] = "";//定义接收对端地址信息结构体的容器struct sockaddr_in cin;socklen_t addrlen = sizeof(cin);while(1){//清空容器bzero(rbuf,sizeof(rbuf));//接收数据recvfrom(sfd,rbuf,sizeof(rbuf)-1,0,(struct sockaddr*)&cin,&addrlen);printf("[%s:%d] : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);//回复strcat(rbuf," <Got it!>");if(sendto(sfd,rbuf,strlen(rbuf),0,(struct sockaddr*)&cin,sizeof(cin)) == -1){perror("write error");return -1;}printf("回复成功\n");}//4、关闭套接字close(sfd);return 0;
}
//客户端
#include<myhead.h>
#define SER_PORT 6666
#define SER_IP "192.168.117.103"
#define CLI_PORT 7777
#define CLI_IP "192.168.117.103"
int main(int argc, const char *argv[])
{//1、创建用于通信的套接字文件描述符int cfd = socket(AF_INET,SOCK_DGRAM,0);if(cfd == -1){perror("socket error");return -1;}printf("socket success cfd = %d\n",cfd);//2、绑定IP地址和端口号//2.1填充客户端地址信息结构体struct sockaddr_in cin;cin.sin_family = AF_INET;cin.sin_port = htons(CLI_PORT);cin.sin_addr.s_addr = inet_addr(CLI_IP);//2.2、绑定if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1){perror("bind error");return -1;}printf("bind success\n");//3、数据收发char wbuf[128] = "";//填充服务器端地址信息结构体struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);while(1){printf("请输入>>>");fgets(wbuf,sizeof(wbuf),stdin); //从终端获取一个字符串wbuf[strlen(wbuf)-1] = 0; //将换行换成'\0'//判断输入的是否为退出if(strcmp(wbuf,"quit") ==0){break;}//将数据发送给服务器sendto(cfd,wbuf,strlen(wbuf),0,(struct sockaddr*)&sin,sizeof(sin));printf("发送成功\n");//接收服务器发送来的消息bzero(wbuf,sizeof(wbuf)); //清零read(cfd,wbuf,sizeof(wbuf)-1);printf("收到服务器的消息为:%s\n",wbuf);}//4、关闭套接字close(cfd);return 0;
}
客户端连接服务器