2017-06-16 93 views
-1

刚刚开始C网络使用TCP/IP Sockets in C: Practical Guide for Programmers。我按照书中的代码示例:TCP/IP Echo-客户端挂起

#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这是第peice的书中代码是

TCPEchoClient.c

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

如果我们 编译 这 应用 为 TCPEchoClient, 我们 可以 与 的 呼应 服务器 与 互联网 地址 169.1.1.1 作为 如下沟通 : %TCPEchoClient i。一世。 我“回声 这个!” 收到' 回声 这!

但是,当我从书中使用这些参数,我的程序只是挂起,当我运行它。我很困惑,为什么会发生这种情况?任何帮助表示赞赏。

+0

您提供的IP地址当然应该是运行服务器的主机地址。你*在某处运行服务器? –

+0

如果您希望其他人阅读它,请缩进您的代码。 – Lundin

+0

我会继续并缩进它。 – Hexodus

回答

2

哦,你似乎忘了一些非常重要的东西,当你复制代码时:其实发送东西到服务器。

某处

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

和第二天线之间

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

你似乎缺少实际的代码,将数据发送到服务器。

由于您没有发送任何内容,因此服务器只需耐心等待其recv呼叫。同时,你被困在你自己的recv呼叫等待从未发生的答复。这导致死锁,其中服务器和客户端都在等待其他对等方发送内容。

+0

感谢我愚蠢的错误。 – Hexodus