将TCP服务器客户端的代码再写一遍上交
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) \
do \
{ \
fprintf(stderr, "line:%d", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.8.253"
#define PORT 6666
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success __%d__\n",__LINE__);
int reuse=1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许快速重用端口成功 __%d__\n",__LINE__);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(connect(cfd, (struct sockaddr *)&sin, sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success __%d__\n", __LINE__);
char buf[128] = "";
ssize_t res = 0;
while (1)
{
bzero(buf, sizeof(buf));
printf("please puts>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(write(cfd,buf,sizeof(buf))<0)
{
ERR_MSG("send");
return -1;
}
res = read(cfd, buf, sizeof(buf));
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("cfd=%d 客户端已下线 __%d__\n",cfd,__LINE__);
break;
}
printf("cfd=%d %s __%d__\n",cfd,buf,__LINE__);
将UDP服务器客户端的代码再写一遍上交
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) \
do \
{ \
fprintf(stderr, "line:%d", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.8.253"
#define PORT 6666
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success __%d__\n",__LINE__);
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
struct sockaddr_in rcvaddr;
socklen_t addrlen=sizeof(rcvaddr);
char buf[128]="";
ssize_t res =0;
while(1)
{
bzero(buf, sizeof(buf));
printf("please puts>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
perror("sento");
return -1;
}
printf("send succes\n");
bzero(buf,sizeof(buf));
res =recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&rcvaddr,&addrlen);
if(res<0)
{
perror("recvfrom");
return -1;
}
printf("[%s : %d] sfd=%d %s __%d__\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),sfd,buf,__LINE__);
}
return 0;
}
. 测试机械臂协议
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) \
do \
{ \
fprintf(stderr, "line:%d", __LINE__); \
perror(msg); \
} while (0)
#define IP "192.168.9.1"
#define PORT 8888
struct node
{
char buf_set;
char buf_jixie;
char buf_x;
char buf_y;
char buf_end;
};
int main(int argc, const char *argv[])
{
struct node buf;
buf.buf_set = 0xff;
buf.buf_end = 0xff;
buf.buf_jixie = 0x02;
buf.buf_x = 0x00;
char y1 = 0xff ;
char y2 = 0xff;
int n;
int cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success __%d__\n", __LINE__);
int reuse = 1;
if (setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许快速重用端口成功 __%d__\n", __LINE__);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if (connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success __%d__\n", __LINE__);
char con[128];
int i = 0;
scanf("%s", con);
while (1)
{
switch (con[i])
{
case 'w':
buf.buf_x = 0x00;
y1++;
buf.buf_y = y1;
break;
case 's':
buf.buf_x = 0x00;
y1--;
buf.buf_y = y1;
break;
case 'a':
buf.buf_x = 0x01;
y2++;
buf.buf_y = y2;
break;
case 'd':
buf.buf_x = 0x01;
y2--;
buf.buf_y = y2;
break;
default:
break;
}
printf("%c",con[i]);
i++;
if (con[i] == 0)
break;
}
write(cfd, &buf.buf_set, sizeof(buf));
write(cfd, &buf.buf_jixie, sizeof(buf));
write(cfd, &buf.buf_x, sizeof(buf));
write(cfd, &buf.buf_y, sizeof(buf));
write(cfd, &buf.buf_end, sizeof(buf));
printf("%#hhx ", buf.buf_set);
printf("%#hhx ", buf.buf_jixie);
printf("%#hhx ", buf.buf_x);
printf("%#hhx ", buf.buf_y);
printf("%#hhx\n ", buf.buf_end);
close(cfd);
return 0;
}