您的位置:首页 > 汽车 > 新车 > TCP简易通信实现

TCP简易通信实现

2024/11/16 21:59:27 来源:https://blog.csdn.net/m0_62342492/article/details/140969309  浏览:    关键词:TCP简易通信实现

服务端

#include "sys/socket.h"
#include "sys/types.h"
#include "arpa/inet.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include "stdlib.h"
#include "errno.h"int main (int argc, char* argv[])
{if(argc != 3){printf("Usage: %s <ip> <port>\n",argv[0]);exit(EXIT_FAILURE);}/*create socket*/int sockfd = socket(AF_INET,SOCK_STREAM,0);if(sockfd == -1){fprintf(stderr,"socket ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}/*bind socket*/struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(atoi(argv[2]));//将主机字节序转为网络字节序大端,头文件为#include "arpa/inet.h"//addr.sin_addr.s_addr = argv[0];//INADDR_ANY是一个预定义的常量,其值通常为0,表示套接字将绑定到本机的所有IP地址上int exchange = inet_aton(argv[1], &addr.sin_addr);if(exchange == 0){printf("error inet_aton\n");exit(EXIT_FAILURE);}int ret_bind = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));if(ret_bind == -1){fprintf(stderr,"bind ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}/*listen socket*/int listen_ret = listen(sockfd,5);if(listen_ret == -1){fprintf(stderr,"listen ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}/* accept client */struct sockaddr_in client_addr;socklen_t len = sizeof(client_addr);int fd = accept(sockfd, (struct sockaddr *)&client_addr, &len);if(fd==-1){fprintf(stderr,"accept ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}char buf[BUFSIZ]={0};while(1){memset(buf,0,BUFSIZ);ssize_t bytes_read = read(fd,buf,BUFSIZ);if(bytes_read==-1){fprintf(stderr,"read ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}else{printf("recv:%s",buf);}}close(fd);close(sockfd);
}

客户端

#include "sys/socket.h"
#include "sys/types.h"
#include "arpa/inet.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include "stdlib.h"
#include "errno.h"/*define area start*/
#define DEBUG 0
/*define area end*//*structure area start*/
/*structure area end*/
int main (int argc, char* argv[])
{if(argc != 3){printf("Usage: %s <ip> <port>\n",argv[0]);exit(EXIT_FAILURE);}/*create socket*/int sockfd = socket(AF_INET,SOCK_STREAM,0);if(sockfd == -1){fprintf(stderr,"socket ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}/*bind socket*/struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(atoi(argv[2]));//将主机字节序转为网络字节序大端,头文件为#include "arpa/inet.h"
#if DEBUGaddr.sin_addr.s_addr = inet_addr("127.0.0.1");//INADDR_ANY是一个预定义的常量,其值通常为0,表示套接字将绑定到本机的所有IP地址上
#elseint exchange = inet_aton(argv[1], &addr.sin_addr);
#endifif(exchange == 0){printf("error inet_aton\n");exit(EXIT_FAILURE);}int ret_connect = connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));if( ret_connect == -1){fprintf(stderr,"bind ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}char buf[BUFSIZ]={0};while(1){printf("please input:\n");fgets(buf,BUFSIZ,stdin);int ret_write = write(sockfd,buf,strlen(buf));if(ret_write == -1){fprintf(stderr,"write ERROR:%s\n",strerror(errno));exit(EXIT_FAILURE);}}close(sockfd);
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com