tcp服务器
#include <myhead.h>
#define ERR_MSG(msg) do{\
printf("__%d__\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.10.147"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if (sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口被快速重用
int reuse = 1;
if (setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速被重用成功\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");
//将套接字设置为被动监听
if (listen(sfd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//存储客户端地址信息
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
//有客户端连接后,创建一个新的通信套接字和客户端通信
int newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);
if (newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d 客户端连接成功\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
//定义容器
char buf[128] = "";
ssize_t res = 0;
while (1)
{
//接受数据
bzero(buf,sizeof(buf));
res = recv(newfd,buf,sizeof(buf),0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
else if (0 == res)
{
printf("[%s : %d] newfd = %d : 客户端下线\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
break;
}
else
{
printf("[%s : %d] newfd = %d : %s\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
}
//发送数据
strcat(buf,"*_*");
if (send(newfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
close(newfd);
//关闭文件描述符
if (close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
tcp客户端
#include <myhead.h>
#define ERR_MSG(msg) do{\
printf("__%d__\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.10.147"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if (cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口被快速重用
int reuse = 1;
if (setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速被重用成功\n");
//绑定服务器的IP和端口 不绑定自动分配
//填充服务器的地址信息结构体,让connect连接
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 = 0;
while (1)
{
//发送数据
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
if (send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
//接受数据
bzero(buf,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
else if (0 == res)
{
printf("cfd = %d 服务器下线\n",cfd);
break;
}
else
{
printf("cfd = %d : %s\n",cfd,buf);
}
}
//关闭文件描述符
if (close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
让红机械臂转到16度
#include <myhead.h>
#define ERR_MSG(msg) do{\
printf("__%d__\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.10.125"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if (cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口被快速重用
int reuse = 1;
if (setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速被重用成功\n");
//绑定服务器的IP和端口 不绑定自动分配
//填充服务器的地址信息结构体,让connect连接
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] = {0xff,0x02,0x00,0x10,0xff};
//发送数据
if (send(cfd,buf,sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
//关闭文件描述符
if (close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
wasd移动机械臂
#include <myhead.h>
#define ERR_MSG(msg) do{\
printf("__%d__\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.10.125"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd = socket(AF_INET,SOCK_STREAM,0);
if (cfd < 0)
{
ERR_MSG("socket");
return -1;
}
//允许端口被快速重用
int reuse = 1;
if (setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速被重用成功\n");
//绑定服务器的IP和端口 不绑定自动分配
//填充服务器的地址信息结构体,让connect连接
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 red[5] = {0xff,0x02,0x00,0,0xff};
char blue[5] = {0xff,0x02,0x01,0,0xff};
//初始化机械臂
if (send(cfd,blue,sizeof(blue),0) < 0)
{
ERR_MSG("send");
return -1;
}
if (send(cfd,red,sizeof(red),0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("初始化成功\n");
char ch;
//用wasd控制红蓝机械臂
while (1)
{
scanf(" %c",&ch);
switch(ch)
{
case 'w':
if (red[3] >= 90)
{
red[3] = 90;
printf("红色机械臂达到90度无法再增加\n");
}
else
{
red[3]++;
printf("红色机械臂增加1度\n");
}
if (send(cfd,red,sizeof(red),0) < 0)
{
ERR_MSG("send");
return -1;
}
getchar();
break;
case 's':
if (red[3] <= -90)
{
red[3] = -90;
printf("红色机械臂达到-90度无法再减少\n");
}
else
{
red[3]--;
printf("红色机械臂减少1度\n");
}
if (send(cfd,red,sizeof(red),0) < 0)
{
ERR_MSG("send");
return -1;
}
getchar();
break;
case 'a':
if (blue[3] >= 180)
{
blue[3] = 180;
printf("蓝色机械臂达到180度无法再增加\n");
}
else
{
blue[3]++;
printf("蓝色机械臂增加1度\n");
}
if (send(cfd,blue,sizeof(blue),0) < 0)
{
ERR_MSG("send");
return -1;
}
getchar();
break;
case 'd':
if (blue[3] <= 0)
{
blue[3] = 0;
printf("蓝色机械臂达到0度无法再减少\n");
}
else
{
blue[3]--;
printf("蓝色机械臂减少1度\n");
}
if (send(cfd,blue,sizeof(blue),0) < 0)
{
ERR_MSG("send");
return -1;
}
getchar();
break;
default : printf("无效操作\n");break;
}
printf("当前红色机械臂度数:%d\n当前蓝色机械臂度数:%d\n",red[3],blue[3]);
}
//关闭文件描述符
if (close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}