1. TCP的服务器 客户端的代码
#include
#define PORT 4396
#define IP "192.168.114.69"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
ERR_MSG("socket");
return -1;
}
printf("sockte success\n");
//定义真实地址信息结构体
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))<0)
{
ERR_MSG("bnd");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听
if((listen(sfd,128))<0)
{
ERR_MSG("listen");
return -1;
}
int nfd;
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
//获取新的套接字
if((nfd=accept(sfd,(struct sockaddr*)&cin,&addrlen))<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d]客户端连接成功 nfd=%d\n",\
inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),\
nfd);
char buf[128];
ssize_t res;
while(1)
{
//清空字符串
bzero(buf,sizeof(buf));
//接受
if((res=recv(nfd,buf,sizeof(buf),0))<0)
{
ERR_MSG("recv");
return -1;
}else if(res==0)
{
printf("[%s:%d]客户端下线 nfd=%d\n",\
inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),\
nfd);
break;
}
printf("[%s:%d] nfd=%d : %s\n",\
inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),\
nfd,buf);
//发送
strcat(buf,"~");
if((send(nfd,buf,sizeof(buf),0))<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
//关闭
close(nfd);
return 0;
}
#include
#define PORT 4396
#define IP "192.168.114.69"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd;
if((cfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
ERR_MSG("socket");
return -1;
}
printf("sockte success\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//连接服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
char buf[128];
ssize_t res;
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入\n");
fgets(buf,sizeof(buf),stdin);
buf[sizeof(buf)-1]=0;
//发送
if(send(cfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success");
bzero(buf,sizeof(buf));
//接受
if(res=(recv(cfd,buf,sizeof(buf),0))<0)
{
ERR_MSG("recv");
return -1;
}else if(res==0)
{
printf("服务器下线 cfd=%d\n",cfd);
break;
}
printf("cfd=%d : %s\n",cfd,buf);
}
//结束
close(cfd);
return 0;
}
2. UDP服务器客户端代码
#include
#define PORT 2200
#define IP "192.168.114.69"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("sockte success\n");
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))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
char buf[128];
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
while(1)
{
//接受
bzero(buf,sizeof(buf));
if(recvfrom(sfd,buf,sizeof(buf),0,\
(struct sockaddr*)&cin,\
&socklen)<0)
{
ERR_MSG("recvfrom");
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 PORT 2200
#define IP "192.168.114.69"
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 success\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
char buf[128];
socklen_t socklen=sizeof(sin);
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入要发送的内容\n");
fgets(buf,sizeof(buf),stdin);
buf[sizeof(buf)-1]=0;
//发送,指定发送的人
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("send success\n");
//接受
bzero(buf,sizeof(buf));
if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&socklen)<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("cfd=%d:%s\n",cfd,buf);
}
//关闭
close(cfd);
return 0;
}