I'm trying to understand how those * functions work...
i have a server wich sends a uint16_t in network format and a client wich reads this uint16_t (this is for the example! my real program should work viceversa, the server reads a uint16_t in nwf from client and than sends the value of array[recivednum])
this is the code of the server:
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "errlib.h"
#include "sockwrap.h"
#define MAXBUFL 255
#define LISTENQ 15
#define PORT 1234
char *prog;
int main (int argc, char *argv[])
{
int listenfd, connfd;
struct sockaddr_in servaddr, cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
char buf[MAXBUFL];
uint16_t TRY=0x7ff6;
prog = argv[0];
listenfd = Socket (AF_INET, SOCK_STREAM, 0);
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
Bind (listenfd, (SA*) &servaddr, sizeof(servaddr));
Listen (listenfd, LISTENQ);
#ifdef TRACE
printf ("(%s) socket created, waiting for connections ...\n", prog);
#endif
for ( ; ; )
{
connfd = Accept (listenfd, (SA*) &cliaddr, &cliaddrlen);
#ifdef TRACE
printf ("(%s) - new connection from client %s:%u\n", prog, inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
#endif
TRY=htons(TRY);
Writen (connfd, &TRY,sizeof(TRY));
printf("send: %u dim: %d\n",TRY, sizeof(TRY));
Close (connfd);
}
}
and this is the client:
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <netinet/in.h>
#include "errlib.h"
#include "sockwrap.h"
#define MAXBUFL 255
#define PORT 1234
char *prog;
int main (int argc, char *argv[])
{
int sockfd, n,i;
char buf[MAXBUFL+1];
struct sockaddr_in servaddr;
uint16_t try;
/* for errlib to know the program name */
prog = argv[0];
/* check the arguments */
if (argc != 2)
err_quit ("usage: %s IPaddress_of_server", prog);
/* create socket */
sockfd = Socket(AF_INET, SOCK_STREAM, 0);
/* specify address of server to connect to */
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
/* try to connect */
Connect (sockfd, (SA*) &servaddr, sizeof(servaddr));
/* read the answer */
memset(buf,'\0',sizeof(buf));
while ( (n = Read(sockfd,buf,MAXBUFL)) > 0)
{
Write (1, buf, n);
}
memcpy(&try,buf,strlen(buf));
try=ntohs(try);
printf("\n(int)try: %d\ntry: %x \nbuf: %s\nlenght: %d\nsize of buf: %d",(int)try,try,buf,strlen(buf),sizeof(buf));
for(i=0;i<strlen(buf);i++)
if(isdigit(buf[i])) printf("\n%d is a number",(int)buf[i]);
else printf("\n%c is not a number",buf[i]);
return 0;
}
the trouble is that client recives sometimes 7ff6 (the number i'd like to read evrytime) and sometimes f67f (wrong numbuer!!!)
how do htons() and ntohs() work? what shoul i change in the client or in the server to get always 7ff6?
another problem is how to "understand" if the recived buffer has got an uint16_t inside (obviously isdigit doesn't work, but i tryed it the same)
thank you very much and sorry for my bad english