C中的连接失败

时间:2015-06-02 22:42:58

标签: c sockets pthreads

我正在尝试创建一个与自身连接的程序。 我触发一个线程客户端和一个线程服务器,我为每个线程创建一个套接字。我使用相同的端口。

当线程客户端尝试连接到线程服务器时,连接失败。为什么?我无法理解。救救我!

完整的代码很复杂,因为它很大。但我会概括。

#define ADDR "127.0.0.1"

struct threadData{
    int portnum;
    int sockid;
    int lastID;
    unsigned int lastCheck;
};

// ----------- ----- Connection ------ ------------- //

void dieError(char *message, int socket){
    printf("%s\n", message);
    if(socket != 0)
        close(socket);
    exit(1);
}

int createSocket(int portnum, struct sockaddr_in* netw){
    int sockid;

    sockid = socket(AF_INET, SOCK_STREAM, 0);
    if(sockid == -1)
        dieError("socket() failed", sockid);

    struct in_addr addrr;
    if(inet_pton(AF_INET, ADDR, &addrr) < 1)
        dieError("pton() failed", sockid);

    netw->sin_family = AF_INET;
    netw->sin_port = htons(portnum);
    netw->sin_addr = addrr;

    return sockid;
}

void listenSocket(struct threadData* thd, struct sockaddr_in* netw){

    if(bind(thd->sockid, (struct sockaddr *) netw, sizeof(struct sockaddr)) == -1)
        dieError("bind() failed", thd->sockid);

    if(listen(thd->sockid, 0) == -1)
        dieError("listen() failed", thd->sockid);
}

int acceptConnection(int sockid, struct sockaddr* addrr){
    socklen_t cLen = sizeof(struct sockaddr);
    int cSocket = accept(sockid, addrr, &cLen);

    if(cSocket == -1)
        dieError("accept() failed", sockid);

    return cSocket;
}

int createConnection(int portnum, struct sockaddr_in* netw){
    int sockid = createSocket(portnum, netw);

    fcntl(sockid, F_SETFL, O_NONBLOCK);

    if(connect(sockid, (struct sockaddr *) netw, sizeof(struct sockaddr)) == -1)
        dieError("connect() failed", sockid);

    return sockid;
}

// ----------- ----- ---------- ------ ------------- //

//Receptor thread
void * verifyPort(void * param){
    struct threadData* thd = param;
    int cSocket;
    struct sockaddr addrr;
    struct sockaddr_in* netw = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));

    thd->sockid = createSocket(thd->portnum, netw);
    listenSocket(thd, netw);
    cSocket = acceptConnection(thd->sockid, &addrr);
}

//Sender thread
void verifyKeyboard(int portnum){
    int sockid;
    struct sockaddr_in* netw = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in));
    sockid = createConnection(portnum, netw);
}

// ----------- ---- -------- ------- ------------- //

int main(int argc, char **argv){
    pthread_t tid;
    struct threadData* thd;
    thd = (struct threadData*) malloc(sizeof(struct threadData));

    thd->portnum = atoi(argv[1]);
    thd->lastID = -1;
    thd->lastCheck = 0;

    pthread_create(&tid, NULL, verifyPort, thd);

    verifyKeyboard(atoi(argv[1]));

    free(thd);

    return 0;
}

1 个答案:

答案 0 :(得分:1)

在我看来,您正在将错误的地址结构大小传递给客户端线程中的window.location.hash = myHash; 。这样:

connect()

应该是:

if (connect(sockid, addrr, sizeof(struct sockaddr)) == -1)

表示您正在将互联网地址传递给if (connect(sockid, addrr, sizeof(struct sockaddr_in)) == -1) connect()显然是错误的,因为那只是通用的结构类型。

如果没有更多代码,很难确定其他问题。