htons and ntohs not working? C UNIX networking -
i'm new network programming in c , started off trying send array read keyboard server. problem whenever read length of array keyboard, add 2/3 , not right anymore. example:
length or array give: 2 client read keyboard 4 or 5 elements , if itry print them afterwards, don't have value gave them. totally different numbers.
this how client looks like:
#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> int main(int argc, char* argv[]) { int sockfd; struct sockaddr_in server; /*char msg[1000], serv_reply[2000];*/ if ( (sockfd = socket(af_inet, sock_stream, 0)) < 0) { perror("error in creating stupid sock client "); exit(errno); } puts("socket created in client "); memset(&server, 0, sizeof(server)); server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = af_inet; server.sin_port = htons(9255); if ( connect(sockfd, (struct sockaddr*)&server, sizeof(server)) < 0) { perror("connection failed "); exit(errno); } puts("yey! connected"); printf("let's try \n" ); printf("nr of elem = "); **int a; scanf("%d", &a); = htons(a); if ( send(sockfd, &a, sizeof(a), 0 ) < 0 ) { perror("client sent error"); exit(errno); } int i, elem; for( =0; i<a; i++) { printf("enter number: "); scanf("%d", &elem); elem = htons(elem); printf("%d", elem); if ( send(sockfd, &elem, sizeof(elem), 0) < 0) { perror("couldn't send damn element "); exit(errno); } }** int somth; recv(sockfd, &somth, sizeof(somth), 0); somth = ntohs(somth); printf("we got %d ", somth); close(sockfd); }
here server:
#include <unistd.h> #include <errno.h> #include <sys/socket.h> #include <arpa/inet.h> #include <resolv.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define my_port 9999 #define maxbuf 1024 int main (int argc, char* argv[]) { int sockfd; struct sockaddr_in server, client; int c, l; if ( (sockfd = socket(af_inet, sock_stream, 0)) < 0) { perror("error creating stupid socket "); exit(errno); } memset(&server, 0, sizeof(server)); server.sin_port = htons(9255); server.sin_family = af_inet; server.sin_addr.s_addr = inaddr_any; if ( bind(sockfd, (struct sockaddr *) &server, sizeof(server)) < 0) { perror("error in socket binding "); exit(errno); } if ( listen (sockfd, 20) < 0) { perror("erro listening dumb sock "); exit(errno); } printf("we're on!! " ); l = sizeof(client); memset(&client, 0, l); while(1) { int a; c = accept(sockfd, (struct sockaddr *) &client, &l); if (c == -1) { perror("error accepting connection"); continue; } else { printf("cool, have new client! \n"); } if (recv( c, &a, sizeof(a), msg_waitall) < 0) { perror("error reading in server "); exit(errno); } = ntohs(a); int i, elem,sum; ( = 0; i<a; i++) { if ( recv(c, &elem, sizeof(elem), msg_waitall)< 0) { perror("error reading elems "); exit(errno); } elem = ntohs(elem); sum = sum + elem; } sum= ntohs(sum); if ( send(c, &sum, sizeof(sum), 0) < 0) { perror("error sending client "); exit(errno); } close(c); } }
i tried switching htons ntohs , inverse , same problem. idea what's wrong?
one major problem loose original a
enter in client, when later use in loop condition might not expect.
another major problem s
in e.g. htons
stands short
, , on modern pc systems short
16 bits while int
32 bits.
try using htonl
, ntohl
instead!
Comments
Post a Comment