ANSI C socket libraries
| Thu, 2005-05-05 16:59 | |
|
I want to implement a Socket Server on a Symbian mobile phone using standard library. I have my ethernet connection configured as DHCP.
----------- CODE N. 1 ----------- When I use the functions socket() + bind() + listen() + accept() + recv() NO IP ADDRESS IS ASSIGNED TO THE RUNNING EMULATOR, to obtain an IP address I workarounded the problem in the following way: - I opened a client connection (on a separate socket) before opening the server socket. - I sniffed my network to see wich IP address is assigned to the emulator This seems very strange to me, anyone have some suggestions ? ----------- CODE N. 2 ----------- I tried to use the function recvfrom() or the function read() to read data from a RAW socket. My code (fully listed below) contains: -------------------------------------------------------------------------------- SockId = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); bytes_received = recvfrom(SockId, buffer, sizeof(buffer), 0,(struct sockaddr *)&from, &fromlen); or alternatively bytes_received = read (SockId, &buffer, sizeof (buffer)); --------------------------------------------------------------------------------- In both cases, at runtime, I get an "Invalid argument" error message on recvfrom() or on read() function. The same program on linux machines works perfectly Can someone help me ? Thanks in advance Mario Following you will find my code ============================================= CODE LISTENING N. 1 ============================================= // Simple test program: A socket server on port 1600 #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <arpa/inet.h> #include <netinet/in.h> #include <sys/socket.h> /* ------------------ Costants ------------------ */ // Listening port unsigned short int Myport = 1600; // Data used by a client connection just to obtain an IP through DHCP unsigned short int Dummyport = 16; char DummyipAddress[20] = "192.168.10.110"; /* ------------------ Variables ------------------ */ int SockId; int SockId_Son; int Ind_sock_length; struct sockaddr_in socketAddress; unsigned char buffer[50]; int lenbuffer; // ---------------------------------------------- // Opening a client connection (even to a non // existing ip address) force the Symbian emulator // to request an IP through DHCP so that an // IP address will be assigned to the emulator // ---------------------------------------------- void OpenClientConnection() { int SockClient; struct sockaddr_in socketClient; int lenbuffcli; // Options socketClient.sin_family = AF_INET; socketClient.sin_port = htons(Dummyport); socketClient.sin_addr.s_addr = inet_addr(DummyipAddress); lenbuffcli=3; printf ("\nConn CLIENT DHCP\n"); /* Socket Opening */ if((SockClient = socket(AF_INET,SOCK_STREAM,0))<0) { printf("\nERRORE Creazione del socket |%s|",strerror(errno)); fflush (stdout); } /* Connect -- with this instruction the DHCP assign me an IP Address */ if (connect(SockClient,(struct sockaddr *)&socketClient,sizeof(socketClient)) >= 0) { close (SockClient); } } // M A I N int main () { int result; int count; // Workaround to obtain a DHCP from my network -------- OpenClientConnection(); // ---------------------------------------------------- SockId = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP); if(SockId == -1) { printf("\nERROR function socket |%s|",strerror(errno)); return (1); } else printf("\nfunction socket OK"); socketAddress.sin_family = AF_INET; socketAddress.sin_port = htons(Myport); socketAddress.sin_addr.s_addr = htonl(INADDR_ANY); /* Bind */ result = bind(SockId,(struct sockaddr *)&socketAddress,sizeof(socketAddress)); if(result == -1) { printf("\nERROR function bind |%s|",strerror(errno)); return (1); } else { printf("\nfunction bind OK"); fflush (stdout); } /* Listen */ result = listen(SockId,1); if(result == -1) { printf("\nERROR function listen |%s|",strerror(errno)); return (1); } else { printf("\nfunction listen OK"); fflush (stdout); } Ind_sock_length = sizeof(socketAddress); printf ("\n\nstart Receiving ...\n"); fflush (stdout); for (count=1; ;count++) { SockId_Son = accept(SockId, (struct sockaddr *)&socketAddress,(size_t *)&Ind_sock_length); printf("\nfunction accept OK"); fflush (stdout); result = recv(SockId_Son, (char *)&buffer[0], 100,0); printf("\nfunction recv OK result = %d",result); fflush (stdout); if(result > 0) { printf ("\n%4.4d Received %d bytes",count,result); fflush (stdout); } else { exit (1); printf("\n%4.4d ERROR function recv |%s|",count,strerror(errno)); fflush(stdout); } close (SockId_Son); } } ============================================ CODE LISTENING N. 2 ============================================ #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <arpa\inet.h> #include <netinet\in.h> #include <sys\socket.h> // Variables int SockId; unsigned int fromlen; int bytes_received; char buffer[65535]; struct sockaddr_in from; struct ip *ip; struct tcp *tcp; int k; int main () { printf ("\nSTART\n"); fflush (stdout); SockId = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); printf ("\nfunction socket OK %d",SockId); fflush (stdout); fromlen = sizeof (from); for(k=0;k<10;k++) { // **************************************************** // HERE I GET AN ERROR BOTH IF I USE // RECVFROM() OR IF I USE READ() // ***************************************************** //bytes_received = recvfrom(SockId, buffer, sizeof(buffer), 0,(struct sockaddr *)&from, &fromlen); bytes_received = read (SockId, &buffer, sizeof (buffer)); if (bytes_received >= 0) { printf ("\nbytes_received %d",bytes_received); } else { printf ("\nERROR recvfrom |%s|",strerror(errno)); exit (1); } } return 0; } |
|





