2023/3/13 TCP&UDP 服务器 客户端
TCP
客户端
#include "head.h"
#define SER_PORT 3333
#define SER_IP "192.168.31.59"
#define CLI_PORT 4444
#define CLI_IP "192.168.31.59"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd = %d__%d__\n",cfd,__LINE__);
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地址,ifconfig
//获取连接成功的文件描述符
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
//绑定
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//连接到服务器端
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success __%d__\n",__LINE__);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//发送
memset(buf,0,sizeof(buf));
printf("输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = 0;
if(send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
//接收
memset(buf,0,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}else if(0 == res)
{
printf("服务器端关闭\n");
break;
}
printf("buf = %s\n",buf);
}
//关闭所有套接字文件描述
close(cfd);
return 0;
}
服务器端
#include "head.h"
#define SER_PORT 3333
#define SER_IP "192.168.31.59"
#define CLI_PORT 4444
#define CLI_IP "192.168.31.59"
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 __%d__\n",__LINE__);
//设置允许端口被快速复用
int resue = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(resue)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("setsockopt success __%d__\n",__LINE__);
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地址,ifconfig
//绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//将套接字设置为被监听状态
if(listen(sfd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
//获取连接成功的文件描述符
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
socklen_t addrlen = sizeof(cin);
int cfd = -1;
cfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(cfd < 0)
{
ERR_MSG("accpet");
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)
{
//接收
memset(buf,0,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}else if(0 == res)
{
printf("[%s : %d] cfd = %d 客户端断开连接\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd);
break;
}
if(strcmp(buf,"quit") == 0)
{
printf("服务器关闭\n");
break;
}
printf("[%s : %d] newfd = %d buf = %s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),cfd,buf);
//发送
strcat(buf,"*_*\n");
if(send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
//关闭所有套接字文件描述符
close(sfd);
return 0;
}
UDP
客户端
#include "head.h"
#define SER_PORT 6666
#define SER_IP "192.168.31.59"
#define CLI_PORT 5555
#define CLI_IP "192.168.31.59"
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd = %d __%d__\n",cfd,__LINE__);
//填充客户器的地址信息结构体
//真实的地址信息结构体根据地址族执行,AF_INET:man 7 ip
struct sockaddr_in cin;
cin.sin_family = AF_INET;//必须填AF_INET
cin.sin_port = htons(CLI_PORT);//客户端的端口号的网络字节序,1024~49151
cin.sin_addr.s_addr= inet_addr(CLI_IP);//IP地址,ifconfig
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
//填充服务器自身的地址信息结构体
//给connect函数使用,指定要连接到哪个服务器
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET
sin.sin_port = htons(SER_PORT);//服务器端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(SER_IP);//服务器端IP地址的网络字节序,ifconfig
socklen_t addrlen = sizeof(sin);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//发送
memset(buf,0,sizeof(buf));
printf("输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) -1] = 0;
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success __%d__\n",__LINE__);
if(strcmp(buf,"quit") == 0)
{
printf("服务器关闭\n");
break;
}
//接收数据
memset(buf,0,sizeof(buf));
res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
//res = recv(sfd,buf,sizeof(buf));
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]buf = %s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
}
//关闭所有文件
close(cfd);
return 0;
}
服务器端
#include "head.h"
#define SER_PORT 6666
#define SER_IP "192.168.31.59"
#define CLI_PORT 5555
#define CLI_IP "192.168.31.59"
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 __%d__\n",sfd,__LINE__);
//绑定,非必须绑定
//填充客户器的地址信息结构体
//真实的地址信息结构体根据地址族执行,AF_INET:man 7 ip
struct sockaddr_in cin;
cin.sin_family = AF_INET;//必须填AF_INET
cin.sin_port = htons(CLI_PORT);//客户端的端口号的网络字节序,1024~49151
cin.sin_addr.s_addr= inet_addr(CLI_IP);//IP地址,ifconfig
//填充服务器自身的地址信息结构体
//给connect函数使用,指定要连接到哪个服务器
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填AF_INET
sin.sin_port = htons(SER_PORT);//服务器端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(SER_IP);//服务器端IP地址的网络字节序,ifconfig
socklen_t addrlen = sizeof(sin);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//接收数据
memset(buf,0,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
//res = recv(sfd,buf,sizeof(buf));
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
if(strcmp(buf,"quit") == 0)
{
printf("客户端退出\n");
strcpy(buf,"服务器关闭\n");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success __%d__\n",__LINE__);
break;
}
printf("[%s : %d]buf = %s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
//发送
strcat(buf,"返回给客户端\n");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success __%d__\n",__LINE__);
}
//关闭所有文件
close(sfd);
return 0;
}