-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
256 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
#include <iostream> | ||
#include <sys/socket.h> | ||
#include <arpa/inet.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <thread> | ||
#define TCP_PORT 1234 | ||
#define UDP_MULTICAST_GROUP "225.0.0.37" | ||
#define UDP_MULTICAST_PORT 5678 | ||
#define BUFFER_SIZE 1024 | ||
|
||
void receiveMulticastMsgUDP(int udp_sock) | ||
{ | ||
// std::cout << "Thread Launched" << std::endl; | ||
struct sockaddr_in sender_addr; | ||
socklen_t sender_addr_len = sizeof(sender_addr); | ||
char buffer[BUFFER_SIZE] = {0}; | ||
while (true) | ||
{ | ||
int bytes_received = recvfrom(udp_sock, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&sender_addr, &sender_addr_len); | ||
if (bytes_received == -1) | ||
{ | ||
std::cerr << "Failed to receive multicast message" << std::endl; | ||
// return -1; | ||
break; | ||
} | ||
|
||
std::cout << "\nReceived multicast message from " << inet_ntoa(sender_addr.sin_addr) << ": " << buffer << std::endl; | ||
memset(buffer, 0, BUFFER_SIZE); | ||
} | ||
|
||
close(udp_sock); | ||
} | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
// Create a tcp socket | ||
|
||
int sock = socket(AF_INET, SOCK_STREAM, 0); | ||
if (sock == -1) | ||
{ | ||
std::cerr << "Failed to create socket." << std::endl; | ||
return 1; | ||
} | ||
|
||
// Configure the server address and port for tcp | ||
sockaddr_in server_address; | ||
server_address.sin_family = AF_INET; | ||
server_address.sin_port = htons(TCP_PORT); | ||
if (inet_pton(AF_INET, "127.0.0.1", &server_address.sin_addr) <= 0) | ||
{ | ||
std::cerr << "Invalid address." << std::endl; | ||
return 1; | ||
} | ||
|
||
// Create UDP socket | ||
int udp_sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
if (udp_sock == -1) | ||
{ | ||
std::cerr << "Failed to create UDP socket" << std::endl; | ||
return -1; | ||
} | ||
|
||
// allow multiple sockets to use the same PORT number | ||
// | ||
u_int yes = 1; | ||
if (setsockopt(udp_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) | ||
{ | ||
perror("Reusing ADDR failed"); | ||
return 1; | ||
} | ||
|
||
// Join multicast group on UDP socket | ||
struct ip_mreq mreq; | ||
mreq.imr_multiaddr.s_addr = inet_addr(UDP_MULTICAST_GROUP); | ||
mreq.imr_interface.s_addr = htonl(INADDR_ANY); | ||
if (setsockopt(udp_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) == -1) | ||
{ | ||
std::cerr << "Failed to join multicast group" << std::endl; | ||
return -1; | ||
} | ||
|
||
// Bind UDP socket to port | ||
struct sockaddr_in udp_addr; | ||
udp_addr.sin_family = AF_INET; | ||
udp_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
udp_addr.sin_port = htons(UDP_MULTICAST_PORT); | ||
if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(udp_addr)) == -1) | ||
{ | ||
std::cerr << "Failed to bind UDP socket" << std::endl; | ||
return -1; | ||
} | ||
|
||
// Connect to the server | ||
if (connect(sock, (sockaddr *)&server_address, sizeof(server_address)) == -1) | ||
{ | ||
std::cerr << "Failed to connect to server." << std::endl; | ||
return 1; | ||
} | ||
|
||
std::thread t1(receiveMulticastMsgUDP, udp_sock); | ||
t1.detach(); | ||
|
||
// Send a message to the tcp server | ||
|
||
while (true) | ||
{ | ||
char buffer[BUFFER_SIZE] = {0}; | ||
// std::this_thread::sleep_for(std::chrono::seconds(1)); | ||
// sleep(1); | ||
// std::cout << "client " << sock << ":"; | ||
std::cin.getline(buffer, BUFFER_SIZE); | ||
if (send(sock, buffer, strlen(buffer), 0) == -1) | ||
{ | ||
std::cerr << "Failed to send message." << std::endl; | ||
return 1; | ||
} | ||
} | ||
// // Receive a response from the server | ||
// char buffer[1024] = {0}; | ||
// if (recv(sock, buffer, 1024, 0) == -1) { | ||
// std::cerr << "Failed to receive message." << std::endl; | ||
// return 1; | ||
// } | ||
// Receive multicast messages on UDP socket | ||
|
||
// struct sockaddr_in sender_addr; | ||
// socklen_t sender_addr_len = sizeof(sender_addr); | ||
|
||
// while (true) | ||
// { | ||
// int bytes_received = recvfrom(udp_sock, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&sender_addr, &sender_addr_len); | ||
// if (bytes_received == -1) | ||
// { | ||
// std::cerr << "Failed to receive multicast message" << std::endl; | ||
// return -1; | ||
// } | ||
|
||
// std::cout << "Received multicast message from " << inet_ntoa(sender_addr.sin_addr) << ": " << buffer << std::endl; | ||
// } | ||
|
||
// Close the socket | ||
// close(udp_sock); | ||
close(sock); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include <iostream> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <thread> | ||
#define MAX_CLIENTS 10 | ||
#define PORT 1234 | ||
#define UDP_PORT 5678 | ||
#define MULTICAST_ADDR "225.0.0.37" | ||
|
||
// function to handle client connection | ||
void handle_client(int client_socket) | ||
{ | ||
char buffer[1024] = {0}; | ||
while (true) | ||
{ | ||
int valread = read(client_socket, buffer, 1024); | ||
if (valread == 0) | ||
{ | ||
// client has disconnected | ||
printf("Client disconnected, socket fd is %d\n", client_socket); | ||
break; | ||
} | ||
printf("Message received from client (socket fd %d): %s\n", client_socket, buffer); | ||
|
||
// multicast message to all connected clients | ||
struct sockaddr_in multicast_addr; | ||
memset(&multicast_addr, 0, sizeof(multicast_addr)); | ||
multicast_addr.sin_family = AF_INET; | ||
multicast_addr.sin_addr.s_addr = inet_addr(MULTICAST_ADDR); | ||
multicast_addr.sin_port = htons(UDP_PORT); | ||
int udp_fd; | ||
if ((udp_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) | ||
{ | ||
perror("socket creation failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
int nbytes = sendto(udp_fd, (const char *)buffer, strlen(buffer), 0, (const struct sockaddr *)&multicast_addr, sizeof(multicast_addr)); | ||
if (nbytes < 0) | ||
{ | ||
perror("sendto"); | ||
// return 1; | ||
} | ||
close(udp_fd); | ||
|
||
// send(client_socket, buffer, strlen(buffer), 0); tcp | ||
memset(buffer, 0, sizeof(buffer)); | ||
} | ||
close(client_socket); | ||
std::cout << "Function finshed" << std::endl; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
// set up the server socket | ||
int server_fd, new_socket, valread; | ||
struct sockaddr_in address; | ||
int opt = 1; | ||
int addrlen = sizeof(address); | ||
char buffer[1024] = {0}; | ||
|
||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) | ||
{ | ||
perror("socket failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) | ||
{ | ||
perror("setsockopt"); | ||
exit(EXIT_FAILURE); | ||
} | ||
address.sin_family = AF_INET; | ||
address.sin_addr.s_addr = INADDR_ANY; | ||
address.sin_port = htons(PORT); | ||
|
||
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) | ||
{ | ||
perror("bind failed"); | ||
exit(EXIT_FAILURE); | ||
} | ||
if (listen(server_fd, MAX_CLIENTS) < 0) | ||
{ | ||
perror("listen"); | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
// accept incoming connections and handle data in separate threads | ||
while (true) | ||
{ | ||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) | ||
{ | ||
perror("accept"); | ||
exit(EXIT_FAILURE); | ||
} | ||
printf("New connection , socket fd is %d , ip is : %s , port : %d\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port)); | ||
|
||
// create new thread to handle client connection | ||
std::thread t(handle_client, new_socket); | ||
t.detach(); // allow thread to run independently | ||
} | ||
|
||
return true; | ||
} |