简体   繁体   English

Linux上带有accept()的分段错误

[英]segmentation fault with accept() on linux

I have a problem with a client/server programm on linux. 我在Linux上的客户端/服务器程序有问题。 I wrote a server programm wich is sending data cyclic to one conneted client. 我编写了一个服务器程序,该程序将循环数据发送到一个连接的客户端。 Now I want to detect, if the client close the connection to the server. 现在,我要检测客户端是否关闭与服务器的连接。 When the connection is closed from the client, i want to wait with accept(...) for an new connection. 当从客户端关闭连接时,我想与accept(...)等待新的连接。 Here the critical parts of my code: 这是我代码的关键部分:

error = send(client_sock, Zeichen, 1, MSG_NOSIGNAL);
if(error < 0)
{
    connected = 0;
    printf("Error, write on TCP Socket failed!!! Reconnecting... \r\n");
    close(serverSocket);
    initServer();
}

int initServer(void)
{
    int *new_sock;
    socklen_t size;
    struct sockaddr_in server, client;

    serverSocket = socket(AF_INET , SOCK_STREAM , 0);       //Create socket
    if (serverSocket == -1)
    {
      printf("Could not create socket \r\n");
      return 0;
    }else
    {
      printf("Socket created \r\n");
    }

    server.sin_addr.s_addr = inet_addr(IPAdresse);
    server.sin_family = AF_INET;
    server.sin_port = htons(TCPPort);

    if(bind(serverSocket,(struct sockaddr *)&server , sizeof(server)) < 0)  
    {
      printf("bind failed. Error \r\n");
      return 0;
    }else
    {
      printf("bind done \r\n");
    }

    listen(serverSocket, 1);        
    printf("Waiting for incoming connections... \r\n");
    size = sizeof(sockaddr_in);
    printf("size of sockaddr_in: %i \r\n", size);
    client_sock = accept(serverSocket, (struct sockaddr *)&client, &size);

   if (client_sock < 0)
   {
     printf("accept failed \r\n");
     return 0;
   }else
   {
    connected = 1;
    return 1;
   }
}

The first time it works fine, I can connect and can send data over the socket. 第一次正常运行时,我可以连接并可以通过套接字发送数据。 When the client close the connection the error is detected, I close the socket an start the server again to wait for a new connection. 当客户端关闭连接时,检测到错误,我关闭套接字,然后再次启动服务器以等待新的连接。 But than I get a segmentation fault when I do the accept(..) for the second time!!! 但是,当我第二次执行accept(..)时,我得到了一个分段错误!!! Can someone help me please!!! 有人能帮助我吗!!! Thanks a lot! 非常感谢!

It's not clear what you're doing when the client connection closes. 尚不清楚客户端连接关闭时您正在做什么。 I see no loop in your code, and yet you're suggesting that accept() is called more than once. 我在您的代码中看不到循环,但是您建议多次调用accept()

Without seeing the rest or the code, I can only speculate that: 在没有看到其余部分或代码的情况下,我只能推测:

  • you're repeatedly calling initServer() , hence attempting to recreate the same server socket over and over (which, of course, would be bad), 您反复调用initServer() ,因此尝试一遍initServer()重新创建相同的服务器套接字(这当然是不好的),

or 要么

  • you're calling accept() again somewhere else in your code, most likely with corrupt arguments. 您在代码的其他位置再次调用了accept() ,很可能是参数损坏了。

At the very least, what your server-side code should do is initialize the server socket once, then loop around accept() , like so: 至少,服务器端代码应该做的是初始化服务器套接字一次,然后围绕accept()进行循环,如下所示:

  • call socket() once 调用一次socket()
  • call bind() once 一次调用bind()
  • call listen() once 调用一次listen()
  • then in a loop: 然后循环:
    • call accept() , this call will block until a client connects, and then return a connected socket, 调用accept() ,该调用将阻塞,直到客户端连接,然后返回连接的套接字,
    • do whatever you need to do with that (connected client) socket 使用该(连接的客户端)套接字做您需要做的一切
    • resume with the loop 继续循环

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

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