I am relativily new to C++, therefore if the issue is basic I am sorry.
Problem
I am attempting to build a socket server which will soon be used to stream a video feed. When I build the server using g++ server.cpp -o server I can seen the buffer being sent from the client. However the moment I switch to building the project using CMake & make the client can not connect to the server socket. Do you know what maybe causing this issue? It only seems to happen when the server.cpp is built using make.
Please see the code for the server.cpp, client.cpp and the CMakeLists.txt below.
server.cpp
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main() {
int port = 30015;
int server = 0, client = 0;
struct sockaddr_in serverAddr;
server = socket(AF_INET, SOCK_STREAM, 0);
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
bind(server, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
listen(server, 0);
cout << "Listening for incoming connetions... :) " << " port: " << port << endl;
char buffer[1024] = { 0 };
while (1) {
client = accept(server, (struct sockaddr*)NULL, NULL);
cout << "Client Connected" << endl;
if (client < 0) break;
read(client, buffer, sizeof(buffer) - 1);
cout << "Client says: buffer is " << buffer << endl;
memset(buffer, 0, sizeof(buffer));
close(client);
cout << "Client Disconnected" << endl;
}
cout << "Server Disconnected" << endl;
return 0;
}
client.cpp
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main() {
char* sip = "127.0.0.1";
int port = 30015;
int serverSock = 0;
struct sockaddr_in addr;
serverSock = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_addr.s_addr = inet_addr(sip);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
cout << "Connecting to server: " << sip << " port: " << port << endl;
if (connect(serverSock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
cout << "failed to connect to server" << endl;
exit(1);
}
cout << "Connected to the server?!" << endl;
char buffer[1024] = {'h', 'e', 'l', 'l', 'o', '.'};
write(serverSock, buffer, strlen(buffer));
cout << "Message sent! With buffer in Client." << buffer << endl;
close(serverSock);
cout << "Socket Closed." << endl;
}
CMakeLists.txt (this is the same for both server and client however the project name and file path is different)
# CMakeLists.txt
# Older versions of CMake are likely to work just fine but, since
# I don't know where to cut off I just use the version I'm using
cmake_minimum_required(VERSION "3.17")
# name of this example project
project(serverSocket)
# set OpenCV_DIR variable equal to the path to the cmake
# files within the previously installed opencv program
set(OpenCV_DIR /usr/local/include/)
# Tell compiler to use C++ 14 features which is needed because
# Clang version is often behind in the XCode installation
set(CMAKE_CXX_STANDARD 14)
# configure the necessary common CMake environment variables
# needed to include and link the OpenCV program into this
# demo project, namely OpenCV_INCLUDE_DIRS and OpenCV_LIBS
find_package( OpenCV REQUIRED )
# tell the build to include the headers from OpenCV
include_directories( ${OpenCV_INCLUDE_DIRS} )
# specify the executable target to be built
add_executable(serverSocket server.cpp)
# tell it to link the executable target against OpenCV
target_link_libraries(serverSocket ${OpenCV_LIBS} )