UDP Inter-Application Communication with MinGW Compiler

Hello. I seem to be having a problem with the connect function in the winsock area of windows. Im trying to get two applciations to speak to each other locally over UDP. However, it seems that the connect function on the sending side doesnt seem to return the correct values. I.E When the recieving end program isnt open and therefore, the port isnt open, conenct just binds any old port and claims it has succeeded. Now i know that the UDP protocol is connectionless but... it seems stupid since i've actually ripped off the MSDN code straight from the MSDN site and it still fails. Should i use something other than connect?

Heres the recieving port code:

int xne_udp_listen(Endpoint endpoint, const char * command) {

struct xne_udp_data * data = (struct xne_udp_data *) endpoint->data;
char * current_token = NULL;
char * local_address = NULL;
struct sockaddr_in* local_sockaddr = (struct sockaddr_in *) malloc( sizeof(struct sockaddr_in) );
int local_port = 0;
int rc = 0;
char * command_token = 0;

fprintf(stderr,"Entering the UDP Listen Func");

/* must be a ready endpoint */
if (endpoint->status != XNE_READY)
return -2;

/* parse the connect command */
command_token = strdup(command);
current_token = strtok(command_token, ":");
if (current_token[0] != '*')
local_address = strdup(current_token);

current_token = strtok(0, ":");
if (strlen(current_token) == 0)
return -2; /* Empty command part */
local_port = atoi(current_token);
free(command_token);

local_sockaddr->sin_family = AF_INET;
local_sockaddr->sin_port = htons(local_port);
if (local_address == NULL) {
local_sockaddr->sin_addr.s_addr = htonl(INADDR_ANY);
} else {
inet_aton(local_address, &local_sockaddr->sin_addr);
free(local_address);
}
rc = bind(data->fd, local_sockaddr, sizeof(struct sockaddr_in));

if (rc < 0) {
#ifdef _WIN32
printError();
#else
fprintf(stderr, "-- errno %d: %s\n", errno, strerror(errno));
#endif
fprintf(stderr, "Cannot bind to local port.\n");
return -1;
}

return 0;
}

Here is the send code:

int xne_udp_connect(Endpoint endpoint, const char * command) {

struct xne_udp_data * data = (struct xne_udp_data *) endpoint->data;
char * current_token = NULL;
char * remote_address = NULL;
struct sockaddr_in remote_sockaddr;
u_short remote_port = 0;
int rc = 0;
char * command_token = NULL;

fprintf(stderr, "UDP Connect: \n");

/* must be a ready endpoint */
if (endpoint->status != XNE_READY)
return -2;

/* parse the connect command */
command_token = strdup(command);
current_token = strtok(command_token, ":");
if (strlen(current_token) == 0)
return -2; /* Empty command part */
remote_address = strdup(current_token);

current_token = strtok(0, ":");
if (strlen(current_token) == 0)
return -2; /* Empty command part */
remote_port = atoi(current_token);
free(command_token);

/* bind to remote port */
remote_sockaddr.sin_family = AF_INET;
remote_sockaddr.sin_port = htons(remote_port);
memset(&(remote_sockaddr.sin_zero),0,sizeof(remote_sockaddr.sin_zero));

fprintf(stderr, "UDP Port: %i\n",remote_sockaddr.sin_port);

fprintf(stderr, "UDP Address: %s\n",remote_address);

remote_sockaddr.sin_addr.s_addr = inet_addr( remote_address );

fprintf(stderr, "inet_addr() = %ld (%ld)\n", inet_addr( remote_address ), INADDR_NONE);

// inet_aton(remote_address, &remote_sockaddr.sin_addr);
free(remote_address);

rc = connect(data->fd, (SOCKADDR *) &remote_sockaddr, sizeof(remote_sockaddr));
// rc = connect(data->fd, 0, 0);

fprintf(stderr, "UDP RC: %i\n",rc);

if (rc < 0) {
fprintf(stderr, "-- errno %d: %s\n", errno, strerror(errno));
fprintf(stderr, "Cannot connect to remote port: %s.\n", command);
return -1;
}

return 0;
}

So, connect simply doesnt work. If i uncomment the following line:

rc = connect(data->fd, 0, 0);

...we get the correct result; that the port cannot be connected to. Of course, thats obvious because i havent specified one. Can anyone help please?
[4554 byte] By [OniDaito] at [2007-11-19 19:30:24]
# 1 Re: UDP Inter-Application Communication with MinGW Compiler
I would like to say that each app should be listening on a different port. By Default they will both go for the same address and port combination and the second app will fail. Additionally you will need a way to point the UDP client of first app at the second app such as a dialog and default to registry setting of the port. In other words first app client side use this port, second app listener use that port.

This will be the same for any platform using C Sockets.

HTH,
ahoodin at 2007-11-9 0:57:24 >
# 2 Re: UDP Inter-Application Communication with MinGW Compiler
For connection-less protocols the connect() function only defines the destination address for the send() and recv() command. There is no test if the address is currently reachable. It is merely a convenience, because otherwise you would have to provide the address everytime you send or receive using sendto() and recvfrom().
philkr at 2007-11-9 0:58:18 >
# 3 Re: UDP Inter-Application Communication with MinGW Compiler
Ok, i should mention communcation is only one way. So, only one app is listening on port 3331 while the other application is sending data to port 3331.

Im aware of the connect function. Simply ignoring it and using the sendto() function results in: WSAENOTCONN 10057 Socket is not connected.

So, it seems connect does need to be called. I should point out that if the listening program isnt running and connect is called from the sending program... connect still claims to succeed. Windows simple assigns the nearest available port and sets it up as a global (0.0.0.0) udp listening port.

Connect, should in theory fail if that port is not open. I guess i need to know why it isnt doing
OniDaito at 2007-11-9 0:59:20 >
# 4 Re: UDP Inter-Application Communication with MinGW Compiler
The error WSAENOTCONN is only returned by the sendto() function, if you are using connection-oriented sockets.
How did you create the sockets? Should be something like this:
hSocket = socket(AF_INET, SOCK_DGRAM, 0);
// or maybe this:
hSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
philkr at 2007-11-9 1:00:20 >
# 5 Re: UDP Inter-Application Communication with MinGW Compiler
No, after you create a UDP socket, you need to fill in the sock_addr_in structures for the server, and the client, it is only necessary to call sendto() with that info. UDP infact is an unreliable protocol that does not establish a connection. Thus no call to connect is made. You will have to make it reliable...

Ok, i should mention communcation is only one way. So, only one app is listening on port 3331 while the other application is sending data to port 3331.

Im aware of the connect function. Simply ignoring it and using the sendto() function results in: WSAENOTCONN 10057 Socket is not connected.

So, it seems connect does need to be called. I should point out that if the listening program isnt running and connect is called from the sending program... connect still claims to succeed. Windows simple assigns the nearest available port and sets it up as a global (0.0.0.0) udp listening port.

Connect, should in theory fail if that port is not open. I guess i need to know why it isnt doing

HTH,
ahoodin at 2007-11-9 1:01:30 >
# 6 Re: UDP Inter-Application Communication with MinGW Compiler
Aye, I should mention that this code is a coversion project between windows and linux. The linux code uses connect which i think rather than connecting, just sends out on a particular port number. I removed the connect line and bound another port that the client code can "send" on. It seems to work ok now.

Cheers for all the help :)
OniDaito at 2007-11-9 1:02:31 >