This program is supposed to receive stuff from a local socket (tcp) and broadcast it, and vice verca.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#define BUFFER_SIZE 1024
#define PORT_TCP 12312
#define PORT_UDP 5678
int main() {
int tcp_sock, udp_sock, client_tcp_sock, bytes_received_udp, bytes_received_tcp;
struct sockaddr_in server_tcp, client_tcp, server_udp, client_udp;
socklen_t client_len;
char buffer[BUFFER_SIZE];
memset(buffer, 0, BUFFER_SIZE); // emptying buffer
// Create TCP socket to talk with python game
tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
if (tcp_sock < 0) {
perror("Error creating TCP socket");
return EXIT_FAILURE;
}
// Create UDP socket for broadcasting
udp_sock = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
if (udp_sock < 0) {
perror("Error creating UDP socket");
return EXIT_FAILURE;
}
//enable le broadcast ?
socklen_t broadcastEnable = 1;
if(setsockopt(udp_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable)) < 0)
{
perror("Reusing ADDR failed()");
exit(2);
}
// Bind TCP socket to server address
memset((char *) &server_tcp, 0, sizeof(server_tcp));
server_tcp.sin_family = AF_INET;
server_tcp.sin_port = htons(PORT_TCP);
server_tcp.sin_addr.s_addr = INADDR_ANY;
if (bind(tcp_sock, (struct sockaddr *) &server_tcp, sizeof(server_tcp)) < 0) {
perror("Error binding TCP socket to server address");
return EXIT_FAILURE;
}
// Bind UDP socket to server address
memset((char *) &server_udp, 0, sizeof(server_udp));
server_udp.sin_family = AF_INET;
server_udp.sin_port = htons(PORT_UDP);
server_udp.sin_addr.s_addr = INADDR_ANY;
if (bind(udp_sock, (struct sockaddr *) &server_udp, sizeof(server_udp)) < 0) {
perror("Error binding UDP socket to server address");
return EXIT_FAILURE;
}
// Listen for incoming connections on TCP socket
if (listen(tcp_sock, 5) < 0) {
perror("Error listening for incoming connections on TCP socket");
return EXIT_FAILURE;
} else {
printf("Listening for incoming connections on TCP socket (port %i).\n", PORT_TCP);
}
// Accept incoming connection on TCP socket
client_len = sizeof(client_tcp);
client_tcp_sock = accept(tcp_sock, (struct sockaddr *) &client_tcp, &client_len);
if (client_tcp_sock < 0) {
perror("Error accepting incoming connection on TCP socket");
return EXIT_FAILURE;
} else {
printf("Accepted connection on TCP socket !\n");
}
while (1) {
// Receive data from UDP socket and send to TCP socket
bytes_received_udp = recvfrom(udp_sock, buffer, sizeof(buffer), 0, (struct sockaddr *) &client_udp, &client_len);
if (bytes_received_udp > 0) {
printf("Data received from UDP : %s\n", buffer);
if (send(client_tcp_sock, buffer, bytes_received_udp, 0) < 0) {
perror("Error sending data to TCP socket");
return EXIT_FAILURE;
} else {
printf("Sent data to TCP socket.\n");
//memset(buffer, 0, BUFFER_SIZE); // emptying buffer
}
}
// Receive data from TCP socket and broadcast via UDP socket
bytes_received_tcp = recv(client_tcp_sock, buffer, sizeof(buffer), MSG_DONTWAIT);
if (bytes_received_tcp == 0) { // connection closed
printf("TCP connection closed !\n");
close(client_tcp_sock);
return EXIT_FAILURE;
} else if (bytes_received_tcp > 0) {
printf("Data received from TCP : %s\n", buffer);
if (sendto(udp_sock, buffer, bytes_received_tcp, 0, (struct sockaddr *) &server_udp, sizeof(server_udp)) < 0) {
perror("Error sending data to UDP socket");
return EXIT_FAILURE;
} else {
printf("Sent data to UDP socket.\n");
//memset(buffer, 0, BUFFER_SIZE); // emptying buffer
}
}
}
// Close sockets
close(tcp_sock);
close(udp_sock);
return EXIT_SUCCESS;
}
When I test it with netcat (nc localhost 12312), it looks like it is sending the messages (as I receive them back), but they aren't really broadcasted (tested with netcat too and not received on other machine).
The other way works fine (nc -bu 255.255.255.255 5678), messages are sent to the local socket, even on another machine.
What am I doing wrong ?
CodePudding user response:
The UDP socket is not sending broadcast packets because you're not sending to a broadcast address.
The SO_BROADCAST
option doesn't automatically make all outgoing packets broadcast. It just enables you to send broadcast.
If you want to send to a broadcast address, you need to do so explicitly.
struct sockaddr_in udp_broadcast;
memset(&udp_broadcast, 0, sizeof(udp_broadcast));
udp_broadcast.sin_family = AF_INET;
udp_broadcast.sin_port = htons(PORT_UDP);
udp_broadcast.sin_addr.s_addr = INADDR_BROADCAST; // same as 255.255.255.255
...
if (sendto(udp_sock, buffer, bytes_received_tcp, 0,
(struct sockaddr *) &udp_broadcast, sizeof(udp_broadcast)) < 0) {
...