Error in creating socket in a client (UDP)

This is a discussion on Error in creating socket in a client (UDP) within the Networking/Device Communication forums, part of the General Programming Boards category; Ok, thanks in advice for your help! I'm creating two new programs: a client and a server: they have to ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    26

    Unhappy Error in creating socket in a client (UDP)

    Ok, thanks in advice for your help! I'm creating two new programs: a client and a server: they have to exchange shot msgs, with UDP.

    But my client doesn't succeed in creating the socket:

    Code:
    #if defined WIN32
    #include <winsock.h>
    #else
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <netdb.h>
    #endif
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 100
    
    void printErr(char *message) {
    	fprintf(stdout, message); }
    
    void ClearWinSock() {
    #if defined WIN32
    	WSACleanup();
    #endif
    }
    
    int main (void) {
    
    	int sock;
    	struct sockaddr_in echoServAddr;
    	struct sockaddr_in fromAddr;
    	unsigned short echoServPort;
    	unsigned int fromSize;
    	char *servIP;
    	char echoString[MAX];
    	char echoBuffer[MAX];
    	int echoStringLen, respStringLen;
    
    
    		servIP = "127.0.0.1";
    
    		do {
    
    			fprintf(stdout, "\nInput:\n");
    			fscanf(stdin, "%s" ,echoString);
    			fprintf(stdout, "%s", echoString);
    
    			} while (strlen(echoString) > MAX);
    
    		echoStringLen = strlen(echoString);
    
    		echoServPort = 23477;
    
                   /* this fails*/
    		if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    			{ printErr("\nSocket() creation failed."); }
    
    		memset(&echoServAddr, 0, sizeof(echoServAddr));
    		memset(&echoBuffer, 0, sizeof(echoBuffer));
    		echoServAddr.sin_family = AF_INET;
    		echoServAddr.sin_port = htons(echoServPort);
    		echoServAddr.sin_addr.s_addr = inet_addr(servIP);
    
    		if (sendto(sock, echoString, echoStringLen, 0,
    				(struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) != echoStringLen)
    			{ printErr("\nsendto() error."); }
    
    		fromSize = sizeof(fromAddr);
    
    		if ((respStringLen = recvfrom(sock, echoBuffer, MAX, 0,
    				(struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen)
    			{ printErr("\nrecvfrom() error."); }
    
    
    		fprintf(stdout, "\nString received = %s", echoBuffer);
    
    
    		closesocket(sock);
    		#if defined WIN32
    			WSACleanup();
    			fprintf(stdout, "\n");
    			system("pause");
    		#endif
    		return EXIT_SUCCESS;
    
    }
    And I don't know why. Can you help?

    PS: I use Eclipse on Windows...
    Last edited by ferenczi; 11-26-2008 at 02:13 PM. Reason: Mistake in typing

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    I assume this is failing on Windows, as it won't even build on Linux (no closesocket function). You didn't call WSAStartup:
    Code:
    WSADATA wsaDATA;
    WSAStartup(MAKEWORD(2,2),&wsaDATA);

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    just call socket(PF_INET,SOCK_DGRAM,0), without any third parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Socket Programming Problem!!!!
    By bobthebullet990 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-21-2008, 06:36 PM
  3. Using select() for client UDP
    By jazzman83 in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 05:31 AM
  4. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 10:59 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21