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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.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
套接字send()和recv()不工作_C_Sockets - Fatal编程技术网

套接字send()和recv()不工作

套接字send()和recv()不工作,c,sockets,C,Sockets,我编写了一个简单的代码来发送和接收数据,但在接收数据时得到的错误是-1。发送没有给出任何错误 服务器代码: struct sockaddr_in name; char *buf; int main(int agrc, char** argv) { int sock, new_sd; //sock is this socket, new_sd is connection socket int adrlen, cnt; name.sin_family=AF_INET; name.sin_p

我编写了一个简单的代码来发送和接收数据,但在接收数据时得到的错误是-1。发送没有给出任何错误

服务器代码:

struct sockaddr_in name;
char *buf;

int main(int agrc, char** argv) {

int sock, new_sd;   //sock is this socket, new_sd is connection socket
int adrlen, cnt;

name.sin_family=AF_INET;
name.sin_port=htons(6050);

sock = socket(AF_INET, SOCK_STREAM, 0);

if (sock < 0) {
    printf("\nserver socket failure ");
    printf("\nServer: ");
    exit(1);
}
printf("sockfd=%d\n",sock);
adrlen=(socklen_t)sizeof(name);
if((bind (sock,(struct sockaddr *)&name,adrlen)) < 0)
   printf("\nBind failure");
if(listen(sock, 5) < 0)
   printf("\nlisten error ");
printf("listen done\n");
while(1) {
    if( new_sd =(accept(sock,(struct sockaddr *)&name,(socklen_t*)&adrlen)) < 0) {
        printf("\nserver accept failure ");
        exit(1);
    }
    printf("new_sd=%d",new_sd);

    buf = (char *)malloc(14);
    if((recv(new_sd,(void *)buf,14,MSG_WAITALL)) < 0){
       printf("\nError receiving data ");
        exit(1);
    }
}   //end while
return 0;
}
客户端代码:

char buf[14];
struct sockaddr_in name;

int main(int agrc, char** argv) {

int sock, new_sd, adrlen, cnt;
name.sin_family=AF_INET;
name.sin_port=htons(6050);

sock = socket(AF_INET, SOCK_STREAM, 0);

if (sock < 0) {
   printf("\nserver socket failure ");
   printf("\nServer: ");
    exit(1);
}

//stuff for server socket
printf("sock is %d\n",sock);
adrlen=(socklen_t)sizeof(name);


if((connect(sock,(struct sockaddr *)&name,adrlen)) < 0) {
    printf("\nclient connection failure ");
    exit(1);
}

printf("\nSuccessful connection from client 1");

strcpy(buf,"\nclientsend");

if((send(sock,(void *)buf,strlen(buf), 0)) < 0) {
    printf("\nError sending data from client 1 ");
    exit(1);
}
printf("\nExiting normally");
return 0;
}

我认为这可能是您的问题:您只从客户端发送了11个字节,但服务器在等待14个字节时阻塞了。客户端发送11个字节后,它退出,导致套接字层终止连接。但是,当启动关闭序列时,服务器仍在等待额外的3个字节


为了避免这种情况,您可以在客户端发送一个包含消息长度的固定大小的头。在服务器端,首先记录邮件头,提取邮件长度,然后使用此长度再次调用recv。

可能会出现许多错误。您应该使用GetLastError来了解有关的更多信息


另外,我注意到,在客户机和服务器示例中,您没有在名称中完全初始化sockaddr_,例如name.ai_addr保持原样。对于TCP SOCK_流,您需要连接到的地址。

代码太多,有关错误的详细信息太少。至少使用perror打印错误消息,以便您知道导致recv失败的原因。您忘记在结构中的结构sockaddr_中添加要连接到的地址。错误处理是。。。好printfoh否,致命故障;没问题,让我们继续…一旦收到一个或多个字节,recv就会解除阻止。它不会“阻止等待[全部]14个字节”。您的意思是WSAGetLastError。