有道云笔记
机械臂
#include <myhead.h>
#define SERPORT 8822
#define SERIP "192.168.125.61"
#define PORT 6666
#define IP "192.168.125.88"
int main(int argc, const char *argv[])
{int cfd=-1;if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror("socket");return -1;}
#if 1int reuse=1;if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){perror("setsocket");return -1;}
#endifstruct 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");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");return -1;}char buf[5]={0xff,0x02,0x00,0x00,0xff};char value=0;buf[3]=0;send(cfd,buf,sizeof(buf),0);buf[2]=0x01;send(cfd,buf,sizeof(buf),0);buf[2]=0x00;while(1){value=getchar();getchar();switch(value){case 'w':case 'W':{buf[2]=0x00;buf[3]+=2;if(buf[3]>=90)buf[3]=90;send(cfd,buf,sizeof(buf),0);}break;case 's':case 'S':{buf[2]=0x00;buf[3]-=2;if(buf[3]<=-90)buf[3]=-90;send(cfd,buf,sizeof(buf),0);}break;case 'd':case 'D':{buf[2]=0x01;buf[3]+=2;if(buf[3]>=180)buf[3]=180;send(cfd,buf,sizeof(buf),0);}break;case 'a':case 'A':{buf[2]=0x01;buf[3]-=2;if(buf[3]<=0)buf[3]=0;send(cfd,buf,sizeof(buf),0);}break;default : {printf("error\n");break;}}}close(cfd);return 0;
}
2tcp多进程
#include <myhead.h>
#define IP "192.168.125.88"
#define PORT 8811
void handle(int signo)
{if(signo==SIGCHLD)while(waitpid(-1,NULL,WNOHANG)>0);
}
int deal_sc(int newfd,struct sockaddr_in cin)
{char buf[128];while(1){bzero(buf,sizeof(buf));int res=recv(newfd,buf,sizeof(buf),0);if(res==0){printf("退出\n");return -1;}printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);strcat(buf,"*_*");send(newfd,buf,sizeof(buf),0);}return 0;
}
int main(int argc, const char *argv[])
{if(signal(SIGCHLD,handle)==SIG_ERR){perror("signal");return -1;}int sfd;if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1){perror("socket");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");return -1;}if(listen(sfd,128)==-1){perror("listen");return -1;}struct sockaddr_in cin;socklen_t socklen=sizeof(cin);int newfd;int pid;while(1){if((newfd=accept(sfd,(struct sockaddr *)&cin,&socklen))==-1){perror("accept");return -1;}pid=fork();if(pid>0){close(newfd);}else if(pid==0){close(sfd);deal_sc(newfd,cin);close(newfd);exit(EXIT_SUCCESS);}else{printf("error\n");return -1;}}close(sfd);return 0;
}
3tcp多线程
#include <myhead.h>
#define PORT 8899
#define IP "192.168.125.88"
struct ds{int newfd;struct sockaddr_in cin;
};
void *task(void *arg)
{int newfd=((struct ds*)arg)->newfd;struct sockaddr_in cin=((struct ds*)arg)->cin;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);}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("socket");return -1;}
#if 1int reuse=1;if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1){perror("setsocket");return -1;}
#endifstruct 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");return -1;}if(listen(sfd,128)==-1){perror("listen");return -1;}struct sockaddr_in cin;socklen_t socklen=sizeof(cin);int newfd;pthread_t tid=-1;while(1){if((newfd=accept(sfd,(struct sockaddr*)&cin,&socklen))==-1){perror("accept");return -1;}struct ds info;info.newfd=newfd;info.cin=cin;if(pthread_create(&tid,NULL,task,&info)!=0){printf(" pthread error\n");return -1;}printf("[%s:%d]:newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);pthread_detach(tid);}close(sfd);return 0;
}