赞
踩
TCP客户端
#include <mystdio.h>
#define CLI_PORT 6666
#define CLI_IP "192.168.124.210"
int main(int argc, const char *argv[])//argv[1] IP argv[2] 端口号
{
if(argc <3)
{
printf("请在命令传参端口号和IP地址\n");
return -1;
}
//创建流式套接字文件
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0)
{
perror("socket");
return -1;
}
printf("socket seccuss cfd = %d __%d__\n",cfd,__LINE__);
//允许端口号重复使用
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
perror("setsockopt");
return -1;
}
//填充客户端自身的地址信息,给bind函数绑定使用;
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(CLI_PORT);
cin.sin_addr.s_addr = inet_addr(CLI_IP);
//填充服务器的地址信息,给connet函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(argv[1]));
sin.sin_addr.s_addr = inet_addr(argv[2]);
//连接指定的服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
perror("connect");
return -1;
}
printf("connet seccuss __%d__\n",__LINE__);
char buf[128]="";
ssize_t res;
while(1)
{
//从终端输入数据
printf("请输入数据>>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送数据
res = send(cfd,buf,sizeof(buf),0);
if(res < 0)
{
perror("send");
return -1;
}
//接收
bzero(buf,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
perror("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线 __%d__\n",__LINE__);
break;
}
printf("%s __%d__",buf,__LINE__);
}
close(cfd);
return 0;
}
UDP客户端
#include <mystdio.h>
#define PORT 8888
#define IP "192.168.124.210"
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
fprintf(stderr,"line:%d",__LINE__);
perror("sockaddr");
return -1;
}
printf("创建报式套接字成功 cfd=%d __%d__\n",cfd,__LINE__);
//允许端口被重复使用
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
fprintf(stderr,"line:%d",__LINE__);
perror("setsockopt");
return -1;
}
//填充服务器的地址信息
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
char buf[128] = "";
struct sockaddr_in rcv;
socklen_t addrlen = sizeof(rcv);
while(1)
{
bzero(buf,sizeof(buf));
printf("请输入信息>>>\n");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
fprintf(stderr,"line:%d",__LINE__);
perror("sendto");
return -1;
}
if(strcmp(buf,"quit") == 0)
{
break;
}
printf("发送成功\n");
bzero(buf,sizeof(buf));
if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&rcv,&addrlen)<0)
{
fprintf(stderr,"line:%d",__LINE__);
perror("recvfrom");
return -1;
}
if(strcmp(buf,"quit") == 0)
{
break;
}
printf("[%s:%d] %s __%d__\n",\
inet_ntoa(rcv.sin_addr),ntohs(rcv.sin_port),buf,__LINE__);
}
close(cfd);
return 0;
}
机械臂
#include <mystdio.h>
#define CLI_PORT 6666
#define CLI_IP "192.168.124.210"
int main(int argc, const char *argv[])//argv[1] IP argv[2] 端口号
{
if(argc <3)
{
printf("请在命令传参端口号和IP地址\n");
return -1;
}
//创建流式套接字文件
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0)
{
perror("socket");
return -1;
}
printf("socket seccuss cfd = %d __%d__\n",cfd,__LINE__);
//允许端口号重复使用
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
perror("setsockopt");
return -1;
}
//填充服务器的地址信息,给connet函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(atoi(argv[1]));
sin.sin_addr.s_addr = inet_addr(argv[2]);
//连接指定的服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
perror("connect");
return -1;
}
printf("connet seccuss __%d__\n",__LINE__);
int fd = open("/dev/input/event1",O_RDONLY);
if(fd<0)
{
perror("open");
return -1;
}
struct input_event ev;
char buf[5]={0xff,0x02,0x00,30,0xff};
unsigned char str[5] = {0xff,0x02,0x01,20,0xff};
if(send(cfd,buf,sizeof(buf),0)< 0)
{
perror("send");
return -1;
}
if(send(cfd,str,sizeof(str),0)< 0)
{
perror("send");
return -1;
}
while(1)
{
if(read(fd,&ev,sizeof(ev))<0)
{
perror("read");
return -1;
}
switch(ev.code*ev.value)
{
case 17:
buf[3] += 5;
if(buf[3]>90)
buf[3] = 90;
break;
case 31:
buf[3] -= 5;
if(buf[3] < -90)
buf[3] = -90;
break;
case 30:
str[3] -= 5;
if(str[3] < 0)
str[3] = 0;
break;
case 32:
str[3] += 5;
if(str[3] >180 )
str[3] = 180;
break;
default:
continue;
}
if(send(cfd,buf,sizeof(buf),0)<0)
{
perror("send");
return -1;
}
if(send(cfd,str,sizeof(str),0)<0)
{
perror("send");
return -1;
}
}
close(cfd);
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。