赞
踩
1.TCP的服务器客户端
1)服务器
- #include <myhead.h>
- #define ERR_MSG(msg) do{\
- fprintf(stderr,"__%d__",__LINE__);\
- perror(msg);\
- }while(0)
-
- #define IP "192.168.10.102"
-
- int main(int argc, const char *argv[])
- {
- int sfd = socket(AF_INET,SOCK_STREAM,0);
- if(sfd < 0)
- {
- ERR_MSG("socket");
- return -1;
- }
-
- //允许端口快速被重用
- int reuse = 1;
- if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) <0)
- {
- ERR_MSG("setsockopt");
- return -1;
-
- }
- printf("允许端口快速被重用成功\n");
-
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_port = htons(8888);
- sin.sin_addr.s_addr = inet_addr(IP);
-
- if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
- {
- ERR_MSG("bind");
- return -1;
- }
- printf("bind success\n");
-
- if(listen(sfd,128) < 0)
- {
- ERR_MSG("listen");
- return -1;
- }
- printf("listen success\n");
-
- struct sockaddr_in cin;
- socklen_t addrlen = sizeof(cin);
-
- int newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
- if(newfd <0)
- {
- ERR_MSG("accept");
- return -1;
- }
-
- printf("[%s : %d] newfd = %d 客户端连接成功\n",\
- inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
-
- char buf[128] = "";
- ssize_t res = 0;
- while(1)
- {
- bzero(buf,sizeof(buf));
-
- res = recv(newfd,buf,sizeof(buf),0);
- if(res < 0)
- {
- ERR_MSG("recv");
- return -1;
- }
- else if(0 == res)
- {
- printf("[%s ; %d] newfd = %d 客户端下线\n",\
- inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
- break;
- }
- printf("[%s ; %d] newfd=%d ; %s\n",\
- inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
-
- strcat(buf,"Q_A_Q");
- if(send(newfd,buf,sizeof(buf),0)<0)
- {
- ERR_MSG("send");
- return -1;
- }
- printf("发送成功\n");
-
- }
-
- close(newfd);
-
- if(close(sfd)<0)
- {
- ERR_MSG("close");
- return -1;
- }
-
- return 0;
- }
2)客户端
- #include <myhead.h>
-
- #define ERR_MSG(msg) do { fprintf(stderr,"__%d__",__LINE__);perror(msg);}while(0)
-
- #define IP "192.168.10.102" //ifconfig查看本机IP
- #define PORT 8888
-
- int main(int argc, const char *argv[])
- {
- //printf("%d %s %s\n",__LINE,__func,__FILE__);
-
- //创建流式套接字
- int cfd = socket(AF_INET,SOCK_STREAM,0);
- if(cfd < 0)
- {
- ERR_MSG("socket");
- return -1;
- }
-
- int reuse = 1;
- if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) <0)
- {
- ERR_MSG("setsockopt");
- return -1;
- }
- printf("允许端口快速被重用成功\n");
-
-
- //填充服务器的地址信息结构体 AF_INET:man 7 ip
- //填充服务器的地址信息结构体,给connect函数使用
- //要连接哪个服务器,就填充哪个服务器绑定的地址信息
-
- struct sockaddr_in sin;
- sin.sin_family = AF_INET; //必须填AF_INET
- sin.sin_port = htons(PORT); //端口号的网络字节序,1024-49151
- sin.sin_addr.s_addr= inet_addr(IP); //本机IP的网络字节序
-
- if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin))<0)
- {
- ERR_MSG("connect");
- return -1;
- }
- printf("connect success\n");
-
- char buf[128] = "";
- ssize_t res = 0;
- while(1)
- {
- bzero(buf,sizeof(buf));
-
- //发送数据
- printf("请输入:");
- fgets(buf,sizeof(buf),stdin);
- buf[strlen(buf)-1]=0;
-
- //对方接收多少个字节,发送多少个字节
- if(send(cfd,buf,sizeof(buf),0) <0)
- {
- ERR_MSG("send");
- return -1;
- }
- printf("发送成功\n");
-
- bzero(buf,sizeof(buf));
- //接收数据
- res = recv(cfd,buf,sizeof(buf),0);
- if(res < 0)
- {
- ERR_MSG("recv");
- return -1;
-
- }
- else if(0 == res)
- {
- printf("cfd = %d 服务器下线\n",cfd);
- break;
- }
- printf("cfd=%d : %s\n",cfd,buf);
-
- }
-
- //关闭文件描述符
- if(close(cfd)<0)
- {
- ERR_MSG("close");
- return -1;
- }
-
- return 0;
- }
-
运行结果:
2. 自行编写一个客户端代码,能够操作红色手臂移动到16度
- #include <myhead.h>
-
- #define ERR_MSG(msg) do { fprintf(stderr,"__%d__",__LINE__);perror(msg);}while(0)
-
- #define IP "192.168.10.135" //ifconfig查看本机IP
- #define PORT 8888
-
- int main(int argc, const char *argv[])
- {
-
- int cfd = socket(AF_INET,SOCK_STREAM,0);
- if(cfd < 0)
- {
- ERR_MSG("socket");
- return -1;
- }
-
- int reuse = 1;
- if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) <0)
- {
- ERR_MSG("setsockopt");
- return -1;
- }
- printf("允许端口快速被重用成功\n");
-
-
- struct sockaddr_in sin;
- sin.sin_family = AF_INET; //必须填AF_INET
- sin.sin_port = htons(PORT); //端口号的网络字节序,1024-49151
- sin.sin_addr.s_addr= inet_addr(IP); //本机IP的网络字节序
-
- if(connect(cfd,(struct sockaddr *)&sin,sizeof(sin))<0)
- {
- ERR_MSG("connect");
- return -1;
- }
- printf("connect success\n");
-
- char buf[5];
-
- while(1)
- {
- printf("请输入:");
- for(int i=0;i<5;i++)
- {
- scanf("%hhx",&buf[i]);
- }
-
- if((send(cfd,buf,sizeof(buf),0))<0)
- {
- ERR_MSG("send");
- return -1;
- }
- printf("发送成功\n");
- }
-
- //关闭文件描述符
- if(close(cfd)<0)
- {
- ERR_MSG("close");
- return -1;
- }
-
- return 0;
- }
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。