TCP / IP Echo-Client挂起

时间:2017-06-16 13:14:49

标签: c linux sockets tcp

刚刚使用TCP/IP Sockets in C: Practical Guide for Programmers启动了C网络。我已经按照书中的代码示例进行了操作:

#include <stdio.h> //for printf and fprintf
#include <sys/socket.h> // for socket() , connect(),send(), and recv()
#include <arpa/inet.h> //for sockaddr_in and inet_addr()
#include <stdlib.h> //for atoi()
#include <string.h> //for memset()
#include <unistd.h> //for close()

#define RCVBUFSIZE 32 //size of recieve  buffer

void DieWithError(char *errorMessage); //error handling function

int main (int argc , char *argv[])
{
    int sock; /*SOcket descriptor*/
    struct sockaddr_in echoServAddr; /*Echo server address*/
    unsigned short echoServPort; /*Echo server port*/
    char *servIP; /*Server IP address (dotted quad)*/
    char *echoString; /*string to send to echo server*/
    char echoBuffer[RCVBUFSIZE]; /*Buffer for echo string*/
    unsigned int echoStringLen; /*Length of string to echo*/
    int bytesRcvd , totalBytesRcvd; /*Bytes read in single recv() and total bytes read*/

    if( (argc < 3) || (argc > 4) ) /*Test for correct number of arguments */
    {
        fprintf(stderr , "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]);
        exit(1);
    }

    servIP = argv[1]; /*first arg: server IP address (dotted quad)*/
    echoString = argv[2]; /*Second arg: string echo*/

    if(argc == 4)
        echoServPort = atoi(argv[3]); /*Use given port, if any*/
    else
        echoServPort = 7; /*7 is the well-known port for the echo service*/

    /*Create a reliable, stream socket using TCP*/
    if( (sock = socket(PF_INET , SOCK_STREAM , IPPROTO_TCP)) < 0 )
        DieWithError("Connect() failed");

    /*COnstruct the server address structure*/
    memset(&echoServAddr , 0 , sizeof(echoServAddr)); /*zero out structure*/
    echoServAddr.sin_family = AF_INET; /*Internet address family*/
    echoServAddr.sin_addr.s_addr = inet_addr(servIP); /*Server IP address*/
    echoServAddr.sin_port = htons(echoServPort); /*Server port*/

    /*Establish the connection the echo server*/
    if(connect(sock, (struct sockaddr *) &echoServAddr , sizeof(echoServAddr)) < 0)
        DieWithError("send() sent a differnt number of bytes than expected");

    /*Recieve the same string back from the server*/
    totalBytesRcvd = 0;
    printf("Recieved: "); /*setup to print the echoed string*/
    while(totalBytesRcvd < echoStringLen)
    {
        /* Recieve up to the bufffer size (minus 1 to leave space for a null terminator)
           bytes from the sender*/
        if( (bytesRcvd = recv(sock , echoBuffer , RCVBUFSIZE - 1, 0)) <=0)
            DieWithError("recv() failed or connection closed prematurely");
        totalBytesRcvd += bytesRcvd; /*Keep tally of total bytes*/
        echoBuffer[bytesRcvd] = '\0'; /*Terminate the string*/
        printf(echoBuffer); /*Print the echo buffer*/
    }

    printf("\n"); /*Print a final linefeed*/
    close(sock);
    exit(0);
}

void DieWithError(char *errorMessage)
{   perror(errorMessage);
    exit(1);
}

预订代码(无法从书中复制,因为我拥有合法的实体副本,但这里是免费在线pdf版本的链接)book这是代码的第一个代码在这本书中

  

TCPEchoClient.c

然后,本书告诉我用这些参数编译和执行程序:

  

如果我们   编   这个   应用   如   TCPEchoClient,   我们   能够   通信   同   一个   回声   服务器   同   互联网   地址   169.1.1.1   如   如下:   %TCPEchoClient   169。   一世。一世。   我&#34; Echo   这个&#34!;   收到&#39;   回声   这个!

然而,当我使用书中的这些参数时,我的程序只是在我运行时挂起。我很困惑为什么会这样?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

哦,当你复制代码时,你似乎已经忘记了一些非常重要的东西:实际上向服务器发送东西。

介于

之间
if(connect(sock, (struct sockaddr *) &echoServAddr , sizeof(echoServAddr)) < 0)

和下一行

DieWithError("send() sent a differnt number of bytes than expected");

您似乎错过了将数据发送到服务器的实际代码。

由于您没有发送任何内容,服务器只会耐心等待recv电话。与此同时,您将陷入自己的recv呼叫,等待从未发生的回复。这会导致死锁,其中服务器和客户端都在等待另一个对等方发送内容。

相关问题