zoukankan      html  css  js  c++  java
  • tcp echo

    https://blog.csdn.net/fcx520/article/details/49889985

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
     
    static const int MAXPENDING = 5;
     
    static const int BUFSIZE = 1024;
     
    void HandleTCPClient(int clntSocket);
     
    void DieWithUserMessage(const char *msg, const char *detail);
     
    void DieWithSystemMessage(const char *msg);
     
    int main(int argc, char *argv[]) 
    {
        if(argc != 2)
              DieWithUserMessage("Parameter(s)", "<Server Port>");
     
        in_port_t servPort = atoi(argv[1]);
     
        // Create socket for incoming connections
        int servSock;
        if((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 ) {
            DieWithSystemMessage("Socket() failed");
        }
     
        // Construct local address structure
        struct sockaddr_in servAddr;            // Local address
        memset(&servAddr, 0, sizeof(servAddr));        // Zero out structure
        servAddr.sin_family =  AF_INET;            // IPv4 address family
        servAddr.sin_addr.s_addr = htonl(INADDR_ANY);    // Any incoming interface.
        servAddr.sin_port = htons(servPort);
        
        //Bind to the local address
        if (bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0 )
            DieWithSystemMessage("bind() failed");
     
        // Mark the socket so it will listen for incoming connections
        if (listen(servSock, MAXPENDING) < 0 )
            DieWithSystemMessage("listen() failed");
     
        for (;;) {                    // Run forever
            struct sockaddr_in clntAddr;        // Client address
            // Set length of client address structure (in-out parameter)
            socklen_t clntAddrLen = sizeof(clntAddr);
            
            // Wait for a client to connect
            int clntSock = accept(servSock, (struct sockaddr*) &clntAddr, &clntAddrLen);
            if ( clntSock < 0 )
                DieWithSystemMessage("accept() failed");
            
            // clntSock is connected to a client!
            
            char clntName[INET_ADDRSTRLEN];        // String to contain client address
            if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName, sizeof(clntName)) != NULL )
                printf("Handling client %s/%d
    ", clntName, ntohs(clntAddr.sin_port));
            else
                puts("Unable to get client address");
     
            HandleTCPClient(clntSock);
            
        }
    }
     
    void HandleTCPClient(int clntSocket)
    {
        char buffer[BUFSIZE];    // Buffer for echo string
        
        // Receive message from client.
        ssize_t numBytesRcvd = recv(clntSocket, buffer, BUFSIZE, 0);
        if ( numBytesRcvd < 0 )
            DieWithSystemMessage("recv() failed");
     
        // Send received string and receive again until end of stream.
        while ( numBytesRcvd > 0 ) {  //0 indicates end of stream.
            // Echo message back to client.
            ssize_t numBytesSent = send(clntSocket, buffer, numBytesRcvd, 0);
            if ( numBytesSent < 0 )
                DieWithSystemMessage("send() failed");
            else if ( numBytesSent != numBytesRcvd )
                DieWithUserMessage("send()", "send unexpected number of bytes");
            
            // See if there is more data to receive.
            numBytesRcvd = recv(clntSocket, buffer, BUFSIZE, 0);
            if ( numBytesRcvd < 0 )
                DieWithSystemMessage("recv() failed.");
        }
        
        close(clntSocket);     // Close client socket.
    }
     
    void DieWithUserMessage(const char *msg, const char *detail) 
    {
        fputs(msg, stderr);
        fputs(": ", stderr);
        fputs(detail, stderr);
        fputc('
    ', stderr);
        exit(1);
    }
     
    void DieWithSystemMessage(const char *msg) 
    {
        perror(msg);
        exit(1);
    }
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    void DieWithUserMessage(const char *msg, const char *detail) {
        fputs(msg, stderr);
        fputs(": ", stderr);
        fputs(detail, stderr);
        fputc('
    ', stderr);
        exit(1);
    }
     
    void DieWithSystemMessage(const char *msg) {
        perror(msg);
        exit(1);
    }
    
    int main(int argc, char **argv){
     
        if ( argc < 3) {
            DieWithUserMessage("Parameter(s)", "<IP> <PORT> [<MESSAGE>]");
        }
     
        // Create local socket
        int sock;
        if ( ( sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) {
            DieWithSystemMessage("socket() failed.");
        } 
     
        int servPort = atoi(argv[2]);
     
        // Create server address
        struct sockaddr_in servAddr;
        memset( &servAddr, 0, sizeof(servAddr) );
        servAddr.sin_family = AF_INET;
        servAddr.sin_port = htons(servPort);
        inet_pton(AF_INET, argv[1], &servAddr.sin_addr.s_addr);
     
        // Connect to server
        if ( (connect( sock, (struct sockaddr *) &servAddr, sizeof(servAddr))) < 0  ) {
            DieWithSystemMessage("connect() failed.");
        }
     
        // Send Message to server
        ssize_t sentByteLen = send( sock, argv[3], sizeof(argv[3]), 0);
        if ( sentByteLen > 0 ) {
            printf("send: %s	bytes:%d
    ", argv[3], sentByteLen);
        } else {
            DieWithSystemMessage("send() failed");
        }
        
        // Receive Message from server
        char recvBuf[sentByteLen];
        ssize_t recvByteLen = recv( sock, recvBuf, sentByteLen, 0);
        if ( recvByteLen > 0 ) {
            printf("recv: %s	bytes:%d
    ", recvBuf, recvByteLen);
        } else {
            DieWithSystemMessage("recv() failed");
        }
     
        close(sock);
        
        printf("end!
    ");
    }
  • 相关阅读:
    Perl分片技术
    Perl的time、localtime和gmtime函数
    Perl文件名通配和文件查找
    Perl复制、移动、重命名文件/目录
    Perl文件、目录常用操作
    Perl文件测试操作和stat函数
    Linux终端会话实时共享(kibitz)
    Perl的IO操作(2):更多文件句柄模式
    Perl的IO操作(1):文件句柄
    Perl文件句柄相关常量变量
  • 原文地址:https://www.cnblogs.com/soul-stone/p/10721697.html
Copyright © 2011-2022 走看看