ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

TCP网络编程

2021-02-01 16:02:17  阅读:180  来源: 互联网

标签:编程 网络 TCP server client sockfd include sin buff


  1. 服务端

    /*
    * @Author: hohj
    * @Date:   2020-12-13 14:14:49
    * @Last Modified by:   hohj
    * @Last Modified time: 2021-01-13 10:25:31
    */
    
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    #include "../color.h"
    
    int main(int argc, char const *argv[])
    {
    	int sockfd = socket(AF_INET, SOCK_STREAM,0 );
    
    	struct sockaddr_in server;		//设置服务端地址信息
    	server.sin_family = AF_INET;
    	server.sin_addr.s_addr = inet_addr("192.168.33.1");
    	server.sin_port = htons(8888);
    	//绑定本地地址信息
    	if(bind(sockfd, (struct sockaddr *)&server, sizeof(server))==-1){
    		perror("bind()");
    		exit(1);
    	}
    	//进入等待连接请求状态
    	listen(sockfd, 5);
    	
    	struct sockaddr_in client;//创建sockaddr_in用于存储客户端地址信息
    	socklen_t len = sizeof(client);
    
    	int newfd = accept(sockfd, (struct sockaddr *)&client, &len);//接受客户端连接请求
    	printf("%d, %d", sockfd, newfd);
    	printf("before listen\n");
    	char buff[10] = {0};//此处设置的buff大小只有10字节
    	
    	while(1) {
        	bzero(buff, sizeof(buff));
            int ret = read(newfd, buff, sizeof(buff));//读取buff大小(10)的数据,
            if(ret==0) exit(0);
            //recv(newfd, buff, sizeof(buff), 0);
            printf(RED"%d: %s(%d): %s\n", newfd, inet_ntoa(client.sin_addr), ntohs(client.sin_port), buff); 
        }
    
    	return 0;
    }
    
    
  2. 客户端

    /*
    * @Author: hohj
    * @Date:   2020-12-13 14:14:39
    * @Last Modified by:   hohj
    * @Last Modified time: 2021-02-01 15:24:30
    */
    #include <unistd.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <stdlib.h>
    #include <arpa/inet.h>
    
    int main(int argc, char const *argv[])
    {
    	int sockfd = socket(AF_INET, SOCK_STREAM, 0);//创建TCP套接字
    
    	struct sockaddr_in server;			//创建sockaddr_in结构体(保存地址信息)
    	server.sin_family = AF_INET;	//使用IPv4地址
    	server.sin_addr.s_addr = inet_addr("192.168.33.20"); //服务器地址
    	server.sin_port = htons(6666);	//服务器端口
    
    	/*struct sockaddr_in client;
    	client.sin_family = AF_INET;
    	client.sin_addr.s_addr = inet_addr("127.0.0.1");
    	client.sin_port = htons(6666);*/
    	
    	/*if(bind(sockfd, (struct sockaddr *)&client, sizeof(client))==-1){
    		perror("bind()");
    		exit(1);
    	}*/
    	//请求连接
    	if(connect(sockfd, (struct sockaddr *)&server, sizeof(server))==-1){
    		perror("connect()");
    		exit(1);
    	}
    	printf("after connect\n");
    
    	char buff[512] = {0};
    	while(1) {
            bzero(buff, sizeof(buff));
            printf(">");
            scanf("%[^\n]s", buff);
            getchar();
            write(sockfd, buff, strlen(buff));//发送数据
            //send(sockfd, buff, strlen(buff), 0);
        }
    
    	return 0;
    }
    
  3. 编译
    gcc client.c -o client
    gcc server.c -o server

  4. 运行
    服务端:./server

    hohj@ubuntu20-10:~/Documents/C/tcp$ gcc server.c -o server
    hohj@ubuntu20-10:~/Documents/C/tcp$ ./server 
    3, 4before listen
    4: 127.0.0.1(40258): this is th
    4: 127.0.0.1(40258): e TCP conn
    4: 127.0.0.1(40258): ect servic
    4: 127.0.0.1(40258): es
    

    客户端:./client

    hohj@ubuntu20-10:~/Documents/C/tcp$ gcc server.c -o server
    hohj@ubuntu20-10:~/Documents/C/tcp$ ./server 
    3, 4before listen
    4: 127.0.0.1(40258): this is th
    4: 127.0.0.1(40258): e TCP conn
    4: 127.0.0.1(40258): ect servic
    4: 127.0.0.1(40258): es
    

标签:编程,网络,TCP,server,client,sockfd,include,sin,buff
来源: https://blog.csdn.net/qq_42058305/article/details/113520055

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有