Thread: UDP Server -> Displaying Client Information

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    UDP Server -> Displaying Client Information

    Hi, I have a UDP server which a client can send messages to.

    I want the UDP server to be able to output the address and the port number of the client that sent the data.

    Any idea how this can be achieved?

    Thank you,
    -Mark

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    You can use something like this:

    Code:
    #include <winsock.h>
    
    void yourfunction(void)
    {
      HOSTENT *hostent;
      char *remotehost;
    
      /*...some code here..*/
    
      hostent = gethostbyname(remotehost);
    
      /*...and more code here...*/
    }
    It will retrieve the address. Not sure what to do for the port. I'm not familiar with UDP though, just TCP.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Would you mind e-mailing me winsock.h, please? I'm trying to do something similar using DJGPP and I don't have that header file. I can't find it for download anywhere either. My address is dragoon_42 "AT" hotmail.com

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm... First, you should probably be looking for <winsock2.h> unless you're planning on marketing a product for Windows95 (and I'm not talking about 98). And also, you probably shouldn't try to "transplant" header files from one compiler to another, since the winsock2.h someone sends you might have a compiler-specific implementation and/or require other headers and misc. files to be used, such as WS2_32.lib, and anything else you might need to make it work.

    Try... installing the latest version of your compiler, buying/downloading a new one, or searching around for a Winsock2 SDK or something to that effect.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Windows Sockets.

    DJGPP is a dos compiler. There are plenty of free windows compilers around.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Does anyone know of a solution to my problem?

    The part of the code where my client sends data is:

    Code:
    	buffer = gets(buffer);
    	printf("Read in %s\n", buffer);
    	sendto(clientSocket, buffer, strlen(buffer) + 1, 0, (struct sockaddr *) &connectTo, sizeof(struct sockaddr_in));
    And for the Server receiving the data:

    Code:
    	while(buffer != "QUIT") {
    		result = recvfrom(serverSocket, buffer, sizeof(buffer), 0, 0, 0);
    		if (result > 0) {
    			printf(">> %s\n", buffer);
    		}
    	}

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    I figured it out eventually. If anyone is interested:

    Code:
    	while ((strcmp (buffer, "QUIT")) != 0) {
    		result = recvfrom(serverSocket, buffer, sizeof(buffer), 0, (struct sockaddr*) &clientInformation, &clientSize);
    		if (result > 0) {
    			buffer[result] = '\0';
    			printf("%s:%d >> %s\n", inet_ntoa(clientInformation.sin_addr), ntohs(clientInformation.sin_port), buffer);
    		}
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Client - Server problems (Async Sockets )
    By Cpp_Noob in forum Networking/Device Communication
    Replies: 4
    Last Post: 06-19-2009, 07:12 AM
  2. server client problem
    By rudeboy in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-17-2008, 12:41 AM
  3. Multi-Threaded Server Help
    By jonswits20 in forum C# Programming
    Replies: 5
    Last Post: 04-17-2007, 11:05 PM
  4. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  5. [SOCKETS] Solaris server in C, NT client in VB
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 10:02 AM