作业
1、将TCP的CS模型再敲一遍
tcpserver.c
#include <myhead.h>
#define SERPORT 1111
#define SERIP "192.168.58.128"
#define BACKLOG 40
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_STREAM,0);//1、产生一个原始套接字文件描述符if(oldfd==-1){perror("socket");return -1;}printf("旧的套接字创建成功\n");//2、绑定主机的IP和端口号struct sockaddr_in sin= {.sin_family = AF_INET, //使用IPv4通信协议族.sin_port = htons(SERPORT), //设置端口号.sin_addr.s_addr = inet_addr(SERIP) //设置IP地址为本机IP};//定义服务器地址结构体变量if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)//2、绑定ip{perror("bind");return -1;}printf("绑定成功\n");if(listen(oldfd,BACKLOG)==-1)//3、监听客户端请求{perror("listen");return -1;}printf("监听成功\n");//使用新描述符通信char buff[1024] = "";int newfd;struct sockaddr_in cin;//定义获取客户端信息的结构体socklen_t cinlen = sizeof(cin);//获取结构体大小newfd = accept(oldfd,(struct sockaddr *)&cin,&cinlen);//4、接收请求,创建新的描述符用于通信if(newfd==-1){perror("accept");return -1;}printf("%s:%d客户端发来连接请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));while(1)//5、收发消息{//int len = read(newfd,buff,sizeof(buff));memset(buff,0,sizeof(buff));//清空缓冲区int len = recv(newfd,buff,sizeof(buff),0);printf("读取的信息:%s\n",buff);if(len==0){printf("你客户跑了\n");}strcat(buff,"520");//write(newfd,buff,sizeof(buff));send(newfd,buff,sizeof(buff),0);memset(buff,0,sizeof(buff));//清空缓冲区}close(oldfd);close(newfd);return 0;
}
tcpclient.c
#include <myhead.h>#define CLIPORT 1111
#define CLIIP "192.168.58.128"
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_STREAM,0);//1、创建原始套节字if(oldfd==-1){perror("socket");return -1;}//2、bind可选//3、连接服务器struct sockaddr_in cin = {.sin_family = AF_INET,//IPV4通信.sin_port = htons(CLIPORT),//端口号转为网络字节序.sin_addr.s_addr = inet_addr(CLIIP)//IP地址转为网络字节序};if(connect(oldfd,(struct sockaddr *)&cin,sizeof(cin))==-1){perror("connect");return -1;}printf("连接服务器成功\n");char buff[1024] = "";while(1)//4、收发消息{memset(buff,0,sizeof(buff));printf("客户端发信息:");fgets(buff,sizeof(buff),stdin);send(oldfd,buff,sizeof(buff),0);//阻塞发送recv(oldfd,buff,sizeof(buff),0);//阻塞接收printf("收到服务器的消息:%s\n",buff);}close(oldfd);return 0;
}
2、UDP服务器中,使用connect函数,实现唯一的客户端与服务器通话
udpserver.c
#include <myhead.h>
#define SERPORT 8888
#define SERIP "192.168.58.128"int main(int argc, const char *argv[])
{//1、创建套接字int oldfd = socket(AF_INET,SOCK_DGRAM,0);//SOCK_DGRAM:UDPif(oldfd==-1){perror("socket");return -1;}//2、绑定struct sockaddr_in sin = {.sin_family = AF_INET,.sin_port = htons(SERPORT),.sin_addr.s_addr = inet_addr(SERIP)};if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1){perror("bind");return -1;}//3、收发消息struct sockaddr_in cin;socklen_t cinlen = sizeof(cin);connect(sockfd, (struct sockaddr*)&cin, sizeof(cin));char buff[100];while(1){memset(buff,0,sizeof(buff));int len = recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,&cinlen);//不用管谁发的if(len==0){printf("客户端跑了\n");break;}printf("服务器接收的信息:%s\n",buff);//printf("服务器发消息:");//fgets(buff,sizeof(buff),stdin);//sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,sizeof(cin));//printf("发送成功\n");}close(oldfd);return 0;
}
udpclient.c
#include <myhead.h>
#define CLIPORT 8888
#define CLIIP "192.168.58.128"
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_DGRAM,0);//1、创建套接字if(oldfd ==-1){perror("socket");return -1;}struct sockaddr_in cin = {.sin_family = AF_INET,.sin_port = htons(CLIPORT),.sin_addr.s_addr = inet_addr(CLIIP)};char buff[1024] ="";while(1)//2、收发信息{printf("客户端发消息:");fgets(buff,sizeof(buff),stdin);sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,sizeof(cin));printf("发送成功\n");}close(oldfd);return 0;
}
知识梳理
TCP和UDP通信基础:
基于TCP通信的流程