Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
cmake_minimum_required(VERSION 3.21)
project(exec)

set(CMAKE_CXX_STANDARD 17)
#add_subdirectory(udp1)
project(MyProject VERSION 1.0)

add_executable(exec main.cpp UDPSocket.cpp)
target_link_libraries(exec ws2_32)
set(CMAKE_CXX_STANDARD 20)

# Create a library for the server and client classes
add_library(MyLib SHARED
clien_server_class/server.cpp
clien_server_class/client.cpp
)
target_include_directories(MyLib PUBLIC clien_server_class)

# Link the library against the required Winsock library
target_link_libraries(MyLib PRIVATE ws2_32)

# Add the executables
add_executable(Server main.cpp)
target_link_libraries(Server PRIVATE MyLib)

add_executable(Client main_s.cpp)
target_link_libraries(Client PRIVATE MyLib)

# Add compiler warnings
if(MSVC)
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()
58 changes: 58 additions & 0 deletions client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Created by Emad on 5/6/2023.
//

#include "client.h"

client::client(char* group , const int &port): m_group{group},m_port{port} {

#ifdef _WIN32
//
// Initialize Windows Socket API with given VERSION.
//
WSADATA wsaData;
if (WSAStartup(0x0101, &wsaData)) {
perror("WSAStartup");
return;
}
fd = WSAStartup(MAKEWORD(2,2), &wsaData);
#else
set_file_descriptor();
#endif
setup();
}

client::~client() {
WSACleanup();
}

void client::message(std::string str) {
m_message = str.c_str();
}

void client::set_file_descriptor_linux() {
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return;
}
}

void client::setup() {
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(m_group);
addr.sin_port = htons(m_port);
}

void client::send() {
int addrlen = sizeof(addr);
recvfrom(
fd,
msgbuf,
MSGBUFSIZE,
0,
(struct sockaddr *) &addr,
&addrlen
);
}
49 changes: 49 additions & 0 deletions client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Created by Emad on 5/6/2023.
//

#ifndef CMAKE_CLIENT_H
#define CMAKE_CLIENT_H

#ifdef _WIN32
#include <Winsock2.h> // before Windows.h, else Winsock 1 conflict
#include <Ws2tcpip.h> // needed for ip_mreq definition for multicast
#include <Windows.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> // for sleep()
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#define MSGBUFSIZE 256

class client {

private:
char* m_group{"239.255.255.250"}; // e.g. 239.255.255.250 for SSDP
int m_port{0};
const char *m_message{""};
int fd{0};
char msgbuf[MSGBUFSIZE];
struct sockaddr_in addr;
public:
client(char* group , const int &port);
virtual ~client();
void message(std::string str);
void set_file_descriptor_linux();
void setup();
void send();
void receive();


public:

};


#endif //CMAKE_CLIENT_H
16 changes: 4 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
#include <iostream>
#include "UDPSocket.h"
#include <thread>
#include <string>
int main() {
UDPSocket *s = new UDPSocket(5000, "255.255.255.255", TRUE, TRUE);

std::string msg = "";
std::cout << "Enter your message" << std::endl;
//#pragma comment(lib, "Ws2_32.lib")
#define MSGBUFSIZE 256

while (1) {
std::cin>>msg;
s->send(msg.c_str(), msg.length());
}
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
11 changes: 11 additions & 0 deletions main_s.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Created by Emad on 5/6/2023.
//
#include <iostream>
//#pragma comment(lib, "Ws2_32.lib")


int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
5 changes: 5 additions & 0 deletions server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Created by Emad on 5/6/2023.
//

#include "server.h"
14 changes: 14 additions & 0 deletions server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Created by Emad on 5/6/2023.
//

#ifndef CMAKE_SERVER_H
#define CMAKE_SERVER_H
#define MSGBUFSIZE 256

class server {

};


#endif //CMAKE_SERVER_H