TCP服务器
#include
#define ERR_MSG(msg) do{fprintf(stderr,"__%d__\n",__LINE__);perror(msg);}while(0)
#define PORT 8888 //端口号,范围1024-49151
#define IP "192.168.115.105" //服务器的ip,点分十进制
int main(int argc, const char *argv[])
{
//创建套接字文件socket
int sfd;
if((sfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success fd = %d\n",sfd);
//填充地址结构体,给bind使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //主机字节序转化成网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //本机IP
//允许端口快速的被复用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速的被复用成功\n"); //与服务器IP和端口进行绑定bind
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字转化为监听状态listen
if(listen(sfd,128) == -1)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
int newfd = -1;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//获得连接成功后的套接字accept
if((newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen)) < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d]客户端连接成功 newfd = %d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
char buf[128] = "";
ssize_t sum=0;
while(1)
{
bzero(buf,sizeof(buf));
//接收
sum=recv(newfd,buf,sizeof(buf),0);
if(sum == -1)
{
ERR_MSG("recv");
return -1;
}
else if(sum == 0)
{
printf("[%s:%d] 客户端下线 newfd = %d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
printf("[%s:%d]newfd = %d : %s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
//发送
strcat(buf,">_<");
if(send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
//关闭文件
close(newfd);
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
printf("close success\n");
return 0;
}
TCP客户端:
#include
#define ERR_MSG(msg) do{fprintf(stderr,"__%d__\n",__LINE__);perror(msg);}while(0)
#define IP "192.168.115.105"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建套接字
int cfd;
if((cfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success fd = %d\n",cfd);
//非必须绑定ip和端口号bind
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;
}
char buf[128] = "";
while(1)
{
//发送
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
bzero(buf,sizeof(buf));
//接收
ssize_t num = recv(cfd,buf,sizeof(buf),0);
if(num < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == num)
{
printf("服务器已下线 fd = %d\n",cfd);
break;
}
printf("cfd = %d: %s\n",cfd,buf);
}
//关闭文件描述符
if(close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
UDP服务器:
#include
#define ERR_MSG(msg) do{fprintf(stderr,"__%d__\n",__LINE__);perror(msg);}while(0)
#define IP "192.168.115.105"
#define PORT 2222
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd;
if((sfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success\n");
//绑定IP地址和端口号,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;
}
printf("bind success\n");
char buf[128] = "";
struct sockaddr_in cin;
socklen_t sum = sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
//接收
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&sum) < 0)
{
ERR_MSG("recvfrom");
break;
}
printf("recvfrom success sfd = %d : %s",sfd,buf);
strcat(buf,">_<");
//发送
sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sum);
}
//关闭文件
if(close(sfd) < 0)
{
return -1;
}
return 0;
}
UDP客户端:
#include
#define ERR_MSG(msg) do{fprintf(stderr,"__%d__\n",__LINE__);perror(msg);}while(0)
#define IP "192.168.115.105"
#define PORT 2222
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd;
if((cfd = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success fd = %d\n",cfd);
//绑定IP地址和端口号,bind,可以不绑定
char buf[128] = "";
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(PORT);
cin.sin_addr.s_addr = inet_addr(IP);
socklen_t sum = sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
//发送
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]= '\0';
sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sum);
bzero(buf,sizeof(buf));
//接收
if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&sum) < 0)
{
ERR_MSG("recvfrom");
break;
}
printf("recvfrom success sfd = %d : %s\n",cfd,buf);
}
//关闭文件
if(close(cfd) < 0)
{
return -1;
}
return 0;
}