TCP
#include "head.h"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sfd)
{
ERR_MSG("socket");
return -1;
}
//允许端口快速被复用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//填充通用地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(6789);
sin.sin_addr.s_addr = inet_addr("192.168.31.69");
//绑定服务器的ip和端口
if(-1 == bind(sfd,(struct sockaddr*)&sin,sizeof(sin)))
{
ERR_MSG("bind");
return -1;
}
//将套接字设置为被动监听状态
if(-1 == listen(sfd,128))
{
ERR_MSG("listen");
return -1;
}
//定义结构体存储获取到的客户端信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//获取连接成功后的套接字
int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(-1 == newfd)
{
ERR_MSG("accept");
return -1;
}
//消息收发
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//读取数据
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if(-1 == res)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
printf("收到信息:%s\n",buf);
//发送数据
strcat(buf,"-wzx");
if(-1 == send(newfd,buf,sizeof(buf),0))
{
ERR_MSG("send");
return -1;
}
}
close(sfd);
close(newfd);
return 0;
}
#include "head.h"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == cfd)
{
ERR_MSG("socket");
return -1;
}
//填充客户端的通用地址信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(8888);
cin.sin_addr.s_addr = inet_addr("192.168.31.69");
socklen_t addrlen = sizeof(cin);
//绑定客户端的ip和端口
if(-1 == bind(cfd,(struct sockaddr*)&cin,addrlen))
{
ERR_MSG("bind");
return -1;
}
//填充服务器的通用地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(6789);
sin.sin_addr.s_addr = inet_addr("192.168.31.69");
addrlen = sizeof(sin);
//连接服务器
if(-1 == connect(cfd,(struct sockaddr*)&sin,addrlen))
{
ERR_MSG("connect");
return -1;
}
//消息收发
char buf[128] = "";
ssize_t res = 0;
while(1)
{
//发送数据
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
res = send(cfd,buf,sizeof(buf),0);
if(-1 == res)
{
ERR_MSG("send");
return -1;
}
else if(0 == strcmp(buf,"quit"))
{
printf("客户端退出\n");
break;
}
//接收数据
bzero(buf,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(-1 == res)
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
printf("收到应答:%s\n",buf);
}
close(cfd);
return 0;
}
UDP
#include "head.h"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(-1 == sfd)
{
ERR_MSG("socket");
return -1;
}
//填充通用地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.31.69");
socklen_t addrlen = sizeof(sin);
//绑定ip地址和端口号
if(-1 == bind(sfd,(struct sockaddr*)&sin,addrlen))
{
ERR_MSG("bind");
return -1;
}
//消息收发
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in cin;
addrlen = sizeof(cin);
while(1)
{
//接收消息
bzero(buf,sizeof(buf));
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(-1 == res)
{
ERR_MSG("recvfrom");
return -1;
}
else if(0 == res)
{
printf("对端关闭\n");
break;
}
printf("收到信息:%s\n",buf);
//发送消息
strcat(buf,"-wzx");
if(-1 == sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,addrlen))
{
ERR_MSG("sendto");
return -1;
}
}
close(sfd);
return 0;
}
#include "head.h"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(-1 == cfd)
{
ERR_MSG("socket");
return -1;
}
//填充通用地址信息结构体
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(6789);
cin.sin_addr.s_addr = inet_addr("192.168.31.69");
socklen_t addrlen = sizeof(cin);
//绑定ip地址和端口号
if(-1 == bind(cfd,(struct sockaddr*)&cin,addrlen))
{
ERR_MSG("bind");
return -1;
}
//消息收发
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.31.69");
addrlen = sizeof(sin);
while(1)
{
//发送消息
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
res = sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,addrlen);
if(-1 == res)
{
ERR_MSG("sendto");
return -1;
}
else if(strcmp(buf,"quit") == 0)
{
printf("客户端关闭\n");
break;
}
//接收消息
if(-1 == recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen))
{
ERR_MSG("recvfrom");
return -1;
}
printf("收到应答:%s\n",buf);
}
close(cfd);
return 0;
}