首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C- Webserver中的Socket编程

C- Webserver中的Socket编程
EN

Stack Overflow用户
提问于 2010-09-08 16:26:53
回答 1查看 2.7K关注 0票数 2

当涉及到套接字编程时,我是一个初学者。我目前正在尝试开发一个web服务器,可以从浏览器中为基本的GET和POST http请求提供服务。我使用我的笔记本电脑作为服务器和客户端。所以我的想法是,我应该能够在火狐中输入http://127.0.0.1:PORT/,我的web服务器应该能够连接到那个端口并开始交谈。

到目前为止,我写的代码似乎不起作用。我正在使用ConText和Cygwin,以便能够在Windows中运行和执行程序。我也从我正在使用的端口中删除了防火墙。但是,seem服务器似乎无法识别该连接。

你知道为什么吗?我附上了我到目前为止写的代码。任何帮助都将不胜感激。目前,程序还没有通过“侦听端口..”。我已经隔离了故障,似乎它卡住了我已经强调的声明。

代码语言:javascript
复制
/* Webserver Code  */

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>             // for the use of inet_ntop()
#include <netinet/in.h>

#define PORT "1500"                // the port we will connect to
#define BACKLOG 10                 // how many pending connections the queue will hold

void *get_in_addr(struct sockaddr *sa);


int main (void) {

    int sockfd, new_fd=0;
    int status;
    struct addrinfo hints;
    struct addrinfo *servinfo;      
    struct sockaddr_storage their_addr;
    char s[INET6_ADDRSTRLEN];
    socklen_t addr_size;            

    FILE *errors;

    memset(&hints, 0, sizeof hints); 
    hints.ai_family = AF_UNSPEC;     
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_flags = AI_PASSIVE;     


    errors = fopen("Errors.txt", "w");
        fprintf(errors,"testing...\n");
    if(errors == NULL){
        fprintf(stderr, "Cant open output file\n");
    }
    if ((status = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
        fprintf(errors, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

    // make a socket
    // socket() function returns a socket file descriptor called sockfd
    sockfd = socket(servinfo->ai_family, servinfo->ai_socktype, 0);

    // bind to the port we passed in to getaddrinfo()
    if((bind(sockfd, servinfo->ai_addr, servinfo->ai_addrlen))== -1) {
        fprintf(errors, "Binding Failed\n");
        exit(1);
    }                                   

    // listen on the port
    if((listen(sockfd, BACKLOG))== -1) {
      fprintf(errors, "Listening failed\n");
      exit(1);
    } else {
      printf("Listening on port...\n");
 }

    //now accept an incoming connection
    addr_size = sizeof(their_addr);
    **new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size)**;
 printf( "new_fd = %d\n", new_fd);
    if(new_fd == -1) {
      fprintf(errors, "Accepting failed\n");
      exit(1);
 } else {
  printf("The Accepting worked\n");
 }

     //Prints out the address of the connector
    inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof(s));
    printf("server: got connection from %s\n", s);
    fclose(errors);

 return 0;
}



// To obtain the client's information
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }

    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-08 18:08:46

getaddrinfo()可能会返回多个条目,包括IPv6条目。这可能是你的问题所在。

注意:在getaddrinfo()调用后,错误检查丢失。

如果您希望浏览器能够连接到"127.0.0.1:PORT",您应该:

通过

  • bind()连接到INADDR_LOOPBACK和端口,而不需要通过对getaddrinfo()getaddrinfo()
  • properly设置调用获得任何帮助,将ai_family设置为AF_INET (这样您就只有IPv4了)。

请注意,如果getaddrinfo()的第一个参数为NULL,则返回的地址将设置为INADDR/IN6ADDRANY,这意味着您的服务器也将接受来自外部的连接。要限制到localhost的连接,请将第一个参数设置为"localhost“,而不是NULL。

为了保存这段代码,下面是我的getaddrinfo()结果转储代码:

代码语言:javascript
复制
    struct addrinfo hints, *aires=0, *ai;
    int i;
    int rc;

    memset( &hints, 0, sizeof(hints) );
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;
    rc = getaddrinfo( "localhost", "1500", &hints, &aires );
    if (rc == 0) {
        for ( ai=aires, i=0; ai; ai=ai->ai_next, i++ ) {
            printf( "gai: ai[%d].ai_canonname = %s\n", i, ai->ai_canonname );
            printf( "gai: ai[%d].ai_flags     = %d\n", i, ai->ai_flags );
            printf( "gai: ai[%d].ai_family    = %d (%s)\n", i, ai->ai_family, get_family_name(ai->ai_family) );
            printf( "gai: ai[%d].ai_socktype  = %d\n", i, ai->ai_socktype );
            printf( "gai: ai[%d].ai_protocol  = %d (%s)\n", i, ai->ai_protocol, get_proto_name(ai->ai_protocol) );
            printf( "gai: ai[%d].ai_addrlen   = %d\n", i, (int)ai->ai_addrlen );

            if (ai->ai_family == AF_INET) {
                struct sockaddr_in *sa = (struct sockaddr_in *)ai->ai_addr;
                printf( "gai: ai[%d].ai_addr[IPv4].sin_family = %d (%s)\n", i, sa->sin_family, get_family_name(sa->sin_family) );
                printf( "gai: ai[%d].ai_addr[IPv4].sin_port   = %d\n", i, ntohs(sa->sin_port) );
                printf( "gai: ai[%d].ai_addr[IPv4].sin_addr   = %s\n",
                    i, inet_ntop(AF_INET, &sa->sin_addr, buf, sizeof(buf)) );
            } else if (ai->ai_family == AF_INET6) {
                struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ai->ai_addr;
                printf( "gai: ai[%d].ai_addr[IPv6].sin6_family   = %d (%s)\n", i, sa->sin6_family, get_family_name(sa->sin6_family) );
                printf( "gai: ai[%d].ai_addr[IPv6].sin6_port     = %d\n", i, ntohs(sa->sin6_port) );
                printf( "gai: ai[%d].ai_addr[IPv6].sin6_flowinfo = %d\n", i, sa->sin6_flowinfo );

                printf( "gai: ai[%d].ai_addr[IPv6].sin6_addr     = %s\n",
                    i, inet_ntop(AF_INET6, &sa->sin6_addr, buf, sizeof(buf)) );

                printf( "gai: ai[%d].ai_addr[IPv6].sin6_scope_id = %d\n", i, sa->sin6_scope_id );
            } else {
                printf( "gai: ai[%d].ai_addr = %p\n", i, ai->ai_addr );
            }
            printf("\n");
        }
    } else {
        printf( "gai: failed (%d)\n", rc );
    }
    freeaddrinfo( aires );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3665843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档