首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的网络编程程序-不工作

简单的网络编程程序-不工作
EN

Stack Overflow用户
提问于 2010-12-06 18:35:58
回答 4查看 683关注 0票数 0

我刚开始学习网络编程。我的第一个程序非常简单:一旦连接,服务器程序就会显示客户机程序发送的内容(字符)。一切都很正常直到它们连接起来。首先,我使用putc执行发送循环:

代码语言:javascript
复制
while((c = getc(stdin)) != '\n')
    putc(c, (FILE *) connection);

以及带有getc的接收者:

代码语言:javascript
复制
while((c = getc((FILE *) connection)) != '\n')
    putc(c, stdout);

一旦连接上,我就会有一个分割错误。然后我试着用send:

代码语言:javascript
复制
while(1) {
    memset(str, null, strlen(memset));

    while(((c = getc(stdin)) != '\n') && sizeof(str) <= 100) {
        strcat(str, (char *) c);       // cast to char * is to make gcc happy
    }

    send(connection, (void *) str, sizeof(str), (int) NULL);
}

和重新登记:

代码语言:javascript
复制
while((stat = recv(connection,str,100,0)) != 0) {
    printf("\nReceived %d bytes of data from %s: %s", stat, client, str);
}

现在recv()继续返回-1,errno设置为107。这是什么意思?我哪里做错了?顺便说一下,我和gcc一起使用Linux。

非常感谢你的帮助!)

编辑:--即使我使用getc()和putc(),我得到了套接字()的连接,然后连接()(客户机)。服务器在获得连接(与套接字())绑定()s之后,然后侦听()s和接受()s。

以下是服务器代码:

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>

void main(int argc, char *argv[])
{
    struct addrinfo hint;
    struct addrinfo *servinfo, *count;
    struct sockaddr_storage conn_addr;
    struct sockaddr host;

    int connessione;
    int stat;
    int conn_sock;
    int conn_size;
    int success;
    int c;

    char t[INET_ADDRSTRLEN];
    char str[100];
    char *client;

    if(argc != 1 && strcmp(argv[1], "aiuto") == 0) {
        printf("%s(porta)\n",argv[0]);
        printf("porta: specifica su quale porta installarsi\n");
        exit(-1);
    }

    memset(&hint,0,sizeof hint);


    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_flags = AI_PASSIVE;

    if((stat = getaddrinfo(NULL, argv[1], &hint, &servinfo)) != 0)  {
        printf("\n[FATAL]: getaddrinfo: %s",gai_strerror(stat));
        exit(-1);
    }

    success = 0;

    for(count = servinfo; count != NULL; count = count->ai_next) {
        if((connessione = socket(count->ai_family, count->ai_socktype, count->ai_protocol)) <= 0)
            continue;

        if((stat = bind(connessione, count->ai_addr, count->ai_addrlen)) == 0) {
            success = 1;
            break;
        }
        else {
            continue;
        }
    }

    if(success == 0) {
        printf("\n[FATAL]: Unable to bind()!\n");
        exit(-1);
    }
    else {
        printf("\n[DEBUG]: %s: listening...", argv[0]);
    }

    servinfo = count;
    freeaddrinfo(count);

    if(listen(connessione, 20) == -1) {
        printf("\n[FATAL]: listen: %s (%d)\n", gai_strerror(errno), errno);
        close(connessione);
        exit(-1);
    }

    conn_size = sizeof(conn_addr);

    if(accept(connessione, (struct sockaddr *) &conn_addr, &conn_size) == -1) {
        printf("\n[FATAL]: accept: %s", gai_strerror(errno));
        close(connessione);
        exit(-1);
    }

    client = (char *) malloc(INET_ADDRSTRLEN * sizeof(char));
    client = inet_ntop(servinfo->ai_family, &conn_addr, t, INET_ADDRSTRLEN);

    printf("\n[DEBUG]: %s: connection accepted: %s", argv[0], client);

    while((stat = recv(connessione,str,100,0)) != 0) {
        printf("\nReceived %d bytes of data from %s: %s", stat,client, str);
    }

    printf("\n[DEBUG]: connection closed by %s\n",client);
}

以及客户的代码:

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>

void main(int argc, char *argv[])
{
    struct addrinfo hint;
    struct addrinfo *servinfo, *count;
    struct sockaddr_storage conn_addr;
    struct sockaddr host;

    int connessione;
    int stat;
    int conn_sock;
    int conn_size;
    int success;
    int c;

    char t[INET_ADDRSTRLEN];
    char *indirizzo;
    char str[100];

    memset(&hint,0,sizeof hint);

    host.sa_family = AF_INET;
    if(inet_pton(host.sa_family, argv[1], (void *) host.sa_data) == -1) {
        printf("\n[FATAL]: pton: %s (%d)\n", gai_strerror(errno), errno);
        exit(-1);
    }

    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;
    hint.ai_flags = AI_PASSIVE;
    hint.ai_addr = &host;

    if((stat = getaddrinfo(argv[1], argv[2], &hint, &servinfo)) != 0)  {
        printf("[FATAL]: getaddrinfo: %s\n",gai_strerror(stat));
        exit(-1);
    }

    success = 0;
    for(count = servinfo; count != NULL; count = count->ai_next) {
        if((connessione = socket(count->ai_family, count->ai_socktype, count->ai_protocol)) == -1)
            continue;

        if(connect(connessione, count->ai_addr, count->ai_addrlen) != -1) {
            success = 1;
            break; 
        }
    }

    if(success == 0) {
        printf("\n[FATAL]: impossibile trovare l'host specificato\n");
        exit(-1);
    }

    servinfo = count;
    freeaddrinfo(count);

    indirizzo = (char *) malloc(INET_ADDRSTRLEN * sizeof(char));
    indirizzo = inet_ntop(servinfo->ai_family, &conn_addr, t, INET_ADDRSTRLEN);

    printf("\n[DEBUG]: %s: connesso a: %s\n",argv[0], indirizzo);

    while(1) {
        strcpy(str,"buffer for data to send");

        while(((c = getc(stdin)) != '\n') && sizeof(str) <= 100) {
            strcat(str, (char *) c);
        }

        send(connessione, (void *) str, sizeof(str), (int) NULL);
    }
}

在最后.

它工作得很完美!谢谢大家。:)

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-12-06 18:42:42

通常,您不使用FILE *句柄进行网络编程。相反,您需要一个由socket(2)系统调用返回的套接字描述符。一旦您拥有它,您可以connect(2)它(客户机)或bind(2)它(服务器)。可以使用recv(2)send(2)执行正在进行的通信。

如果您真的想使用FILE *句柄进行网络编程,请查看fdopen(3)库函数。它使用一个可以由socket(2)返回的描述符,并返回一个与fread / fwrite / fprintf兼容的FILE *句柄。

在发布完整的代码之后,我看到的最重要的错误是:

if (accept(connessione, (struct sockaddr *) &conn_addr, &conn_size))

accept(2)返回您必须与recv(2) / send(2)一起使用的套接字,然后使用服务器套接字来丢弃它。

票数 5
EN

Stack Overflow用户

发布于 2010-12-06 18:47:37

我建议你读http://beej.us/guide/

这是一个非常好的套接字编程指南。

票数 1
EN

Stack Overflow用户

发布于 2010-12-06 19:05:09

当您使用强制转换(如(FILE *) connection)时,您正在告诉编译器:“相信我,这是事实”。当你对编译器撒谎时,它相信你。

你对编译器撒谎了。connection不是指向文件结构的指针。分割错误是你能得到的最好的结果,因为它告诉你一些事情是严重错误的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4369731

复制
相关文章

相似问题

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