UDP Inter-Application Communication with MinGW Compiler
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?

