使用a d s w控制
#include <myhead.h>
#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));
ssize_t res=0;
char red[5]={0xff,0x02,0x00,-90,0xff};//red
unsigned char blue[5]={0xff,0x02,0x01,0,0xff}; //bluei
char c;
while(1){
c=getchar();
getchar();
switch(c){
case 'w': //red++
red[3]+=5;
if(red[3]>90){
red[3]=90;
}
break;
case 's': //red--
red[3]-=5;
if(red[3]<-90){
red[3]=-90;
}
break;
case 'a': //blue++
blue[3]+=5;
if(blue[3]>180){
blue[3]=180;
}
break;
case 'd': //blue--
blue[3]-=5;
if(blue[3]<0){
blue[3]=0;
}
break;
default:
continue;
}
if(send(cfd,red,sizeof(red),0)<0){
ERR_MSG("send");
return -1;
}
printf("red send success\n");
// sleep(1);
if(send(cfd,blue,sizeof(blue),0)<0){
ERR_MSG("send");
return -1;
}
printf("blue send success\n");
}
//关闭所有文件描述符
close(cfd);
return 0;
}
//TCP服务器
#include <myhead.h>
#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 <myhead.h>
#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 <myhead.h>
#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 <myhead.h>
#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;
}