1.TCP通信
服务器端
#include <myhead.h>
#define SERPORT 6666
#define SERIP "192.168.0.136"
#define BACKLOG 5
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//绑定
struct sockaddr_in sin={
.sin_family=AF_INET,
.sin_port=htons(SERPORT),
.sin_addr.s_addr=inet_addr(SERIP)
};
if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind");
return -1;
}
//监听
if(listen(oldfd,BACKLOG)==-1)
{
perror("listen");
return -1;
}
//结束信息
int newfd;
struct sockaddr_in cin;
socklen_t cinlen =sizeof(cin);
newfd=accept(oldfd,(struct sockaddr *)&cin,&cinlen);
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("%s:%d客户端发来连接请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
char buff[1024];
while(1)
{
memset(buff,0,sizeof(buff));
int len=recv(newfd,buff,sizeof(buff),0);
printf("收到的信息:%s\n",buff);
memset(buff,0,sizeof(buff));
}
close(oldfd);
close(newfd);
return 0;
}
客户端:
#include <myhead.h>
#define SERPORT 6666
#define SERIP "192.168.0.136"
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
struct sockaddr_in cin={
.sin_family=AF_INET,
.sin_port=htons(SERPORT),
.sin_addr.s_addr=inet_addr(SERIP)
};
if(connect(oldfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("connect");
return -1;
}
char buff[1024];
while(1)
{
memset(buff,0,sizeof(buff));
printf("客户端发信息:");
fgets(buff,sizeof(buff),stdin);
send(oldfd,buff,sizeof(buff),0);//阻塞发送
}
close(oldfd);
return 0;
}
2.UDP服务器中,使用connect函数,实现唯一的客户端与服务器通话
服务器端
#include <myhead.h>
#define SERPORT 6666
#define SERIP "192.168.0.136"
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//绑定
struct sockaddr_in sin={
.sin_family=AF_INET,
.sin_port=htons(SERPORT),
.sin_addr.s_addr=inet_addr(SERIP),
};
if(bind(oldfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind");
return -1;
}
//收消息
struct sockaddr_in cin;
socklen_t cinlen = sizeof(cin);
char buff[1024];
while(1)
{
memset(buff,0,sizeof(buff));
int len =recvfrom(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,&cinlen);
if(len==0)
{
break;
}
if(strcmp(buff,"only\n")==0)
{
//sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,sizeof(cin));
if(connect(oldfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("connect");
return -1;
}
printf("进入唯一通信\n");
}
printf("服务器接收到的信息:%s\n",buff);
}
return 0;
}
客户端
#include <myhead.h>
#define CLIPOTR 6666
#define CLIIP "192.168.0.136"
int main(int argc, const char *argv[])
{
int oldfd=socket(AF_INET,SOCK_DGRAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
struct sockaddr_in cin={
.sin_family=AF_INET,
.sin_port=htons(CLIPOTR),
.sin_addr.s_addr=inet_addr(CLIIP)
};
char buff[1024];
while(1)
{
printf("客户端发送消息:");
fgets(buff,sizeof(buff),stdin);
sendto(oldfd,buff,sizeof(buff),0,(struct sockaddr *)&cin,sizeof(cin));
printf("发送成功\n");
}
return 0;
}
标签:UDP,int,cin,TCP,收发,oldfd,sizeof,sin,buff
From: https://blog.csdn.net/m0_58572142/article/details/141871520