Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TCP客户端连接函数的C问题_C_Sockets_Connect - Fatal编程技术网

TCP客户端连接函数的C问题

TCP客户端连接函数的C问题,c,sockets,connect,C,Sockets,Connect,我在让connect函数工作时遇到问题。这段代码将被编译,但当我运行程序时,我得到一个错误,即我将无效参数传递给connect。我已经尝试了我能想到的一切,但似乎没有任何效果。谢谢你的帮助 /* * Creates a streaming socket and connects to a server. * * serverName - the ip address or hostname of the server given as a string * port - the po

我在让connect函数工作时遇到问题。这段代码将被编译,但当我运行程序时,我得到一个错误,即我将无效参数传递给connect。我已经尝试了我能想到的一切,但似乎没有任何效果。谢谢你的帮助

/*
* Creates a streaming socket and connects to a server.
*
* serverName - the ip address or hostname of the server given as a string
* port       - the port number of the server
* dest       - the server's address information; the structure should be created
* with    information
*              on the server (like port, address, and family) in this function call
*
* return value - the socket identifier or a negative number indicating the error if
* a   connection could not be established
*/

int createSocket(char * serverName, int port, struct sockaddr_in ** dest){

    int sockfd;
    struct hostent *hostptr;

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){

        error("ERROR socket creation:");
        return -1;
    }

    /* gethostbyname: get the server's DNS entry */
    hostptr = gethostbyname(serverName);

    if (hostptr == NULL) {

        fprintf(stderr,"ERROR, no such host as %s\n", serverName);
        return -1;
    }

    /* build the server's Internet address */
    memset((void *) (*dest),0,(size_t)sizeof(dest));
    fprintf(stderr,"%d\n",(*dest)->sin_family);

    fprintf(stderr,"%d\n",ntohs((*dest)->sin_port));

    (*dest)->sin_family = (short)(AF_INET);
    memcpy((void *) &(*dest)->sin_addr,hostptr->h_addr,hostptr->h_length);
    (*dest)->sin_port = htons((u_short)port);

    fprintf(stderr,"%d\n",(*dest)->sin_family);

    fprintf(stderr,"%d\n",ntohs((*dest)->sin_port));

    /* connect: create a connection with the server*/ 
    //if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0) 
    if (connect(sockfd, (struct sockaddr *)(*dest), sizeof((*dest))) < 0){

        error("ERROR connecting");
        return -2;
    }

    return sockfd;
}
/*
*创建流式套接字并连接到服务器。
*
*serverName—以字符串形式给出的服务器的ip地址或主机名
*port—服务器的端口号
*dest-服务器的地址信息;应该创建结构
*带着信息
*在服务器上(如端口、地址和系列)执行此函数调用
*
*返回值-套接字标识符或一个负数,表示如果
*无法建立连接
*/
int createSocket(char*serverName,int-port,结构sockaddr\u in**dest){
int-sockfd;
结构主机*hostptr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0){
错误(“错误套接字创建:”;
返回-1;
}
/*gethostbyname:获取服务器的DNS条目*/
hostptr=gethostbyname(serverName);
if(hostptr==NULL){
fprintf(stderr,“错误,没有像%s\n这样的主机”,serverName);
返回-1;
}
/*构建服务器的Internet地址*/
memset((void*)(*dest),0,(size_t)sizeof(dest));
fprintf(stderr,“%d\n”,(*dest)->sin_族);
fprintf(stderr,“%d\n”,ntohs((*dest)->sin_port));
(*dest)->sin_family=(短)(AF_INET);
memcpy((void*)和(*dest)->sin_addr,hostptr->h_addr,hostptr->h_长度);
(*dest)->sin_端口=htons((u_短)端口);
fprintf(stderr,“%d\n”,(*dest)->sin_族);
fprintf(stderr,“%d\n”,ntohs((*dest)->sin_port));
/*连接:创建与服务器的连接*/
//if(connect(sockfd,&serveraddr,sizeof(serveraddr))<0)
if(connect(sockfd,(struct sockaddr*)(*dest),sizeof((*dest)))<0){
错误(“连接错误”);
返回-2;
}
返回sockfd;
}

您正在传递
sizeof(*dest)
作为地址的大小,但是
dest
不是地址,只是指向它的指针


如果可能的话,我建议避免使用双指针,因为它们通常会引起混淆。

使用双指针时,请尝试使用
sizeof(**dest)
作为地址大小。

*dest
memset
,你应该仔细看看它。还有
sizeof
的另一个用法,我知道你说的应该是memset((void*)(*dest),0,(size_t)sizeof((*dest));但这并不能真正解决问题。或者更好的是
memset((void*)(*dest),0,(size_t)sizeof((**dest))
使用
dest
还有其他可能导致未定义行为的潜在问题。比如,您在哪里为
*dest
分配内存?更大的问题是memset将0复制到指针,这意味着后续行将访问空指针。我认为情况并非如此,因为当我从main访问指针时,createSocket中分配的值是正确的。