使用a d s w控制
#include
#define IP "192.168.124.7"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建socket文件
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0){
ERR_MSG("socket");
return -1;
}
printf("cfd create success\n");
//绑定服务器的IP和端口号
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
socklen_t len=sizeof(sin);
if(connect(cfd,(struct sockaddr*)&sin,len)<0){
ERR_MSG("connect");
return -1;
}
//把服务器的IP和端口提出来
printf("连接 %s:%d 成功\n",inet_ntoa(sin.sin_addr),\
ntohs(sin.sin_port));
//打开键盘驱动文件
int fd=open("/dev/input/event1",O_RDONLY);
if(fd<0){
ERR_MSG("open");
return -1;
}
ssize_t res=0;
char red[5]={0xff,0x02,0x00,-90,0xff};//red
unsigned char blue[5]={0xff,0x02,0x01,0,0xff}; //blue
struct input_event ev; //键盘传输结构体
int flag_r=0; //为1的时候红色发
int flag_b=0; //为1的时候蓝色发
while(1){
read(fd,&ev,sizeof(ev));
switch(ev.code*ev.value){//code是按键值,value 1:按下 2:长按 0:弹起
case 17: //red++ w 17
red[3]+=5;
if(red[3]>90){
red[3]=90;
}
flag_r=1;
break;
case 31: //red-- s 31
red[3]-=5;
if(red[3]<-90){
red[3]=-90;
}
flag_r=1;
break;
case 30: //blue++ a 30
blue[3]+=5;
if(blue[3]>180){
blue[3]=180;
}
flag_b=1;
break;
case 32: //blue-- d 32
if(blue[3]>=5)
blue[3]-=5;
flag_b=1;
break;
default:
continue;
}
if(1==flag_r){
if(send(cfd,red,sizeof(red),0)<0){
ERR_MSG("send");
return -1;
}
// printf("red send success\n");
flag_r=0;
}
// sleep(1);
if(1==flag_b){
if(send(cfd,blue,sizeof(blue),0)<0){
ERR_MSG("send");
return -1;
}
printf("blue send success\n");
flag_b=0;
}
}
//关闭所有文件描述符
close(cfd);
close(fd);
return 0;
}
//TCP服务器
#include
#define IP "192.168.124.74"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建socket文件
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("sfd create success\n");
//绑定服务器的IP和端口号
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))<0){
ERR_MSG("bind");
return -1;
}
printf("bind IP&PORT success\n");
//将文件转为被动监听模式
if(listen(sfd,128)<0){
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//用连接成功的客户端的信息生成accept文件
struct sockaddr_in cin;
socklen_t len=sizeof(cin);
int newfd=accept(sfd,(struct sockaddr*)&cin,&len);
if(newfd<0){
ERR_MSG("accept");
return -1;
}
//把客户端的IP和端口提出来
printf("newfd:%d %s:%d 连接成功\n",newfd\
,inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
ssize_t res=0;
char buf[128]="";
while(1){
bzero(buf,sizeof(buf));
//接收客户端数据
res=recv(newfd,&buf,sizeof(buf),0);
if(res<0){
ERR_MSG("recv");
break;
}else if(res==0){
printf("客户端下线\n");
break;
}
printf("[%s:%d]:%s\n",\
inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port),buf);
//向客户端发送数据
//要发送的数据
strcat(buf,"^-^");
res=send(newfd,buf,sizeof(buf),0);
if(res<0){
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
//关闭所有文件描述符
close(sfd);
close(newfd);
return 0;
}
//TCP客户端
#include
#define IP "192.168.124.73"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建socket文件
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0){
ERR_MSG("socket");
return -1;
}
printf("cfd create success\n");
//绑定服务器的IP和端口号
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
socklen_t len=sizeof(sin);
if(connect(cfd,(struct sockaddr*)&sin,len)<0){
ERR_MSG("connect");
return -1;
}
//把服务器的IP和端口提出来
printf("连接 %s:%d 成功\n",inet_ntoa(sin.sin_addr),\
ntohs(sin.sin_port));
ssize_t res=0;
char buf[128]="";
while(1){
bzero(buf,sizeof(buf));
//向服务器发送数据
//要发送的数据
printf("请输入要发送的信息>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
res=send(cfd,buf,sizeof(buf),0);
if(res<0){
ERR_MSG("send");
return -1;
}
printf("send success\n");
//接收客户端数据
res=recv(cfd,&buf,sizeof(buf),0);
if(res<0){
ERR_MSG("recv");
break;
}else if(res==0){
printf("服务器下线\n");
break;
}
printf("[%s:%d]:%s\n",\
inet_ntoa(sin.sin_addr),\
ntohs(sin.sin_port),buf);
}
//关闭所有文件描述符
close(cfd);
return 0;
}
//UDP服务器
#include
#define PORT 6666
#define IP "192.168.124.74"
int main(int argc, const char *argv[])
{
//创建套接字
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0){
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
//绑定服务器地址信息
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))<0){
ERR_MSG("bind");
return -1;
}
printf("bind sockaddr success\n");
ssize_t res=0;
char buf[128]="";
struct sockaddr_in cin;
socklen_t len=sizeof(cin);
while(1){
bzero(buf,sizeof(buf));
//接收客户端数据
res=recvfrom(sfd,buf,sizeof(buf),0,\
(struct sockaddr*)&cin,&len);
if(res<0){
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//向客户端发送数据
strcat(buf,"\t--get");
res=sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin));
if(res<0){
ERR_MSG("sendto");
return -1;
}
printf("send to %s:%d success\n",inet_ntoa(cin.sin_addr),\
ntohs(cin.sin_port));
}
close(sfd);
return 0;
}
//UDP客户端
#include
#define IP "192.168.124.74"
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建套接字文件
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0){
ERR_MSG("socket");
return -1;
}
printf("socket create success\n");
//填充地址信息体
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
char str[128];
ssize_t res=0;
struct sockaddr_in recvin;
socklen_t len=sizeof(recvin);
while(1){
bzero(str,sizeof(str));
printf("请输入想要发送的内容>>>");
scanf("%s",str);
getchar();
//向服务器发送
res=sendto(cfd,str,sizeof(str),0,\
(struct sockaddr*)&sin,sizeof(sin));
if(res<0){
ERR_MSG("sendto");
return -1;
}
printf("sendto %s:%d success\n",\
inet_ntoa(sin.sin_addr),ntohs(sin.sin_port));
//从服务器接收
res=recvfrom(cfd,str,sizeof(str),0,\
(struct sockaddr*)&recvin,&len);
if(res<0){
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d]: %s\n",inet_ntoa(recvin.sin_addr),\
ntohs(recvin.sin_port),str);
}
//关闭文件
close(cfd);
return 0;
}