Reading RSysLog tcp messages - c++

I am attempting to read RSysLog log from a machine running debian linux. I have setup a server on another linux machine and can read the log messages. I want to be able to read them on a windows machine from c++. I have setup the remote machine to distribute the logs to the windows machine. I have an application running on the machine that writes log messages at intervals. The setup to distribute the logs is of the form local1.* ##192.168.1.8 which is the ip address of the windows machine.
I know I am not thinking about this correctly, the remote machine does not attempt a connection to the windows machine and the program hangs on the accept call.
Edit:
After some further testing I figured out that the connect occurs when the syslog daemon is started. To use tcp the server has to be alive when the daemon is started. This is not what I want so I will work more with the UDP implementation.
Here is the code I am using, I have tried to make it as simple as possible.
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
SOCKET h;
h = socket(AF_INET, SOCK_STREAM, 0);
if (h == INVALID_SOCKET)
{
std::cout << "Socket Failure: " << std::endl;
return 1;
}
std::cout << "Socket Success: " << std::endl;
// The server
sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("192.168.1.8");
server.sin_port = htons(514);
iResult = bind(h, (SOCKADDR *)&server, sizeof(server));
if (iResult == SOCKET_ERROR)
{
std::cout << "Bind Failure: " << std::endl;
closesocket(h);
return 1;
}
std::cout << "Bind Success: " << std::endl;
if (listen(h, SOMAXCONN))
{
std::cout << "Listen Failure: " << std::endl;
errcode = WSAGetLastError();
closesocket(h);
return 1;
}
std::cout << "Listen Success: " << std::endl;
struct sockaddr_in dest;
int addrlen = sizeof(dest);
SOCKET s = accept(h, (sockaddr *)&dest, &addrlen);
if (s == INVALID_SOCKET)
{
std::cout << "Accept Failure: " << std::endl;
}
else
{
std::cout << "Accept Success: " << std::endl;
}
WSACleanup();
return 0;

You probably need SOCK_DGRAM instead of SOCK_STREAM.
Syslog on port 514 seems to send UDP packets, not TCP.
You could also try to connect to the server with a browser to check if all the rest is ok (seems ok though) - 192.168.1.8:514 (while still in TCP mode).

Related

I coded a c++ server/client program using windows sockets. getting 10061 from wsagetlasterror() trying to connect using my public ip addr

Im attempting to connect to my server via my client and if i use my local host address 127.0.0.1 everything works perfectly fine. it connects and im able to send data. however whenever i switch it to my public address it fails to connect. i already went through my router and forwarded the port im using. and ive tested if the port is open with canyouseeme.org and port forward network utilities. the error code i get from getlasterror() is 10061. i also called my isp and they told me the port is open so i should be able to connect but im not. i turned off my firewall and AV as well. i have windows 10 64 bit. im at a loss and would really appreciate the help. the error code means that the server is actively refusing the connection. but nothing happens on my server side...it stays in the accept() call.
this is the server code. how do i solve this issue? i currently have both the server and client on one computer.
class Server
{
public:
Server(int port)
:
port(port)
{
std::cout << "Initializing server...\n";
if (WSAStartup(version, &wsData) == 0)
{
auto result = createListenSock();
//if no value is returned, it was a success
if (!result.has_value())
std::cout << "Successfully created a server! Accepting Clients now.\n";
else
std::cout << result.value() << std::endl;
}
else
std::cout << "Failed to initialize WinSock. Error : " << WSAGetLastError() << std::endl;
}
~Server()
{
closesocket(listensock);
closesocket(client);
WSACleanup();
}
std::optional<std::string> acceptClient()
{
int sz = sizeof(clientinfo);
if ((client = accept(listensock, (sockaddr*)&clientinfo, &sz)) == INVALID_SOCKET)
return {};
else
{
char clientipaddr[100];
inet_ntop(AF_INET, &clientinfo.sin_addr, clientipaddr, sizeof(clientipaddr));
return std::string(clientipaddr);
}
}
private:
std::optional<std::string> createListenSock()
{
//AF_INET for ipv4
//SOCK_STREAM A socket type that provides sequenced, reliable, two-way,
//connection-based byte streams with an OOB data transmission mechanism.
if ((listensock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
return "Failed to created listen socket...\n";
initConnectionInfo();
if (bind(listensock, (sockaddr*)&serverInfo, sizeof(serverInfo)) == SOCKET_ERROR)
return "Failed to bind...\n";
if (listen(listensock, SOMAXCONN) == SOCKET_ERROR)
return "Failed to put into listening mode...\n";
//if it returns an empty container, that means it was successful
return {};
}
void initConnectionInfo()
{
//make it use ipv4
serverInfo.sin_family = AF_INET;
//htons converts the port from host to network byte order.
serverInfo.sin_port = htons(port);
//specify that we can accept any ip address
serverInfo.sin_addr.S_un.S_addr = INADDR_ANY;
}
private:
WSADATA wsData;
int version = MAKEWORD(2,2);
SOCKET listensock;
sockaddr_in serverInfo;
int port;
private:
//client variables
SOCKET client;
sockaddr_in clientinfo;
};
int main()
{
//create server
Server server(1024);
auto clientip = server.acceptClient();
if (!clientip.has_value())
{
std::cout << "Couldn't connect to client.";
system("pause");
return 0;
}
std::cout << "Connected to client!" << std::endl << "Client IP Address: " << clientip.value() << std::endl;
and this is the client code
SOCKET startclient()
{
WSADATA data;
//server to connect to
std::string ipAddress = "127.0.0.1"; //IF I CHANGE THIS TO MY PUBLIC IP ADDRESS IT FAILS TO CONNECT
int port = 1024;
// initialize winsock
if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
{
std::cout << "Could not initialize WinSock...\n";
return SOCKET_ERROR;
}
//create a socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
std::cout << "Couldn't create a socket, Error " << WSAGetLastError() << std::endl;
WSACleanup();
return SOCKET_ERROR;
}
//this tells our socket what info to use to connect to the server
sockaddr_in serverinfo;
serverinfo.sin_family = AF_INET;
//htons converts the port number from host to network byte order
serverinfo.sin_port = htons(port);
inet_pton(AF_INET, ipAddress.c_str(), &serverinfo.sin_addr);
std::cout << "Attempting to connect..." << std::endl;
if (connect(sock, (sockaddr*)&serverinfo, sizeof(serverinfo)) == SOCKET_ERROR)
{
std::cout << "Failed to connect... Error : " << WSAGetLastError() << std::endl;
WSACleanup();
closesocket(sock);
return SOCKET_ERROR;
}
else
std::cout << "Connected!" << std::endl;
return sock;
}
int main()
{
SOCKET client = startclient();
if (client == SOCKET_ERROR)
{
system("pause");
return 0;
}

WinSock c++ inet_ntop always displays 204.204.204.204 (and accept() didn't failed)

I'm trying to make a winsock server and I want to display the client's ip on the server when he connects but that's where there is a problem. Every time I try to connect it display 204.204.204.204. I tried to connect with another computer but the result was the same.
result in localhost
After this, I started looking for people having the same problem as me on this website and I found several people who had the same as me but they all had either the accept or the inet_ntop function that wasn't working correctly. So I check and none of this two functions return an error. Maybe I'm stupid but I really can't figured out what's the problem. (btw english is not my first language so please tell me if you noticed or if my english isn't too bad)
the part of the code that isn't working
sockaddr_in from;
int clientlen = sizeof(from);
// accept
SOCKET client = accept(server, (SOCKADDR*)&client, &clientlen);
if (client == INVALID_SOCKET)
{
std::cout << "Error in accept(): " << WSAGetLastError << std::endl;
WSACleanup();
}
else
{
char clientIp[17];
if (inet_ntop(AF_INET, &from.sin_addr, clientIp, 17) == NULL)
{
std::cout << "Can't get the client's ip: " << WSAGetLastError() << std::endl;
}
std::cout << "ip connected: " << clientIp << std::endl;
the whole code if you need it
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <string>
#pragma comment(lib, "ws2_32.lib")
int main()
{
std::cout << "--- Tcp/ip Server ---" << std::endl;
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
if (server == INVALID_SOCKET)
{
std::cout << "error in SOCKET(): "<< WSAGetLastError() << std::endl;
WSACleanup();
}
sockaddr_in s;
s.sin_family = AF_INET;
s.sin_addr.s_addr = INADDR_ANY;
s.sin_port = htons(52000);
// bind
if (bind(server, (sockaddr*)&s, sizeof(s)) == SOCKET_ERROR)
{
std::cout << "Error: bind()" << std::endl;
}
//listen
if (listen(server, SOMAXCONN) == SOCKET_ERROR)
{
std::cout << "Error in listen(): " << WSAGetLastError() << std::endl;
WSACleanup();
}
sockaddr_in from;
int clientlen = sizeof(from);
// accept
SOCKET client = accept(server, (SOCKADDR*)&client, &clientlen);
if (client == INVALID_SOCKET)
{
std::cout << "Error in accept(): " << WSAGetLastError << std::endl;
WSACleanup();
}
else
{
char clientIp[17];
if (inet_ntop(AF_INET, &from.sin_addr, clientIp, 17) == NULL)
{
std::cout << "Can't get the client's ip: " << WSAGetLastError() << std::endl;
}
std::cout << "ip connected: " << clientIp << std::endl;
// the code isn't finished yet
system("pause");
WSACleanup();
}
return 0;
}
You are passing the address of the wrong variable in the second parameter of accept().
You are passing the address of your SOCKET client variable that you are about to assign the result of accept() to. C++ allows a variable's address to be taken when declaring and initializing the variable in the same statement. But that is not what you want in this case. You need to pass the address of your sockaddr_in from variable instead:
sockaddr_in from;
int clientlen = sizeof(from);
// accept
SOCKET client = accept(server, (SOCKADDR*)&from, &clientlen); // <-- &from instead of &client
You are leaving your from variable uninitialized, and your compiler fills uninitialized variables with 0xCC (decimal 204) bytes in debug mode, so that is why you end up seeing 204.204.204.204 (hex 0xCC 0xCC 0xCC 0xCC) from inet_ntop() when you don't initialize your from variable properly.

TCP Server Socket Opening Fail in C++

I am currently creating a program where I require a TCP server for communication with an android application. I have written and tested the TCP server as an individual project and it runs completely fine. When including this into a larger project, where I have other processes, it no longer opens the socket for listening.
My project is being created in Visual Studio 2017 and the libraries I am using are:
WS2_32.lib for the TCP
OpenCV for image processing
Libcurl for sending files to a database
ACTi SDK for pulling image feed from a camera
The TCP server code I have written is (taken from https://www.youtube.com/watch?v=WDn-htpBlnU&t=162s):
void TCPServer()
{
//Initalize winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOk = WSAStartup(ver, &wsData);
if (wsOk != 0)
{
cerr << "Can't init winsock" << endl;
return;
}
//Create a socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
cerr << "Can't create socket" << endl;
return;
}
//Bind the socket to an ip address and port
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(100);
hint.sin_addr.S_un.S_addr = INADDR_ANY; //Could use inet_pton()
bind(listening, (sockaddr*)&hint, sizeof(hint));
//Tell Winsock the socket is for listening
listen(listening, SOMAXCONN);
//Wait for a connection
sockaddr_in client;
int clientSize = sizeof(client);
SOCKET clientsocket = accept(listening, (sockaddr*)&client, &clientSize);
char host[NI_MAXHOST]; //Clients remote name
char service[NI_MAXHOST]; //Service the client is on
ZeroMemory(host, NI_MAXHOST);
ZeroMemory(service, NI_MAXHOST);
if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
cout << host << "connected on port " << service << endl;
}
else {
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
cout << host << " connected on port " << ntohs(client.sin_port) << endl;
}
//Close listening socket
closesocket(listening);
//While loop:accept and echo message back to client
char buf[4096];
while (true)
{
ZeroMemory(buf, 4096);
//Wait for client to send data
int bytesReceived = recv(clientsocket, buf, 4096, 0);
if (bytesReceived == SOCKET_ERROR)
{
cerr << "Error in recv()" << endl;
break;
}
if (bytesReceived == 0)
{
cout << "Client disconnected" << endl;
break;
}
cout << buf << endl;
}
//Close the socket
closesocket(clientsocket);
//cleanup windsock
WSACleanup();
}
As I said, this code works as an individual project however, when I include this into my overall project the socket fails to open and a connection to 204.204.204.204 is instantly made. This was checked in both situations by viewing all open sockets with netstat. I feel this may be a library conflict, maybe between WS2_32 and libcurl, however I am unsure.
I am currently testing this by calling TCPServer() in my main, however I plan to run the server threaded along with my other processes.
Any suggestions as to why the socket may be failing to open would be much appreciated.
After carrying out checks on the function it was possible to narrow the failure down to the bind(...) function. It seems the error is down to having using namespace std;
The solution was to call bind from the global namespace by doing ::bind(...). This solution was found here: Compilation errors with socket bind function

c++ : TCP Server "bind" function failed (errno 98) if I do not wait enough time between two consecutive app launch [duplicate]

This question already has answers here:
What are the use cases of SO_REUSEADDR?
(2 answers)
Closed 7 years ago.
discovering TCP socket, I made a very simple test based on my understanding of the subject and some tuto found on the net
Server :
void Server(void)
{
int localSocket;
int distantSocket;
sockaddr_in serverInfo;
sockaddr_in clientInfo;
int sizeOfSocketInfo = sizeof(struct sockaddr_in);
/* Open Socket */
std::cout << "open socket" << std::endl;
localSocket = socket(AF_INET, SOCK_STREAM, 0);
if (localSocket == -1)
{
std::cout << "open failed, error - " << (int)errno << std::endl;
exit(errno);
}
/* Configure server */
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_family = AF_INET;
serverInfo.sin_port = htons(61001);
/* Bind Socket */
std::cout << "bind socket" << std::endl;
if (bind (localSocket, (sockaddr *) &serverInfo, sizeof(serverInfo)) == -1)
{
std::cout << "bind failed, error - " << (int)errno << std::endl;
exit(errno);
}
/* Wait for client */
std::cout << "Wait for client ..." << std::endl;
listen(localSocket, 1);
distantSocket = accept(localSocket, (sockaddr *)&clientInfo, (socklen_t*)&sizeOfSocketInfo);
std::cout << "client connected - " << inet_ntoa(clientInfo.sin_addr) << std::endl;
/* Close Socket */
close (localSocket);
close (distantSocket);
std::cout << "socket closed" << std::endl;
}
and client :
void Client(void)
{
int localSocket;
sockaddr_in clientInfo;
/* Open Socket */
std::cout << "open socket" << std::endl;
localSocket = socket(AF_INET, SOCK_STREAM, 0);
if (localSocket == -1)
{
std::cout << "open failed, error - " << (int)errno << std::endl;
exit(errno);
}
clientInfo.sin_family = AF_INET;
clientInfo.sin_port = htons(61001);
clientInfo.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Open Socket */
std::cout << "connect to server" << std::endl;
if (connect(localSocket, (sockaddr*)&clientInfo, sizeof(clientInfo)) < (int)0)
{
std::cout << "connect failed, error - " << (int)errno << std::endl;
exit(errno);
}
std::cout << "connected !" << std::endl;
close(localSocket);
}
When I launch the server in one terminal and the client in another one, it seems to be ok :
server side :
> ./tcpTest -s
open socket
bind socket
Wait for client ...
client connected - 127.0.0.1
socket closed
>
and client side :
> ./tcpTest -c
open socket
connect to server
connected !
>
But, if, just after this first try, I launch the server again ...
> ./tcpTest -s
open socket
bind socket
bind failed, error - 98
>
And I have to wait a "certain time", I don't know exactly how long, one minute maybe, to have the server working again.
I can't figure out what's happening, looking to open socket with sockstat does not show anything strange (I only see mozilla socket).
I found this guy having the same problem but in Ruby
basic Ruby TCP server demo fails on startup: `bind': Address already in use, Errno::EADDRINUSE
If this is really the same problem, how can I apply the same solution in C++ ? Or do you have any idea ?
Thank you
You may need to use both SO_REUSEADDR and SO_REUSEPORT. You can further go through the documentation of Socket: Socket Docs
const int trueFlag = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &trueFlag, sizeof(int)) < 0)
error("Failure");
You can use reuse Port in similar way. Hope this helps
Try setsockopt(SO_REUSEADDR), this should help.

C++ Socket Connection automatically establishing?

I am currently programming a C++ module that creates a socket server thread, which polls the accept() function every 1ms. I have a test module that spoofs a client connection that is also running. The test module initiates the creation of the server thread. After the thread is created and verified to be running, the client runs the connect() command as a test. My code states that a connection was established, returning a 0 on the connect command. However, the accept() running in my server thread never receives the connection.
The server is bound to accepting any IP:50000, and the client is not bound, but has 127.0.0.1:50000 set as its destination. Is my Linux environment automatically accepting this connection?
Here is the code for my server's socket:
int nSocket;
int nOn = 1;
int nControl;
struct sockaddr_in sServerAddress;
nSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(nSocket < 0)
{
std::cout << "Failed to create socket\n";
}
if(setsockopt(nSocket, SOL_SOCKET, SO_REUSEADDR, &nOn, sizeof(int)) < 0)
{
std::cout << "failed to set socket option\n";
}
nControl = fcntl(nSocket, F_GETFL);
if(fcntl(nSocket, F_SETFL, O_NONBLOCK | nControl) < 0)
{
std::cout << "set not blocking failed\n";
}
memset(&sServerAddress, 0x00, sizeof(struct sockaddr_in));
sServerAddress.sin_family = AF_INET;
sServerAddress.sin_addr.s_addr = htonl(INADDR_ANY);
sServerAddress.sin_port = htons(mtListenPort);
if(bind(nSocket, (struct sockaddr*)&sServerAddress, sizeof(struct sockaddr_in)) < 0)
{
std::cout << errno << "bind failed\n";
}
if(listen(nSocket, MAXPENDING) < 0)
{
std::cout << "listen failed\n";
}
Here is the code for my test client's socket:
int nSocket;
nSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(nSocket < 0)
{
std::cout << "Failed to create socket\n";
}
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
inet_aton(LOCAL_HOST, &serverAddress.sin_addr);
serverAddress.sin_port = htons(LISTEN_PORT00);
char msg[] = "Hello World.";
usleep(10000);
if (connect(nSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
{
std::cout << errno << "Could not connect to the server.\n";
}
usleep(10000);
if (sendto(nSocket, msg, strlen(msg), 0, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
{
std::cout << errno << "Could not send data to the server.\n";
}
Here is a small part of the thread that runs the accept code.
while(mbListening)
{
nMessengerSocket = accept(mnSocket, (struct sockaddr*)&sClientAddr, &tClientAddrLength);
if(nMessengerSocket >= 0)
{
std::cout << "Accepted connection from " << inet_ntoa(sClientAddr.sin_addr) << std::endl;
mnConnections++;
mbListening = false;
}
mThreadRunningTime++;
usleep(1000);
}
Make sure tClientAddrLength is initialized to sizeof sClientAddr before calling accept(). That is a common error.
Don't silently ignore errors. Examine the error code in errno because it tells you what went wrong.
Are you using sendto? That's for UDP... Use send.
Also, check if the port is free:
telnet localhost 50000
netstat -a