目录:
1、server.c
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netinet/ip.h> #include<unistd.h> #include<arpa/inet.h> #include <sys/time.h> #include <unistd.h> #define MAXLINE 25 int main(int argc, char** argv){ int listenfd, connfd; struct sockaddr_in servaddr; char buff[25]; int n,cnt=0,i; int res,ret; char buf111[8] = {0x01,0x03,0x00,0x65,0x00,0x06,0xD5,0xD7}; struct timeval tv1,tv2; if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){ printf("create socket error: %s(errno: %d) ",strerror(errno),errno); return 0; } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(8080); if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){ printf("bind socket error: %s(errno: %d) ",strerror(errno),errno); return 0; } if( listen(listenfd, 10) == -1){ printf("listen socket error: %s(errno: %d) ",strerror(errno),errno); return 0; } //通过程序获取刚建立连接的socket的客户端的IP地址和端口号 struct sockaddr_in cin; socklen_t addrlen = sizeof(cin); if((connfd = accept(listenfd,(struct sockaddr *)&cin,&addrlen)) < 0) { printf("accept err "); exit(1); } char ipv4_addr[16]; if(!inet_ntop(AF_INET,(void *)&cin.sin_addr,ipv4_addr,sizeof(cin))) { printf("inet_ntop "); exit(1); } printf("Client(%s:%d) is connected! ",ipv4_addr,htons(cin.sin_port)); printf("======waiting for client's request====== "); while(1){ cnt++; printf("%drecv msg from client:",cnt); memset(buff, 0, sizeof(buff)); n = recv(connfd, buff, MAXLINE, 0); if(n > 0) { for(i = 0;i< n;i++) printf("%02X ", buff[i]); } printf(" "); send(connfd, buff, n,MSG_NOSIGNAL); //将接收到的数据发送回去 } close(connfd); close(listenfd); return 0; }
2、client.c
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<unistd.h> #define MAXLINE 25 int main(int argc, char** argv){ int sockfd, n,i,cnt=0; char recvline[25], sendline[25]; struct sockaddr_in servaddr; char buf111[8]={0x01,0x01,0x00,0x40,0x00,0x18,0x3D,0xD4}; char buff[25]; if( argc != 2){ printf("usage: ./a.out <ipaddress> "); return 0; } if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("create socket error: %s(errno: %d) ", strerror(errno),errno); return 0; } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8080); if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ printf("inet_pton error for %s ",argv[1]); return 0; } if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ printf("connect error: %s(errno: %d) ",strerror(errno),errno); return 0; } while(1) { sleep(1); cnt++; send(sockfd, buf111, sizeof(buf111),MSG_NOSIGNAL); printf("%dsend ok ",cnt); printf("%drecv msg from client:",cnt); memset(buff, 0, sizeof(buff)); n = recv(sockfd, buff, MAXLINE, 0); if(n > 0) { for(i = 0;i< n;i++) printf("%02X ", buff[i]); } printf(" "); } close(sockfd); return 0; }