本文主要是介绍网络编程TCP和UDP,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
将TCP的CS模型再敲一遍
TCP服务器
1->创建原始的套接字描述符
2->将原始套接字与主机ip绑定
3->将原始套接字设置监听状态
4->接收客户端连接,获取客户端信息,因为原始套接字被用了,所以创建新的套接字描述符用于客户端通信
5->发送接收信息
6->关闭
#include <myhead.h>#define SERPORT 3456
#define SERIP "192.168.0.102"
#define BACKLOG 5
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_STREAM,0);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地址为本机地址};//定义服务器地址结构体变量if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)//绑定ip{perror("bind");return -1;}printf("绑定成功\n");if(listen(oldfd,BACKLOG) == -1)//监听客户端需求{perror("listen");return -1;}printf("监听成功\n");char buff[1024];struct sockaddr_in cin;//定义获取客户端信息结构体socklen_t cinlen = sizeof(cin);//获取结构体大小int newfd = accept(oldfd,(struct sockaddr *)&cin,&cinlen);//接收请求,创建新的描述符if(newfd == -1){perror("accept");return -1;}printf("%s:%d客户端发来连接请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));while(1){memset(buff,0,sizeof(buff));//int len = read(newfd,buff,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;
}
TCP客户端
1->创建原始套接字描述符
2->可以绑定也可以不绑定,这里就没有绑定了
3->连接服务器,创建结构体用于接收自己的信息,在连接时要带上自己“身份”
4->发送接收信息
5->关闭
#include <myhead.h>#define SERPORT 3456
#define SERIP "192.168.0.102"
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_STREAM,0);if(oldfd == -1){perror("socket");return -1;}//2.绑定(可选)//连接服务器struct sockaddr_in cin = {.sin_family = AF_INET,//使用IPv4通信协议族.sin_port = htons(SERPORT),//设置端口号.sin_addr.s_addr = inet_addr(SERIP)//设置IP地址为本机地址};//定义服务器地址结构体变量if(connect(oldfd,(struct sockaddr *)&cin,sizeof(cin)) == -1){perror("connect");return -1;}printf("连接成功\n");char buff[1024];while(1){memset(buff,0,sizeof(buff));printf("客户端发信息");fgets(buff,sizeof(buff),stdin);buff[strlen(buff)-1] = '\0';send(oldfd,buff,sizeof(buff),0);recv(oldfd,buff,sizeof(buff),0);printf("收到服务器信息%s\n",buff);}close(oldfd);return 0;
}
UDP服务器中,使用connect函数,实现唯一的客户端与服务器通话
UDP服务器
UDP服务器可以不连接(connect),连接后会与指定的客户端通信,不会接收其他客户端信息
代码中当接收到ccc字符串的时候使用connect函数,就只与发ccc字符串的客户端通信了
#include <myhead.h>#define SERPORT 8888
#define SERIP "192.168.0.102"
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_DGRAM,0);if(-1 == oldfd){perror("socket");return -1;}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;}struct sockaddr_in cin;socklen_t cinlen = 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(strcmp(buff,"ccc")==0){connect(oldfd,(struct sockaddr*)&cin,cinlen);}if(len == 0){printf("客户端跑了\n");break;}printf("服务器接收到信息:%s\n",buff);//strcat(buff,"666");//sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr*)&sin,sizeof(sin));//printf("发送成功\n");//memset(buff,0,sizeof(buff));}close(oldfd);return 0;
}
UDP客户端
#include <myhead.h>#define CLIPORT 8888
#define CLIIP "192.168.0.102"
int main(int argc, const char *argv[])
{int oldfd = socket(AF_INET,SOCK_DGRAM,0);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){printf("客户端发信息:");fgets(buff,sizeof(buff),stdin);sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr*)&cin,sizeof(cin));printf("发送成功");}close(oldfd);return 0;
}
TCP和UDP的思维导图
这篇关于网络编程TCP和UDP的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!