写一个基于TCP协议的客户端来控制RobArm机械臂
#include
#define SER_PORT 8888
#define SER_IP "192.168.122.71"
#define CLI_PORT 6666
#define CLI_IP "192.168.122.36"
int main(int argc, const char *argv[])
{
//1.创建套接字
int cfd = socket(AF_INET,SOCK_STREAM,0/*IPPROTO_TCP*/);
if(cfd==-1)
{
perror("socket error");
return -1;
}
printf("cfd=%d\n",cfd);
//将端口号快速重用函数
int reuse =1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
//2.绑定(随意)
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(CLI_PORT);
cin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success");
//printf("bind success %s %s %d\n",__FILE__,__func__,__LINE__);
//3.连接服务器
//3.1填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
//3.2链接
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("链接成功\n");
//4.收发数据
char bufred[5]={0xff,0x02,0x00,-90,0xff};
char bufblue[5]={0xff,0x02,0x01,0,0xff};
while(1)
{
char ch;
printf("请输入w&a&s&d:");
scanf("%c",&ch);
getchar();
if(ch == 'w')
{
bufred[3]+=5;
if(bufred[3]>90)
{
bufred[3]=90;
continue;
}
}
else if(ch == 's')
{
bufred[3]-=5;
if(bufred[3]<-90)
{
bufred[3]=-90;
continue;
}
}
else if(ch == 'a')
{
bufblue[3]-=5;
if(bufblue[3]<0)
{
bufblue[3]=0;
continue;
}
}
else if(ch == 'd')
{
bufblue[3]+=5;
if(bufblue[3]>180)
{
bufblue[3]=180;
continue;
}
}else{
puts("输入有误");
}
//发送给服务器
send(cfd,bufred,sizeof(bufred),0);
send(cfd,bufblue,sizeof(bufblue),0);
printf("发送成功\n");
}
//5.关闭套接字
close(cfd);
return 0;
}
再次完成TCP网络通信
#include
#define SER_PORT 8888
#define SER_IP "192.168.122.36"
int main(int argc, const char *argv[])
{
//1.创建套接字
int sfd = socket(AF_INET,SOCK_STREAM,0/*IPPROTO_TCP*/);
if(sfd==-1)
{
perror("socket error");
return -1;
}
printf("sfd=%d\n",sfd);
//将端口号快速重用函数
int reuse =1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
printf("端口号快速重用成功\n");
//2.给套接字绑定IP和端口号
//2.1填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
//2.2绑定
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success %s %s %d\n",__FILE__,__func__,__LINE__);
//3.将套接字设置成监听状态
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
printf("listen success %s %s %d\n",__FILE__,__func__,__LINE__);
//4.阻塞等待客户端的链接请求
//4.1定义容器接收客户端的地址信息
struct sockaddr_in cin; //用于接收地址信息
socklen_t socklen = sizeof(cin); //用于接收地址信息的大小
int newfd=-1;
if((newfd =accept(sfd,(struct sockaddr*)&cin,&socklen))==-1)
{
perror("accept error");
return -1;
}
printf("您有一个新的客户端[%s:%d]发来连接请求 success %s %s %d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),__FILE__,__func__,__LINE__);
//5.跟客户端进行通信
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
int res=recv(newfd,buf,sizeof(buf),0);
if(res==0)
{
puts("客户端已经下线");
break;
}
printf("[%s:%d] : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//给客户端发送消息
strcat(buf,"=-=");
send(newfd,buf,sizeof(buf),0);
printf("发送成功\n");
}
//6.关闭套接字
close(newfd);
close(sfd);
return 0;
}
#include
#define SER_PORT 8888
#define SER_IP "192.168.122.36"
#define CLI_PORT 6666
#define CLI_IP "192.168.122.36"
int main(int argc, const char *argv[])
{
//1.创建套接字
int cfd = socket(AF_INET,SOCK_STREAM,0/*IPPROTO_TCP*/);
if(cfd==-1)
{
perror("socket error");
return -1;
}
printf("cfd=%d\n",cfd);
//2.绑定(随意)
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(CLI_PORT);
cin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success %s %s %d\n",__FILE__,__func__,__LINE__);
//3.连接服务器
//3.1填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
//3.2链接
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("");
return -1;
}
printf("链接成功\n");
//4.收发数据
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
//发送给服务器
send(cfd,buf,sizeof(buf),0);
printf("发送成功\n");
if(strcmp(buf,"quit")==0)
{
break;
}
//接收服务器发来的消息
recv(cfd,buf,sizeof(buf),0);
printf("[%s:%d]:%s\n",SER_IP,SER_PORT,buf);
}
//5.关闭套接字
close(cfd);
return 0;
}