TCP网络编程

  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
    
上一篇:嵌入式系统c语言socket之udpserver


下一篇:[python全栈]05.网络编程(3)