简体   繁体   English

c如何结束套接字读取

[英]c how to end socket reading

I am trying to read in the server until client sends done msg. 我试图在服务器中读取,直到客户端发送完成的味精。 which is myVar.id==-1. 这是myVar.id ==-1。 I read with an infinite loop in the server until it reads done flag from the client. 我在服务器中读取一个无限循环,直到它从客户端读取完成标志。 It reads until it gets done flag from client but it is not ending it is hanging there. 它读取直到从客户端获取完成标志,但并没有结束它挂在那里。 any idea? 任何想法?

//server

sockdesc = socket(AF_INET, SOCK_STREAM, 0);
if ( sockdesc < 0 )
{
  cout << "Error creating socket" << endl;
  exit(0);
}
else{
   if ( getaddrinfo("0.0.0.0", portnum, NULL, &myinfo) != 0 )
   {
      cout << "Error getting address" << endl;
      exit(0);
   }
}
if (bind(sockdesc, myinfo->ai_addr, myinfo->ai_addrlen) < 0 ){
  cout << "Error binding to socket" << endl;
  exit(0);
}
if ( listen(sockdesc, 1) < 0 ){
  cout << "Error in listen" << endl;
  exit(0);
}
connection = accept(sockdesc, NULL, NULL);
if ( connection < 0 ){
  cout << "Error in accept" << endl;
  exit(0);
}
while(true){
    value = read(connection, (char*)&myVar, sizeof(procd));
    if (value < 0) {
      perror("ERROR reading from socket");
      exit(1);
    }
    if(myVar.id==-1){
        close(connection);
        exit(0);
    }//if
    else{
        //display what received

    }           
}

//client
while ( getline (inputFile,line) )//read a line till EOF
{       
    write(sockdesc, (char*)&myVar, sizeof(procd));  
}
myVar.id=-1;
write(sockdesc, (char*)&myVar, sizeof(procd));

You must also handle the condition that your client can die unexpectedly... ... In which case READ would return a zero... 您还必须处理您的客户端可能意外死亡的情况... ...在这种情况下,READ将返回零...

Although you don't state what platform you are using, but the manual page on Linux says: If fildes refers to a socket, read() shall be equivalent to recv() with no flags set. 尽管您没有说明正在使用的平台,但是Linux上的手册页指出:如果fildes引用套接字,则read()等同于未设置标志的recv()。

And the manual page for recv() says: 并且recv()的手册页显示:

Return Value 返回值

Upon successful completion, recv() shall return the length of the message in bytes. 成功完成后,recv()将以字节为单位返回消息的长度。 If no messages are available to be received and the peer has performed an orderly shutdown, recv() shall return 0. Otherwise, -1 shall be returned and errno set to indicate the error. 如果没有消息可供接收并且对等方已执行有序的关闭,则recv()将返回0。否则,将返回-1并将errno设置为指示错误。

Your code doesn't handle the special condition where read returns zero. 您的代码无法处理读取返回零的特殊条件。

Your code doesn't really show enough... Note, sending structs over a socket is tricky, see this 您的代码显示得还不够……请注意,通过套接字发送结构很棘手,请参见

passing a struct over TCP (SOCK_STREAM) socket in C 在C中通过TCP(SOCK_STREAM)套接字传递结构

Here are some thoughts that might help... 这里有一些想法可能会有所帮助...

while ( getline (inputFile,line) ) {       
  write(sockdesc, (char*)&myVar, sizeof(procd));  
}
myVar.id=-1;
write(sockdesc, (char*)&myVar, sizeof(procd));

I'm assuming that myVar is declared as follows procd myVar; 我假设myVar声明如下procd myVar; on the stack? 在堆栈上?

I don't know what getline is doing, getline in stdio takes three arguments?What getline function are you using, if this is C this is likely a problem? 我不知道getline在做什么, stdio getline需要三个参数?您正在使用什么getline函数,如果是C,这可能是一个问题?

Where is getline putting the data, are one of the args to getline a pointer into myVar ? 哪里是函数getline把数据,是参数传递给一个getline指针到myVar

I also have to assume that these are on the same machine using the same compiler because sending a struct in binary won't work in some cases ie if you send it from x86 to a big endian machine or to a machine who packs the struct differently things will not work. 我还必须假定它们在使用相同编译器的同一台机器上,因为在某些情况下,以二进制形式发送结构将无法正常工作,即,如果将其从x86发送到大型字节序计算机或以不同方式打包结构的计算机事情不起作用。 See the link I posted to earlier. 请参阅我之前发布的链接。

With all that said and you really need to do more error checking. 综上所述,您确实需要执行更多错误检查。 I'd try the following to see if it works then work from there... 我会尝试以下方法,看看是否可行,然后从那里开始工作...

struct test {
  ssize_t id;
};
struct test payload;
payload.id=-1;  
write(sockdesc, (char*)&payload, sizeof(test));  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM