Thread: Error in creating socket in a client (UDP)

  1. #1
    Registered User
    Join Date
    Feb 2008

    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:

    #if defined WIN32
    #include <winsock.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    #define MAX 100
    void printErr(char *message) {
    	fprintf(stdout, message); }
    void ClearWinSock() {
    #if defined WIN32
    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 = "";
    		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);
    		#if defined WIN32
    			fprintf(stdout, "\n");
    		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 03:13 PM. Reason: Mistake in typing

  2. #2
    Join Date
    Dec 2007
    I assume this is failing on Windows, as it won't even build on Linux (no closesocket function). You didn't call WSAStartup:

  3. #3
    Registered User
    Join Date
    Apr 2008
    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, 07: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, 11: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