zoukankan      html  css  js  c++  java
  • Linux C语言实现的Socket通信 good

    Linux C语言实现的Socket通信 - Chris_Home - 博客频道 - CSDN.NET

    Linux C语言实现的Socket通信

    分类: Linux 630人阅读 评论(0) 收藏 举报

    其实这篇文章就是前面一篇文章的复制体,主要是今天闲着无聊,就在Ubuntu下又写了一篇这个传说中的简单Socket通信。

    以下是Linux网络编程的函数说明


    'socket' Function
    To perform network I/O, the first thing a process must do is call the socket function,
    specifying the type of communication protocol desired (TCP using IPv4, UDP using
    IPv6, Unix domain stream protocol, etc.).
    #include <sys/socket.h>
    int socket (int family, int type, int protocol);
    Returns: non-negative descriptor if OK, -1 on error
        

    具体参数如下



    'connect' Function
    The connect function is used by a TCP client to establish a connection with a TCP
    server.
    #include <sys/socket.h>
    int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);
    Returns: 0 if OK, -1 on error
    其中struct sockaddr
    是一个结构体。
    struct sockaddr_in { sa_family_t sin_family; /* address family: AF_INET */ in_port_t sin_port; /* port in network byte order */ struct in_addr sin_addr; /* internet address */};/* Internet address. */struct in_addr { uint32_t s_addr; /* address in network byte order */};



    'bind' Function
    #include <sys/socket.h>
    int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);
    Returns: 0 if OK,-1 on error



    'listen' Function
    #include <sys/socket.h>
    #int listen (int sockfd, int backlog);
    Returns: 0 if OK, -1 on error



    'accept' Function
    #include <sys/socket.h>
    int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
    Returns: non-negative descriptor if OK, -1 on error


    This function returns up to three values: an integer return code that is either a new
    socket descriptor or an error indication, the protocol address of the client process
    (through the cliaddr pointer), and the size of this address (through the addrlen
    pointer). If we are not interested in having the protocol address of the client returned,
    we set both cliaddr and addrlen to null pointers.


    'close' Function
    #include <unistd.h>
    int close (int sockfd);
    Returns: 0 if OK, -1 on error

    服务器端:

    1. #include<stdio.h>  
    2. #include<sys/socket.h>  
    3. #include<string.h>  
    4. #include<sys/types.h>  
    5. #include<netinet/in.h>  
    6. #include<arpa/inet.h>  
    7. #include<unistd.h>  
    8. #include<stdlib.h>  
    9. #define MAXN 4096   
    10.   
    11. int main(int argc ,char **argv){  
    12.     struct sockaddr_in A ,B ;  
    13.     char meg[MAXN] ;  
    14.     char rev[MAXN] ;  
    15.     socklen_t len;  
    16.     int s_socket,socket_conn;  
    17.     s_socket = socket(AF_INET,SOCK_STREAM,0);     
    18.     if(s_socket < 0){  
    19.         printf("Faile to socket!\n");  
    20.         return 1;  
    21.     }                         
    22.     A.sin_family = AF_INET ;  
    23.     if(argv[1]){  
    24.         A.sin_port = htons(atoi(argv[1])) ;  
    25.     }  
    26.         else  
    27.     A.sin_port = htons(1234);  
    28.     A.sin_addr.s_addr = htonl(INADDR_ANY) ;   
    29.     bind(s_socket ,(struct sockaddr *)&A,sizeof(A));          
    30.     listen(s_socket,5);                               
    31.     printf("Server is Waiting ...\n");            
    32.     len = sizeof(struct sockaddr_in) ;    
    33.     socket_conn = accept(s_socket,(struct sockaddr *)&B ,&len);  
    34.     if(socket_conn >= 0){  
    35.             printf("连接成功,开始通信!\n");  
    36.             while(1){                                         
    37.                 fgets(meg,MAXN,stdin);  inet_ntoa(B.sin_addr);    
    38.                 len = strlen(meg);  
    39.                 if(meg[len-1] == '\n')  meg[len-1] = 0 ;  
    40.                 send(socket_conn,meg,strlen(meg)+1,0);            
    41.                 printf("已经成功发送%d个字节\n",len);  
    42.                 if(strcmp(meg,"quit") == 0){  
    43.                     printf("Server is cancelling the communication!\n");  
    44.                     break ;  
    45.                 }  
    46.                 recv(socket_conn,rev,MAXN,0);                         
    47.                 if(strcmp(rev,"quit")==0){  
    48.                     printf("Client is cancelling the communication!\n");  
    49.                     break ;  
    50.                 }  
    51.                 printf("成功接受%d个字符,字符为:%s\n",strlen(rev),rev);  
    52.       
    53.             }  
    54.             close(socket_conn) ;                          
    55.     }  
    56.     else{  
    57.         printf("Faile to aceept!\n");  
    58.     }  
    59.     close(s_socket);                                  
    60.     return 0;  
    61. }  

    客户端:

    1. #include<stdio.h>  
    2. #include<string.h>  
    3. #include<sys/socket.h>  
    4. #include<sys/types.h>  
    5. #include<netinet/in.h>  
    6. #include<arpa/inet.h>  
    7. #include<unistd.h>  
    8. #include<stdlib.h>  
    9. #define MAXN 4096   
    10.   
    11. int main(int argc, char **argv){  
    12.     int c_socket ,conn;  
    13.     int len ;  
    14.     char rev[MAXN] ,buf[MAXN];  
    15.     struct sockaddr_in A ;  
    16.     c_socket = socket(AF_INET,SOCK_STREAM,0);         
    17.     A.sin_family = AF_INET ;      
    18.     if(argv[1]){  
    19.         A.sin_port = htons(atoi(argv[1]));  
    20.     }         
    21.     if(argv[2]){  
    22.         A.sin_addr.s_addr = inet_addr(argv[2]);  
    23.     }  
    24.     conn = connect(c_socket,(struct sockaddr *)&A,sizeof(struct sockaddr_in));        
    25.     if(conn >= 0){  
    26.         printf("正在进行通信!\n");  
    27.         while(1){  
    28.             recv(c_socket,rev,MAXN,0);                                    
    29.             if(strcmp(rev,"quit") == 0){  
    30.                 printf("Server is Cancelling the communication!\n");  
    31.                 break ;  
    32.   
    33.             }  
    34.                         printf("成功接收到了%d字符,字符为:%s\n",strlen(rev),rev);  
    35.             fgets(buf,MAXN,stdin);  
    36.             len = strlen(buf) ;  
    37.             if(buf[len-1] == '\n')  buf[len-1] = 0 ;  
    38.             send(c_socket,buf,strlen(buf)+1,0);           
    39.             if(strcmp(buf,"quit") == 0){  
    40.                 printf("Client is Cancelling the communication!\n");  
    41.                 break ;  
    42.             }  
    43.         }  
    44.         printf("Communication is Done!\n");  
    45.     }  
    46.     else{  
    47.         printf("Faile to connet\n");      
    48.         return 1;  
    49.     }  
    50.     close(c_socket);                      
    51.     return 0;  
    52. }  

    GCC编译过程:

    gcc -Wal -o Server Server.c

    ./Server 1234

    gcc -Wall -o Client Client.c

    ./Client  1234 127.0.0.1

  • 相关阅读:
    《新下级学》第八章第八、九节——责任总论等
    《新下级学》第八章第五、六、七节——信息不透明导致奖金失效等
    《新下级学》第八章第四节——不信任沟通
    《新下级学》第八章第三节——信任沟通
    《新下级学》第八章第二节——沟通工具
    《新下级学》第八章序和第一节——宏观互动
    《新下级学》第七章第五节——互动的陷阱
    《新下级学》第七章第四节——互动的三个频道
    GPS校时设备,GPS对时产品,NTP授时服务器
    北斗授时设备(NTP)在医疗行业的重要性
  • 原文地址:https://www.cnblogs.com/lexus/p/2977502.html
Copyright © 2011-2022 走看看