1. TCP的服务器
#include
#define PORT 4399 //端口号,范围1024~49151
#define IP "192.168.114.47"//本机IP ifconfig
int main(int argc, const char *argv[])
{
//1、创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket creat 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");
//2、绑定服务器地址信息 必须绑定 bind
//真实的地址信息结构体根据地址族指定 AF_INET :man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);//端口号网络字节序
sin.sin_addr.s_addr = inet_addr(IP);//本机IP
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1 )
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//3、将套接字设置为被动监听状态 listen
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//4、获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
int newfd = -1;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
// accept(sfd, NULL, NULL);
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));
//5、接收
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] newfd=%d : %s\n",\
inet_ntoa(cin.sin_addr), \
ntohs(cin.sin_port), newfd, buf);
//6、发送
strcat(buf, "*_*");
if(send(newfd, buf, strlen(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
//7、关闭文件描述符
if(close(newfd) < 0)
{
ERR_MSG("close");
return -1;
}
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
客户端的代码
#include
#define PORT 4399 //端口号,范围1024~49151
#define IP "192.168.114.47"//本机IP ifconfig
int main(int argc, const char *argv[])
{
//1、创建流式套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket creat success cfd=%d\n",cfd);
//2、绑定客户端的地址信息-----非必须绑定
//操作系统会自动给客户端绑定本机IP和随机端口
//真实的地址信息结构体根据地址族指定 AF_INET :man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);//服务器绑定的端口号
sin.sin_addr.s_addr = inet_addr(IP);//服务器绑定的IP
//3、连接服务器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("请输入>>> ");
//4、发送
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
if(send(cfd, buf, strlen(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
bzero(buf, sizeof(buf));
//5、接收
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线 cfd = %d\n", cfd);
break;
}
printf("cfd = %d : %s\n", cfd, buf);
}
//6、关闭文件描述符
if(close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
2. UDP服务器代码
#include
#define PORT 4399 //端口号,范围1024~49151
#define IP "192.168.114.47"//本机IP ifconfig
int main(int argc, const char *argv[])
{
//1、创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket creat success sfd=%d\n",sfd);
//填充服务器的地址信息结构体,
//真实的地址信息结构体根据地址族指定,AF_INET
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);//端口号网络字节序1024-49151
sin.sin_addr.s_addr = inet_addr(IP);//本机IP
//绑定地址信息-----bind
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));
int res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
if(0 == strcmp(buf,"quit"))
{
printf("[%s:%d]的客户端已下线\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));
}
if( res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
else if(0 == res)
{
printf("服务器已下线\n");
break;
}
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;
}
UDP客户端代码
#include
#define PORT 4399 //目标端口号,范围1024~49151
#define IP "192.168.114.47"//目标IP
int main(int argc, const char *argv[])
{
//1、创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket creat success sfd=%d\n",sfd);
//填充服务器的地址信息结构体,
//真实的地址信息结构体根据地址族指定,AF_INET
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);//目标的端口号网络字节序1024-49151
sin.sin_addr.s_addr = inet_addr(IP);//目标IP
//绑定地址信息-----bind(非必须绑定)
char buf[128]="";
socklen_t addrlen = sizeof(sin);
while(1)
{
bzero(buf,sizeof(buf));
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("sendto");
return -1;
}
if(strcmp(buf,"quit") == 0)
{
printf("客户端已下线\n");
break;
}
printf("sendto success\n");
bzero(buf,sizeof(buf));
//接收数据
int res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
else if(0 == res)
{
printf("服务器已下线\n");
break;
}
printf("[%s:%d] : %s\n",inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),buf);
}
//关闭
close(sfd);
return 0;
}