套接字服务器程序不会在“接受”阶段等待客户端并在无限循环中运行

时间:2014-06-30 10:44:20

标签: c sockets socketserver

如果我运行我的以下套接字服务器程序,它不会等待来自客户端的连接"接受"阶段,而不是它开始在无限循环中运行。我的程序保存为server.c,我尝试使用以下命令在命令行中运行它:

$ ./server /tmp/socket

完整套接字服务器代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>     



int server (int client_socket)
{
    while (1) {
        int length;
        char* text;             

        if (read (client_socket, &length, sizeof (length)) == 0) 
            return 0;

        text = (char*) malloc (length);

        read (client_socket, text, length);
        printf ("%s\n", text);

        free (text);

        if (!strcmp (text, "quit"))
            return 1;
    }
}


int main (int argc, char* const argv[])
{
    const char* const socket_name = argv[1];                
    int socket_fd; 
    struct sockaddr_un name; 
    int client_sent_quit_message;           
    socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0); 

    name.sun_family = AF_LOCAL; 
    strcpy (name.sun_path, socket_name); 
    bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name)); 

    listen (socket_fd, 5); 

    do {
        struct sockaddr_un client_name;

        socklen_t client_name_len;
        int client_socket_fd;

        client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
        client_sent_quit_message = server (client_socket_fd); 

        close (client_socket_fd);
      }
    while (!client_sent_quit_message);

    close (socket_fd);
    unlink (socket_name);
    return 0;
}   

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>     
int server (int client_socket)
{
  while (1) {
    int length=30;
    char* text;             
    text = (char*) malloc (length);
    if (read (client_socket, text, length) == 0) 
        return 0;
    printf ("%s\n", text);
    if (!strcmp (text, "quit"))
    {
        free(text);
        return 1;
    }
    free (text);
  }
}
int main (int argc, char* const argv[])
{ 
  const char* const socket_name = argv[1];                
  int socket_fd; 
  struct sockaddr_un name; 
  int client_sent_quit_message;           
  socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0); 
  name.sun_family = AF_LOCAL; 
  strcpy (name.sun_path, socket_name); 
  bind (socket_fd, (struct sockaddr*)&name, SUN_LEN (&name)); 
  listen (socket_fd, 5); 
  do {
    struct sockaddr_un client_name;
    int client_name_len=sizeof(struct sockaddr_un);
    int client_socket_fd;
    client_socket_fd = accept (socket_fd,(struct sockaddr *)&client_name, &client_name_len);
    client_sent_quit_message = server (client_socket_fd); 
    close (client_socket_fd);
}
  while (!client_sent_quit_message);
  close (socket_fd);
  unlink (socket_name);
  return 0;
}

错误在于你没有输入sockaddr_unsockaddr *的接受函数,那么第三个参数无效,我们不能直接输入socklen_t到{ {1}}而是将值存储为整数,并将指针作为参数。

不要使用这种长度:
read(client_socket,&amp; length,sizeof(length));

它从套接字读取输入并尝试分配&amp; length,然后尝试将值存储在字符变量中,根据长度给出错误或读取长度的时间。