Connection failed socket in C -
i'm trying create program connect itself. trigger thread client , thread server , create socket each thread. use same port.
when thread client tries connect thread server, connection fails. why? can't understanding. me!
the full code it's complicated, because it's big. i'll generalize.
#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; }
looks me passing wrong size of address structure connect()
in client thread. this:
if (connect(sockid, addrr, sizeof(struct sockaddr)) == -1)
should be:
if (connect(sockid, addrr, sizeof(struct sockaddr_in)) == -1)
to indicate passing internet address connect()
. sizeof(struct sockaddr)
wrong, since that's generic structure type.
it's hard pinpoint other problems without more code.
Comments
Post a Comment