1.编写TCP服务器端与客户端
tcpSer.c
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.18.129"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1.创建流式套接字
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");
//2.填充服务器的地址信息结构体 man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //AF_INET
sin.sin_port = htons(PORT); //端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //本机IP的网络字节序
//2.绑定服务器的IP和端口 (必须绑定)
if(bind(sfd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//3.将套接字设置为被动监听状态,监听是否有客户端连接
if(listen(sfd, 128) < 0){
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin; //存储客户端地址信息
socklen_t addlen = sizeof(cin);
//4.阻塞函数,阻塞等待客户端连接成功;从已完成连接的队列头中获取一个客户端信息,生成一个新的文件描述符
//这个新的文件描述符,才是与客户端通信的文件描述符
int newfd = accept(sfd, (struct sockaddr*)&cin, &addlen);
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){
//清空buf
bzero(buf, sizeof(buf));
//5.接收数据
res = recv(newfd, buf, sizeof(buf), 0);
if(res < 0){
ERR_MSG("recv");
return -1;
}else if(res == 0){
printf("[%s : %d] newfd = %d 客户端下线\n",\
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
break;
}
printf("[%s : %d] newfd = %d : %s\n",\
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);
//6.发送数据
strcat(buf, "*_*");
if(send(newfd, buf, sizeof(buf), 0) < 0){
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
close(newfd);
//文件描述符
if(close(sfd) < 0){
ERR_MSG("close");
return -1;
}
return 0;
}
tcpCli.c
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.18.129"
#define PORT 6666
int main(int argc, const char *argv[])
{
//1.创建流式套接字
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");
//填充服务器的地址信息结构体,给connect函数使用
//2.要连接哪个服务器,就填充哪个服务器绑定的地址信息
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //本机IP的网络字节序
//2.连接服务器
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){
//清空buf
bzero(buf, sizeof(buf));
//3.发送数据
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("发送成功\n");
bzero(buf, sizeof(buf));
//4.接收数据
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0){
ERR_MSG("recv");
return -1;
}else if(res == 0){
printf("cfd = %d 服务器下线\n", cfd);
break;
}
printf("cfd = %d : %s\n", cfd, buf);
}
//关闭文件描述符
if(close(cfd) < 0){
ERR_MSG("close");
return -1;
}
return 0;
}
2.自行编写一个客户端代码,能够操作红色手臂移动到16度
ji_tcpCli.c
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0);
#define IP "192.168.10.106"
#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;
}
//填充服务器的地址信息结构体,给connect函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //本机的IP网络字节序
//连接服务器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0){
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
//发送机械臂指令
char cmd[5] = {0xff, 0x02, 0x00, 0x10, 0xff};
if(send(cfd, cmd, sizeof(cmd), 0) < 0){
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
if(close(cfd) < 0){
ERR_MSG("close");
return -1;
}
return 0;
}
3.附加题:通过wasd按键控制机械臂,ws控制红色手臂加减,da控制蓝色手臂加减
ji_dyn_tcpCli.c
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0);
#define IP "172.20.10.4"
#define PORT 6666
//定义指令
int command(char *cmd, int cfd, int size){
if(send(cfd, cmd, size, 0) < 0){
ERR_MSG("send");
return -1;
}
return 0;
}
int main(int argc, const char *argv[])
{
//创建流式套接字
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0){
ERR_MSG("socket");
return -1;
}
//填充服务器的地址信息结构体,给connect函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //端口号的网络字节序
sin.sin_addr.s_addr = inet_addr(IP); //本机的IP网络字节序
//连接服务器
if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0){
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
//定义蓝臂指令
unsigned char bcmd[5] = {0xff, 0x02, 0x01, 0x00, 0xff};
//定义红臂指令
char rcmd[5] = {0xff, 0x02, 0x00, 0x00, 0xff};
char key;
while(1){
key = getchar();
while(getchar() != '\n');
if(key == 'u'){
break;
}
switch(key){
case 'w' : //红臂角度增加
rcmd[3] += 5;
if(rcmd[3] > 90){
rcmd[3] = 90;
}
command(rcmd, cfd, sizeof(rcmd));
break;
case 's': //红臂角度减小
rcmd[3] -= 5;
if(rcmd[3] < -90){
rcmd[3] = -90;
}
command(rcmd, cfd, sizeof(rcmd));
break;
case 'd': //蓝臂角度增加
bcmd[3] += 5;
if(bcmd[3] > 180){
bcmd[3] = 180;
}
command(bcmd, cfd, sizeof(bcmd));
break;
case 'a': //蓝臂角度减小
if(bcmd[3] < 5){
bcmd[3] = 0;
}else{
bcmd[3] -= 5;
}
command(bcmd,cfd, sizeof(bcmd));
break;
default:
continue;
}
}
close(cfd);
return 0;
}