TCP/IP in C not working -
i having trouble getting tcp/ip connection between client , server working.
here server code -
int main() { int servsock; /* socket descriptor server */ int clntsock; /* socket descriptor client */ unsigned short echoservport; /* server port */ struct sockaddr_in echoservaddr; /* local address */ struct sockaddr_in echoclntaddr; /* local address */ pid_t processid; /* process id fork()*/ unsigned int childproccount = 0; /* number of child processes */ unsigned int clntlen; unsigned int recvmsgsize; echoservport = 22; if ((servsock = socket(pf_inet, sock_stream, ipproto_tcp)) < 0){ printf("socket failed"); } echoservaddr.sin_family = af_inet; //internet address family echoservaddr.sin_addr.s_addr = htonl(inaddr_any); //any incoming interface echoservaddr.sin_port = htons(echoservport); // local port if (bind(servsock, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr)) < 0){ printf("bind failed"); } if (listen(servsock, maxpending) < 0){ printf("listen() failed"); } clntlen = sizeof(echoclntaddr); if ((clntsock=accept(servsock,(struct sockaddr *)&echoclntaddr,&clntlen))<0){ printf("accept() failed"); } if ((recvmsgsize = recv(clntsock, buf, 1024, 0)) < 0){ printf("recv() failed"); } /* send received string , receive again until end of transmission */ while (recvmsgsize > 0) { /* 0 indicates end of transmission */ printf("%d", recvmsgsize); if (send(clntsock, buf, recvmsgsize, 0) != recvmsgsize){ //printf(“send() failed”); } if ((recvmsgsize = recv(clntsock, buf, 1024, 0)) < 0){ //printf(“recv() failed”); } } sleep(60); } } return exit_success; }
and client code, cgi application.
int main(void) { int servsock; /* socket descriptor server */ int clntsock; /* socket descriptor client */ unsigned short echoservport; /* server port */ struct sockaddr_in echoservaddr; /* local address */ struct sockaddr_in echoclntaddr; /* local address */ struct sockaddr_in { __uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; /*pid_t processid; process id fork() unsigned int childproccount = 0; number of child processes unsigned int clntlen;*/ //char echoservip = "10.0.0.2"; printf("content-type: text/html\n\n"); puts("<html>"); puts("<body>"); echoservport = 22; servsock = 22; clntsock = 22; puts("<br>"); if ((clntsock = socket(pf_inet, sock_stream, ipproto_tcp)) < 0){ printf("socket() failed"); } echoservaddr.sin_family = af_inet; //internet address family echoservaddr.sin_addr.s_addr = inet_addr("10.0.0.2"); //server ip address echoservaddr.sin_port = htons(echoservport); //server port echoclntaddr.sin_addr.s_addr = inet_addr("10.0.0.1"); if (connect(clntsock, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr)) < 0){ printf("connect() failed\n"); } int clntlen; clntlen = sizeof(echoclntaddr); if ((clntsock=accept(servsock,(struct sockaddr *)&echoclntaddr,&clntlen))<0){ printf("accept() failed\n"); } char echostring[4] = "helo"; int echostringlen; echostringlen = strlen(echostring); //determine input length //send string server if (send(clntsock, echostring, echostringlen, 0) != echostringlen){ printf("send() sent different number of bytes expected"); } puts("<p>hello <b>cgi</b</p>"); puts("</body>"); puts("</html>"); return exit_success; }
when debugging, problem occurs on client side @ line
if ((clntsock=accept(servsock,(struct sockaddr *)&echoclntaddr,&clntlen))<0){ printf("accept() failed\n"); }
and at
if(send(clntsock, echostring, echostringlen, 0) != echostringlen){ printf("send() sent different number of bytes expected"); }
i output
<html> <body> <br> accept() failed send() sent different number of bytes expected<p>hello <b>cgi</b</p>
need fixing this!, thanks.
you don't need call accept()
on client - need connect.
and
char echostring[4] = "helo";
is wrong. it's not nul-terminated. do
char echostring[] = "helo";
Comments
Post a Comment