简体   繁体   English

Unix C套接字服务器不接受连接

[英]Unix C socket server not accepting connections

Here's the deal, I'm writing a simple tcp socket server in C (with unix system calls) that I'm not able to get to accept connections. 这是交易,我在C中编写一个简单的tcp套接字服务器(使用unix系统调用),我无法接受连接。

From what I can tell, I get through the server initialization just fine, but when I try to connect to the port that I print out (see code below) it refuses as if nothing is there. 据我所知,我完成了服务器初始化,但是当我尝试连接到我打印出来的端口时(见下面的代码),它拒绝好像没有任何东西。

More to the point, when I netstat that port isn't even in use. 更重要的是,当我netstat该端口甚至没有使用。 I'm not throwing any errors with my current set up, I'm all dried up for ideas. 我现在的设置没有任何错误,我全都干涸了。

int main(){

    int sock_fd;
    int conn_fd;
    struct sockaddr_in serv_addr;
    struct sockaddr_in cli_addr;
    socklen_t* serlen;
    socklen_t* clilen;
    clilen  = malloc(sizeof(socklen_t));
    serlen  = malloc(sizeof(socklen_t));
    *serlen = sizeof(serv_addr);
    *clilen = sizeof(cli_addr);

    /*=============================Create Socket=============================*/


        //Create Socket
        sock_fd = socket(AF_INET, SOCK_STREAM, 0);
            if(sock_fd<0){
                fprintf(stderr,"error creating socket\n");
                exit(1);}

        //Initialize Server Address Struct
        bzero((char *) &serv_addr, *serlen);
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        serv_addr.sin_port = 0;

    /*=============================Bind Address==============================*/

        //Bind socket to an address
        if(bind(sock_fd,(struct sockaddr*)&serv_addr,*serlen)<0){
            fprintf(stderr,"error binding\n");
            exit(1);}

        //Get socket data
        if(getsockname(sock_fd,(struct sockaddr*)&serv_addr, serlen)<0){
            fprintf(stderr,"error with socket name");
            exit(1);}

    /*=============================Server Started============================*/

        //Listen for connections
        listen(sock_fd,32);

        //Print port
        printf("%i", serv_addr.sin_port);

        conn_fd = accept(sock_fd,(struct sockaddr*)&cli_addr,clilen);

        /**Do something exciting with my new connection**/

}

Are you really trying to listen on port zero? 你真的想听零端口吗? Try a high port number, preferably > 1024. /etc/services will give a hint about free ports - but it only a set of comments, those port numbers are not enforced. 尝试一个高端口号,最好是> 1024./ etc / services会给出一个关于空闲端口的提示 - 但它只是一组注释,这些端口号没有强制执行。

Edit: another hint. 编辑:另一个提示。 The port number should be in network order , so the assignment should use htons() . 端口号应按网络顺序排列 ,因此赋值应使用htons() It could be that the "random numbers" you are getting are simple numbers that appear garbled because you might be on a little-endian machine (like Intel). 可能是你得到的“随机数字”是简单的数字,因为你可能在一个小端机器(如英特尔)上显得乱码。 When you print them, convert them back using ntohs() . 打印时,使用ntohs()将它们转换回来。

props to @askmish for inspiring this one 为@askmish提供鼓舞人心的道具

    //Print port
    printf("%i", serv_addr.sin_port);

becomes

    //Print port
    printf("%i", htons(serv_addr.sin_port));

In your code: 在你的代码中:

  • Instead of: 代替:

     serv_addr.sin_port = 0; 

    try this: 尝试这个:

     serv_addr.sin_port=htons(2056);//Any port no. 
  • Instead of: 代替:

      listen(sock_fd,32); 

    try this: 尝试这个:

      if(listen(sock_fd,SOMAXCONN)<0)//Just to be sure that you are considering max. no. of requests { fprintf(stderr,"error with listen"); exit(1);} 
  • Also for: 也用于:

      conn_fd = accept(sock_fd,(struct sockaddr*)&cli_addr,clilen); 

    Add this: 添加这个:

      if(conn_fd <0) { //handle the error here } 

If none of these solve your issues, then, there might be problem with the client code or your server environment. 如果这些都无法解决您的问题,那么客户端代码或服务器环境可能会出现问题。

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

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