1TCP
1.1tcp模型图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c346020bc51f90601c60d0d06df75224.png)
1.2tcp搭建
1.2.1 服务器搭建
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0);
#define IP "192.168.31.111"
#define PORT 2222
int main(int argc, const char *argv[])
{
int sfd = socket (AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d\n",sfd);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT) ;
sin.sin_addr.s_addr = inet_addr(IP);
if(connect(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("[%s:%d] sfd = %d 服务器连接成功\n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),sfd);
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
strcat(buf,"*_*");
if(send(sfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success");
res=recv(sfd,buf,sizeof(buf),0);
if(res< 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
fprintf(stderr,"[%s:%d] sfd = %d 服务器下线\n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),sfd);
break;
}
printf("[%s :%d] buf=%s\n",inet_ntoa(sin.sin_addr),\
ntohs(sin.sin_port),buf);
}
close(sfd);
return -1;
}
1.2.2用户端搭建
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/types.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0);
#define IP "192.168.31.111"
#define PORT 2222
int main(int argc, const char *argv[])
{
int sfd = socket (AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d\n",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)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定成功\n");
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int cfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);
if(cfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] cfd = %d 客户端连接成功\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
res=recv(cfd,buf,sizeof(buf),0);
if(res< 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
fprintf(stderr,"[%s:%d] cfd = %d 标准客户端下线\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
break;
}
printf("[%s :%d] buf=%s\n",inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),buf);
strcat(buf,"*_*");
if(send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success");
}
close(sfd);
close(cfd);
return -1;
}
1.2.3结果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/49a70cdde445072713ce305e4c4972b5.png)
2.UDP
2.1 UDP模型图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/80e67e66d007b7f17bb509357fb9e190.png)
2.2.UDP搭建
2.2.1UDP服务器搭建
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define PORT 2222
#define IP "192.168.5.11"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d \n",sfd);
int resue = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
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))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128]="";
ssize_t res =-1;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr *)&cin,\
&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] sfd=%d %s \n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),sfd,buf);
strcat(buf,"*_*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,addrlen) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
}
close(sfd);
return 0;
}
2.2.2UDP客户端搭建
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d ",__LINE__);\
perror(msg);\
}while(0)
#define PORT 2222
#define IP "192.168.5.11"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d \n",sfd);
int resue = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
socklen_t addrlen = sizeof(sin);
sin.sin_family = AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
char buf[128]="";
ssize_t res =-1;
while(1)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
strcat(buf,"*_*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,addrlen) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
bzero(buf,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&sin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] sfd=%d %s \n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),sfd,buf);
}
close(sfd);
return 0;
}
2.2.3结果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4765ee4c3c6c847a35053e93bb284fbd.png)