1、机器臂调试
#include<my_head.h>
#define PORT 8888
#define IP "192.168.125.151"
#define CPORT 9001
#define CIP "192.168.125.213"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd = -1;
if((cfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("soket error");
return -1;
}
//设置端口号快速重用
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
//填充绑定套接字的客户端地址信息
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_addr.s_addr = inet_addr(CIP);
cin.sin_port = htons(CPORT);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1)
{
perror("bind");
return -1;
}
//定义服务器地址结构体信息
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
//连接服务器端
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("连接成功\n");
//定义数据发送搬运工
char rbuf[5] = {0xff,0x02,0x00,0x10,0xff};
unsigned char bbuf[5] = {0xff,0x02,0x01,0x50,0xff};
//从终端获取操作的字符
char s = 0;
while(1)
{
s = getchar();
getchar();
switch (s)
{
case 'w':
case 'W':
{
rbuf[3] += 2;
if(rbuf[3] >= 90)
{
rbuf[3] = 90;
}
if(send(cfd,rbuf,sizeof(rbuf),0) == -1)
{
perror("send");
return -1;
}
break;
}
case 's':
case 'S':
{
rbuf[3] -= 2;
if(rbuf[3] <= -90)
{
rbuf[3] = -90;
}
if(send(cfd,rbuf,sizeof(rbuf),0) == -1)
{
perror("send");
return -1;
}
break;
}
case 'a':
case 'A':
{
bbuf[3] += 2;
if(bbuf[3] >= 180)
{
bbuf[3] = 180;
}
if(send(cfd,bbuf,sizeof(bbuf),0) == -1)
{
perror("send");
return -1;
}
break;
}
case 'd':
case 'D':
{
bbuf[3] -= 2;
if(bbuf[3] <= 0)
{
bbuf[3] = 0;
}
if(send(cfd,bbuf,sizeof(bbuf),0) == -1)
{
perror("send");
return -1;
}
break;
}
case 'q':
case 'Q':
{
break;
}
default:printf("enter error");
}
if(s == 'q'||s == 'Q')
{
break;
}
}
//关闭
close(cfd);
return 0;
}
2、TCP并发服务器--多进程
#include<my_head.h>
#define PORT 9999
#define IP "192.168.125.241"
/********************************定义回收子进程资源函数***************************************/
void handler(int signo)
{
if(signo == SIGCHLD)
{
while(waitpid(-1,NULL,WNOHANG) > 0);
}
}
/*******************************处理客户端消息函数**********************************/
int send_recv(int newfd,struct sockaddr_in cin)
{
char rbuf[128] = "";
while(1)
{
bzero(rbuf,sizeof(rbuf));
int res = recv(newfd,rbuf,sizeof(rbuf),0);
if(res == 0)
{
printf("客户端已下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr)\
,ntohs(cin.sin_port),rbuf);
//加一个字符串发回去
strcat(rbuf,"*_*");
send(newfd,rbuf,sizeof(rbuf),0);
}
close(newfd);
return 0;
}
/**************************主程序**************************************/
int main(int argc, const char *argv[])
{
//绑定信号用来回收子进程资源
if(signal(SIGCHLD,handler) == SIG_ERR)
{
perror("signal");
return -1;
}
//创建套接字
int sfd = -1;
if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("sock error");
return -1;
}
//绑定服务端地址端口号
//填充服务器地址端口号
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_%s_%s\n",__LINE__,__FILE__,__func__);
//设置监听
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_%s_%s\n",__LINE__,__FILE__,__func__);
//定义用于接收客户端地址信息结构体变量
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
//接收客户端链接请求,创建新文件描述符用于通信
int newfd = -1;
pid_t pid = -1;
while(1)
{
if((newfd = accept(sfd,(struct sockaddr*)&cin,&socklen)) == -1)
{
perror("accept newfd error");
return -1;
}
printf("[%s:%d] 连接成功,newfd = %d\n",inet_ntoa(cin.sin_addr)\
,ntohs(cin.sin_port),newfd);
//创建子进程
pid = fork();
//父进程干活
if(pid >0)
{
close(newfd);
}
//子进程干活
else if(pid == 0)
{
close(sfd);
send_recv(newfd,cin);
exit(EXIT_SUCCESS);
}else
{
perror("fock");
return -1;
}
//收发消息
//定义搬运工
}
//关闭套接字
close(sfd);
return 0;
}
3、TCP并发服务器--多线程
#include<my_head.h>
#define PORT 9999
#define IP "192.168.125.241"
struct pthread_ds
{
int newfd;
struct sockaddr_in cin;
};
/*******************************处理客户端消息函数**********************************/
void *deal_cli_msg(void *arg)
{
//分解参数结构体
int newfd = ((struct pthread_ds*)arg)->newfd;
struct sockaddr_in cin = ((struct pthread_ds*)arg)->cin;
char rbuf[128] = "";
while(1)
{
bzero(rbuf,sizeof(rbuf));
int res = recv(newfd,rbuf,sizeof(rbuf),0);
if(res == 0)
{
printf("客户端已下线\n");
break;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr)\
,ntohs(cin.sin_port),rbuf);
//加一个字符串发回去
strcat(rbuf,"*_*");
send(newfd,rbuf,sizeof(rbuf),0);
}
close(newfd);
pthread_exit(NULL);
}
/**************************主程序**************************************/
int main(int argc, const char *argv[])
{
//创建套接字
int sfd = -1;
if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("sock error");
return -1;
}
//绑定服务端地址端口号
//填充服务器地址端口号
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(IP);
sin.sin_port = htons(PORT);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_%s_%s\n",__LINE__,__FILE__,__func__);
//设置监听
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_%s_%s\n",__LINE__,__FILE__,__func__);
//定义用于接收客户端地址信息结构体变量
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
//接收客户端链接请求,创建新文件描述符用于通信
int newfd = -1;
pthread_t tid = -1;
while(1)
{
if((newfd = accept(sfd,(struct sockaddr*)&cin,&socklen)) == -1)
{
perror("accept newfd error");
return -1;
}
printf("[%s:%d] 连接成功,newfd = %d\n",inet_ntoa(cin.sin_addr)\
,ntohs(cin.sin_port),newfd);
//填充分支线程函数参数结构体
struct pthread_ds info = {newfd,cin};
//创建分支线程
if(pthread_create(&tid,NULL,deal_cli_msg,&info) !=0)
{
printf("tid create error\n");
return -1;
}
//将线程设置成分离态,交给系统自动回收
pthread_detach(tid);
}
//关闭套接字
close(sfd);
return 0;
}