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
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
You can use something like this:
It will retrieve the address. Not sure what to do for the port. I'm not familiar with UDP though, just TCP.Code:#include <winsock.h> void yourfunction(void) { HOSTENT *hostent; char *remotehost; /*...some code here..*/ hostent = gethostbyname(remotehost); /*...and more code here...*/ }
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
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.
Windows Sockets.
DJGPP is a dos compiler. There are plenty of free windows compilers around.
Does anyone know of a solution to my problem?
The part of the code where my client sends data is:
And for the Server receiving the data:Code:buffer = gets(buffer); printf("Read in %s\n", buffer); sendto(clientSocket, buffer, strlen(buffer) + 1, 0, (struct sockaddr *) &connectTo, sizeof(struct sockaddr_in));
Code:while(buffer != "QUIT") { result = recvfrom(serverSocket, buffer, sizeof(buffer), 0, 0, 0); if (result > 0) { printf(">> %s\n", buffer); } }
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); } }