TCP机械臂测试
#include
#define SERIP "192.168.125.181"
#define IP "192.168.125.83"
#define SERPORT 8888
#define PORT 8080
int main(int argc, const char *argv[])
{
//创建用于通信的套接字文件,返回文件描述符
int cfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(cfd == -1)
{
perror("socket error");
return -1;
}
//绑定
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(PORT);
cin.sin_addr.s_addr = inet_addr(IP);
if(bind(cfd,(struct sockaddr *)&cin,sizeof(cin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_\n",__LINE__);
//连接服务器
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SERPORT);
sin.sin_addr.s_addr = inet_addr(SERIP);
if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("connect success _%d_\n",__LINE__);
//通信
char red_buf[5] = {0xff,0x02,0x00,0x00,0xff};
unsigned char blue_buf[5] = {0xff,0x02,0x01,0x5a,0xff};
char key = 0;
while(1)
{
scanf("%c",&key);
getchar();
switch(key)
{
case 'a':
case 'A':
{
red_buf[3] += 2;
if(red_buf[3] >90)
red_buf[3] = 90;
if(send(cfd,red_buf,sizeof(red_buf),0) == -1)
{
perror("send error");
return -1;
}
break;
}
case 'd':
case 'D':
{
red_buf[3] -= 2;
if(red_buf[3] < -90)
red_buf[3] = -90;
if(send(cfd,red_buf,sizeof(red_buf),0) == -1)
{
perror("send error");
return -1;
}
break;
}
case 'w':
case 'W':
{
blue_buf[3] += 2;
if(blue_buf[3] > 180)
blue_buf[3] = 180;
if(send(cfd,blue_buf,sizeof(blue_buf),0) == -1)
{
perror("send error");
return -1;
}
break;
}
case 's':
case 'S':
{
if(blue_buf[3] < 2)
{
blue_buf[3] = 0;
}
else
{
blue_buf[3] -= 2;
}
if(send(cfd,blue_buf,sizeof(blue_buf),0) == -1)
{
perror("send error");
return -1;
}
break;
}
case 'q':
case 'Q':
{
break;
}
default:
{
printf("输入有误,请重新输入\n");
break;
}
}
if(key == 'q'||key == 'Q')
break;
}
//关闭套接字文件描述符
close(cfd);
return 0;
}
TCP基础通信模型服务器端
#include
#define PORT 8888
#define IP "192.168.125.83"
int main(int argc, const char *argv[])
{
//创建套接字文件,返回用于连接的文件描述符
int sfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sfd == -1)
{
perror("socket error");
return -1;
}
//将端口号快速重用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
{
perror("reuse error");
return -1;
}
//填充地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定服务器地址端口号到套接字
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success: %s %s %d\n",__FILE__,__func__,__LINE__);
//将套接字设置成被动监听状态
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
//阻塞等待接收客户端连接请求,创建用于通信的套接字
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
int newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("[%s : %d]连接成功:\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
//跟客户端进行通信
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
int res = recv(newfd,buf,sizeof(buf),0);
if(res == 0)
{
printf("客户端已下线\n");
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");
}
//关闭套接字
close(newfd);
close(sfd);
return 0;
}
TCP基础通信模型客户端
#include
#define SERIP "192.168.125.83"
#define IP "192.168.125.83"
#define SERPORT 8888
#define PORT 8080
int main(int argc, const char *argv[])
{
//创建用于通信的套接字文件,返回文件描述符
int cfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(cfd == -1)
{
perror("socket error");
return -1;
}
//可以绑定,也可以不绑定
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(PORT);
cin.sin_addr.s_addr = inet_addr(IP);
if(bind(cfd,(struct sockaddr *)&cin,sizeof(cin)) == -1)
{
perror("bind error");
return -1;
}
//连接服务器
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SERPORT);
sin.sin_addr.s_addr = inet_addr(SERIP);
if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
//通信
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);
if(strcmp(buf,"quit") == 0)
break;
recv(cfd,buf,sizeof(buf),0);
printf("收到服务器消息:%s\n",buf);
}
//关闭套接字文件描述符
close(cfd);
return 0;
}