1. TCP的服务器 客户端的代码
服务器
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.104"
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success! sfd = %d\n",sfd);
//允许端口快速的被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速的被复用成功\n");
//绑定服务器的地址信息 --->必须绑定 bind
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;
}
//将套接字设置为被动监听状态 listen
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success!\n");
//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
int newfd=-1;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("newfd");
return -1;
}
printf("[%s:%d]客户端连接成功 newfd=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
char buf[128]="";
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
//接收
res=recv(newfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("[%s:%d]客户端下线 newfd=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s:%d] @_@:%s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
}
close(newfd);
//关闭文件描述符
if(close(sfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
客户端
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__); \
perror(msg);\
}while(0)
#define PORT 8888 //服务器绑定的端口号
#define IP "192.168.114.104" //服务器绑定的IP
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d\n",cfd);
//绑定客户端的地址信息---》非必须绑定
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//连接指定服务器 connect
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128]="";
ssize_t res=0;
while(1)
{
//清空字符串
bzero(buf,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\n");
//清空字符串
memset(buf,0,sizeof(buf));
//接受
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器下线\n");
break;
}
printf("cfd=%d , %s",cfd,buf);
}
if(close(cfd)<0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
2. UDP服务器客户端代码
服务器
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888
#define IP "192.168.114.104"
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int sfd = socket(AF_INET,SOCK_DGRAM,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; //必须填AF_INET;
sin.sin_port = htons(PORT); //端口号的网络字节序,1024-49151
sin.sin_addr.s_addr = inet_addr(IP);//本机IP, ifconfig
//绑定地址信息结构体
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128]="";
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
while(1)
{
//清空数据
bzero(buf,sizeof(buf));
if(recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen) < 0)
{
ERR_MSG("recv");
return -1;
}
printf("[%s:%d] : %s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
//发送数据----->谁发给我,我发还给谁
strcat(buf, "*_*");
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
close(sfd);
return 0;
}
客户端
#include
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__:", __LINE__); \
perror(msg);\
}while(0)
#define PORT 8888 //服务器绑定的端口号
#define IP "192.168.114.104" //服务器绑定的IP
int main(int argc, const char *argv[])
{
//创建报式套接字 socket
int sfd = socket(AF_INET, SOCK_DGRAM, 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; //必须填AF_INET;
sin.sin_port = htons(PORT); //服务器绑定的端口号
sin.sin_addr.s_addr = inet_addr(IP);//服务器绑定的IP
char buf[128] = "";
ssize_t res = 0;
socklen_t addrlen=sizeof(sin);
while(1)
{
//清空字符串
bzero(buf, sizeof(buf)); //memset
printf("请输入>>> ");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//发送
if(sendto(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
bzero(buf, sizeof(buf)); //memset
//接收
res = recvfrom(sfd, buf, sizeof(buf), 0,(struct sockaddr*)&sin,&addrlen);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线 sfd=%d\n", sfd);
break;
}
printf("sfd=%d : %s\n", sfd, buf);
}
//关闭套接字释放资源
close(sfd);
return 0;
}
3.思维导图