美文网首页
tcp网络编程(C语言)

tcp网络编程(C语言)

作者: 奋斗live | 来源:发表于2019-03-19 15:24 被阅读0次

    以下是两个文件.client.c和server.c

    server.c

    
    /***********引入必要的文件**************/
    #include<stdio.h>
    
    #include<string.h>
    #include<sys/types.h>
    
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    #include<netdb.h>
    #include<errno.h>
    #include<stdlib.h>
    #include<unistd.h>
    
    int main(int argc,char **argv){
        int sock,accetp_sock,sock_len,numbytes,i;
        struct sockaddr_in server_addr;
        struct sockaddr_in client_addr;
        char buff[BUFSIZ];
        
        /*
        *sin_family   AF_INET(ipv4) AF_INET6(ipv6) AF_LOCAL(unix协议) AF_KEY(秘钥)
        */
        server_addr.sin_family = AF_INET;
        //绑定端口
        server_addr.sin_port = htons(8000);
        //绑定本地ip(也就是0.0.0.0)
        //表示本机的所有ip(可能本机有多个网卡,这能代表所有网卡)
        server_addr.sin_addr.s_addr = INADDR_ANY;
        //清零
        bzero(&(server_addr.sin_zero),8);
        sock_len = sizeof(struct sockaddr_in);
        printf("start\n");
        sock = socket(AF_INET,SOCK_STREAM,0);
        //绑定
        while(bind(sock,(struct sockaddr *)&server_addr,sock_len)==-1);
        printf("bind success\n");
        while(listen(sock,10)==-1);
        printf("Listening..\n");
        printf("Ready for Accept,Waiting...\n");
        //开始阻塞
        accetp_sock = accept(sock,(struct sockaddr *)&client_addr,&sock_len);
        printf("Get the client\n");
        numbytes = send(accetp_sock,"Welecome to my server\n",21,0);
        //开始阻塞
        while((numbytes = recv(accetp_sock,buff,BUFSIZ,0))>0){
            buff[numbytes] = '\0';
            printf("%s\n",buff);
            if(send(accetp_sock,buff,numbytes,0)<0){
                perror("write");
                return 1;
            }
        }
        close(accetp_sock);
        close(sock);
        return 0;
    }   
    
    

    client.c

    
    /***********引入必要的文件**************/
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<errno.h>
    #include<netdb.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    int main(int argc,char **argv){
        int sockfd,numbers;
        char buf[BUFSIZ];
        struct sockaddr_in sockaddress;
        printf("start:\n");
        while((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1);
        printf("get sockfd:\n");
        /*
        *sin_family   AF_INET(ipv4) AF_INET6(ipv6) AF_LOCAL(unix协议) AF_KEY(秘钥)
        */
        sockaddress.sin_family = AF_INET;
        //绑定端口
        sockaddress.sin_port = htons(8000);
        //连接某个ip
        sockaddress.sin_addr.s_addr = inet_addr(argv[1]);
        //清零
        bzero(&(sockaddress.sin_zero),8);
        //连接
        while(connect(sockfd,(struct sockaddr*)&sockaddress,sizeof(struct sockaddr))==-1);
        printf("Get the server\n");
        //开始阻塞
        numbers = recv(sockfd,buf,BUFSIZ,0);
        buf[numbers]= '\0';
        printf("%s",buf);
        while(1){
            printf("entersome thing:");
            scanf("%s",buf);
            numbers = send(sockfd,buf,sizeof(buf),0);
            //开始阻塞
            numbers = recv(sockfd,buf,BUFSIZ,0);
            buf[numbers]= '\0';
            printf("received:%s\n",buf);
        }
        close(sockfd);
        return 0;
    }
    
    

    运行效果如下


    image.png

    相关文章

      网友评论

          本文标题:tcp网络编程(C语言)

          本文链接:https://www.haomeiwen.com/subject/edzgmqtx.html