How to send a structure containing pointers over Windows winsock send()? - c++

I'm creating a server/client program in C++ using Winsock.
pack the SLogin and Sid to char array
Sid to buf[0]
and SLogin to buf[1]
Sid to get from buf[0]
Problems in to get Slogin from buf[1].
This is my code:
#include <Winsock2.h>
#include <Windows.h>
#include <stdio.h>
#include <string>
#pragma comment (lib, "ws2_32.lib")
struct Sid
{
unsigned int id;
};
struct SLogin
{
char * login;
char * password;
};
struct SloginRet
{
bool OkOrFalse;
};
SOCKET sSocket = INVALID_SOCKET;
void Connect()
{
WSAData data;
if(WSAStartup(MAKEWORD(2,2), &data) != NO_ERROR)
{
printf("WSAStartup: %d\n", WSAGetLastError());
return;
}
sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sSocket == INVALID_SOCKET)
{
printf("socket: %d\n", WSAGetLastError());
return;
}
sockaddr_in service;
memset(&service, 0, sizeof(service));
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(5000);
connect(sSocket, (SOCKADDR*)&service, sizeof(service));
Sid idd;
idd.id = 1001;
char buffer[1];
memcpy(&buffer[0], &idd, sizeof(idd));
Sid ida;
memcpy( &ida, &buffer[0], sizeof(int));
printf("%d %d\n", ida.id, sizeof(buffer));
SLogin login;
login.login = "Vitor";
login.password = "123";
memcpy(&buffer[1], &login, sizeof(char));
SLogin *pLogin = (SLogin*)buffer[1];
printf("%s", pLogin->login);
// send(sSocket, buffer,strlen(buffer), 0);
closesocket(sSocket);
WSACleanup();
}
void main()
{
Connect();
system("pause");
}
Server Recv:
recv(socket,buffer,1,0);
char buffer[1]; //receive
Sid id;
memcpy(&id, &buffer[0],sizeof(buffer[0]));
SLogin login;
memcpy(&login, &buffer[1],sizeof(buffer[1]));
Using memcpy to copy Sid to char array buf[0], (char buf[1])
and get Sid from buf OK.
My Error on SLogin:
I copy with memcpy SLogin login to buf[1] and get Slogin from the buf with problems.

You are not allocating enough memory. Your buffer is only 1 char in size (sizeof(char) is 1 byte), but you are trying to stuff 12 bytes into it (sizeof(Sid) is 4 bytes, sizeof(SLogin) is 8 bytes). Even if you did allocate enough memory, it still would not work, as SLogin contains pointers to outside memory, which you are not serializing into a contiguous format that you can send/recv safely.
Try something more like this instead:
#pragma pack(push, 1)
// or equivilent for your compiler
struct Sid
{
unsigned int id;
};
struct SLogin
{
char login[20];
char password[20];
};
#pragma pack(pop)
// or equivilent for your compiler
#include <Winsock2.h>
#include <Windows.h>
#include <stdio.h>
#include <string>
#pragma comment (lib, "ws2_32.lib")
SOCKET sSocket = INVALID_SOCKET;
void Connect()
{
WSAData data;
int err = WSAStartup(MAKEWORD(2,2), &data);
if (err != NO_ERROR)
{
printf("WSAStartup: %d\n", err);
return;
}
sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sSocket == INVALID_SOCKET)
{
printf("socket: %d\n", WSAGetLastError());
return;
}
sockaddr_in service;
memset(&service, 0, sizeof(service));
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("127.0.0.1");
service.sin_port = htons(5000);
if (connect(sSocket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
{
printf("connect: %d\n", WSAGetLastError());
closesocket(sSocket);
return;
}
char buffer[sizeof(Sid)+sizeof(SLogin)];
Sid *pSid = (Sid*) &buffer[0];
PSid->id = 1001;
printf("%ul\n", pSid->id);
SLogin *pLogin = (SLogin*) &buffer[sizeof(Sid)];
strncpy(pLogin->login, "Vitor", 20);
strncpy(pLogin->password, "123", 20);
printf("%.20s\n", pLogin->login);
//send(sSocket, buffer, sizeof(buffer), 0);
closesocket(sSocket);
WSACleanup();
}
void main()
{
Connect();
system("pause");
}
char buffer[256]; //receive
int numBytes = recv(socket, buffer, sizeof(buffer), 0);
...
Sid *pSid = (Sid*) &buffer[0];
SLogin *pLogin = (SLogin*) &buffer[sizeof(Sid)];

Related

simple multicast app not working on different computer on the same network

If I launch 2 instances of the code below in the same computer the multicast works fine.
If I launch it on a different computer in the same network I won't receive anything.
Any idea what could be wrong? This code should compile as is.
I am testing on win10 but I get similar results when I run this on linux.
#include "pch.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include "winsock2.h"
#include <iostream>
#include <conio.h>
#include <thread>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
char mcastGroup[] = "224.1.2.3";
int mcastPort = 5435;
int CreateSocket()
{
return socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
void JoinGroup(int sck)
{
struct ip_mreq grp;
grp.imr_multiaddr.s_addr = inet_addr(mcastGroup);
grp.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sck, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&grp, sizeof(grp)) < 0)
{
printf("Error in joining group\n");
closesocket(sck);
exit(1);
}
}
int receiver()
{
int sck = CreateSocket();
int reuse = 1;
if (setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
{
perror("Socket reuse address error\n");
closesocket(sck);
exit(1);
}
JoinGroup(sck);
struct sockaddr_in lclSck;
memset((char *)&lclSck, 0, sizeof(lclSck));
lclSck.sin_family = AF_INET;
lclSck.sin_port = htons(mcastPort);
lclSck.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sck, (struct sockaddr*)&lclSck, sizeof(lclSck)))
{
perror("Error in binding socket\n");
closesocket(sck);
exit(1);
}
while (1)
{
int blen;
char buf[1024];
blen = sizeof(buf);
memset(buf, 0, blen);
struct sockaddr_in addrin;
int addrinlen = sizeof(addrin);
memset(&addrin, 0, sizeof(addrin));
int res = recvfrom(sck, buf, blen, 0, (sockaddr *)&addrin, &addrinlen);
if (res<0)
{
printf("Message read error\n");
closesocket(sck);
exit(1);
}
else
{
printf(": %s\n", buf);
}
}
return 0;
}
int sender()
{
int sck = CreateSocket();
struct in_addr lclInterface;
lclInterface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sck, IPPROTO_IP, IP_MULTICAST_IF, (char *)&lclInterface, sizeof(lclInterface)) < 0)
{
printf("Local interface error\n");
exit(1);
}
else
{
printf("Local interface set\n");
}
u_char ttl = 5;
setsockopt(sck, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl));
while (1)
{
int blen;
char buf[1024];
blen = sizeof(buf);
memset(buf, 0, blen);
for (int i = 0; i < 100; i++)
{
fgets(buf, blen, stdin);
sockaddr_in grpSck;
memset((char *)&grpSck, 0, sizeof(grpSck));
grpSck.sin_family = AF_INET;
grpSck.sin_port = htons(mcastPort);
grpSck.sin_addr.s_addr = inet_addr(mcastGroup);
if (sendto(sck, buf, blen, 0, (struct sockaddr*)&grpSck, sizeof(grpSck)) < 0)
{
printf("Error in sending message");
}
}
}
return 0;
}
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
std::thread t1([&] { receiver(); return 0; });
sender();
WSACleanup();
}
This code should work, the problem was that I was using IP_MULTICAST_IF, that forces to use a network interface different from the default one. In case one needs to use such a thing I was able to get the multicast working by following
Thanks to Remy Lebeau advice, that is to make sure you are binding the sockets to IPs that are in the same network.
Here is working code the code:
#ifdef WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#endif
#include <thread>
char mcastGroup[] = "224.1.2.3";
int mcastPort = 5435;
void PrintAddrIn(sockaddr_in addr_in)
{
char str[255];
inet_ntop(AF_INET, &addr_in.sin_addr, (char *)str, sizeof(str));
printf("%s", str);
}
int receiver(int sck)
{
while (1)
{
char buf[1024];
memset(buf, 0, sizeof(buf));
struct sockaddr_in addrin;
socklen_t addrinlen = sizeof(addrin);
memset(&addrin, 0, sizeof(addrin));
int res = recvfrom(sck, buf, sizeof(buf), 0, (sockaddr *)&addrin, &addrinlen);
if (res<0)
{
printf("Message read error\n");
exit(1);
}
else
{
PrintAddrIn(addrin); printf(": %s\n", buf);
}
}
return 0;
}
int sender(int sck)
{
while (1)
{
sockaddr_in grpSck;
memset((char *)&grpSck, 0, sizeof(grpSck));
grpSck.sin_family = AF_INET;
grpSck.sin_port = htons(mcastPort);
grpSck.sin_addr.s_addr = inet_addr(mcastGroup);
for (int i = 0; i < 100; i++)
{
char buf[1024];
fgets(buf, sizeof(buf), stdin);
if (sendto(sck, buf, strlen(buf), 0, (struct sockaddr*)&grpSck, sizeof(grpSck)) < 0)
{
printf("Error in sending message");
exit(1);
}
}
}
return 0;
}
int main()
{
#ifdef WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
int sck = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Set reuse
//
int reuse = 1;
if (setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
{
perror("Socket reuse address error\n");
exit(1);
}
else
{
printf("Socket reuse address successfull\n");
}
// Join mcast group
//
struct ip_mreq grp;
grp.imr_multiaddr.s_addr = inet_addr(mcastGroup);
grp.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sck, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&grp, sizeof(grp)) < 0)
{
printf("Error in joining group\n");
exit(1);
}
else
{
printf("Group joined successfully\n");
}
// Bind socket
//
struct sockaddr_in lclSck;
memset((char *)&lclSck, 0, sizeof(lclSck));
lclSck.sin_family = AF_INET;
lclSck.sin_port = htons(mcastPort);
lclSck.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sck, (struct sockaddr*)&lclSck, sizeof(lclSck)))
{
perror("Error in binding socket\n");
exit(1);
}
else
{
printf("Socket binding successfull\n");
}
u_char ttl = 5;
setsockopt(sck, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl));
std::thread t1([&] { receiver(sck); return 0; });
sender(sck);
#ifdef WIN32
WSACleanup();
#endif
}

push notification in FCM using http post

I'd like to send http post to my fcm clients.
I made android client app using FCM, and server is executed on Window environment.
I was finding how to push notification that fcm offer, finally
I found that using http, I can make it easily.
fire base said that I can use this.
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
"to": "/topics/foo-bar",
"data": {
"message": "This is a Firebase Cloud Messaging Topic Message!",
}
}
but I don't know how to apply this to my code.
because I didn't learn how to use http.
to match host, I used 'gethostbyname' function.
However this function return 'nullptr'!
what I want to know is what hname should be? my local ip? or what?
I really confused.
this is my code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#define MAXLINE 4096
#define MAXSUB 400
SSIZE_T process_http(int sockfd, char *page, char *poststr,char *auKey,char *topic)
{
char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
SSIZE_T n;
snprintf(sendline, MAXSUB,
"https:%s\r\n"
"Content-type: application/json\r\n"
"Authorization:key=%s\r\n"
"{\r\n"
"\"to\":\"\\topics\\%s\",\r\n"
"\"data\" : {\r\n"
"\"message\" : \"%s\",\r\n"
"}\r\n"
"}\r\n"
, page, auKey, topic, poststr);
printf("send data : %s\n\n", sendline);
send(sockfd, sendline, strlen(sendline), 0);
while ((n = recv(sockfd, recvline, MAXLINE, 0)) > 0)
{
recvline[n] = '\0';
printf("%s\n\n", recvline);
}
return n;
}
int main()
{
WSADATA wsaData;
SOCKET hSocket;
int strlen;
hostent *Host;
SOCKADDR_IN servAddr;
//********** You can change. Put any values here *******
char *hname = "https://fcm.googleapis.com/fcm/send";
char *page_0 = "//fcm.googleapis.com/fcm/send";
char *auKey = "AAAAXTiYFNU:APA91bGcH-Ee7JDVC9ZvDA4n09VR3W6x3AEcVrWQjrhbCeQQ_L5pF-7mNRgeMg0xW8g78uLVkjygJ93Za3NL7BXxDvetpZYTSHfiitZwPpiD9iQhgslNuy5Mlz4iuhAHILUpXhAK_o1W";
char *poststr_0 = "1#";// using camera Number
char *topic = "news"; // input topic name
//*******************************************************
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed.\n");
exit(1);
}
while (1)
{
hSocket = socket(PF_INET, SOCK_STREAM, 0);
if (hSocket == INVALID_SOCKET)
{
printf("Socket failed.\n");
exit(1);
}
//Host = gethostbyname(hname);
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(hname);
servAddr.sin_port = htons(80);
if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
{
printf("Connect failed.\n");
exit(1);
}
printf("Connected\n\n");
process_http(hSocket, page_0, poststr_0, auKey, topic);
closesocket(hSocket);
Sleep(10000);
}
WSACleanup;
return 0;
}
I made it based on your source. This source used "C". Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#define MAXLINE 4096
#define MAXSUB 400
SSIZE_T process_http(int sockfd, char *page, char *hname, char *data,char *auKey)
{
char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
SSIZE_T n;
sprintf(sendline,
// Header
"POST %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Content-Type: application/json;charset=utf-8;\r\n"
"Authorization:key=%s\r\n"
"Content-Length: %d\r\n\r\n"
// Data
"%s", page, hname, auKey, strlen(data), data);
printf("send data : %s\n\n", sendline);
send(sockfd, sendline, strlen(sendline), 0);
while ((n = recv(sockfd, recvline, MAXLINE, 0)) > 0)
{
recvline[n] = '\0';
printf("recv : %s\n\n", recvline);
}
return n;
}
int main()
{
WSADATA wsaData;
SOCKET hSocket;
int strlen;
struct hostent *Host;
SOCKADDR_IN servAddr;
char *hname = "fcm.googleapis.com";
char *page_0 = "/fcm/send";
char *auKey = "(Input your authorization key)";
char *data = "";
data =
"{\"to\":\"/topics/(Input your topic)\","
"\"data\":{"
"\"title\":\"TEST TITLE\","
"\"message\":\"TEST_Message\""
"}"
"}\"\r\n";
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed.\n");
exit(1);
}
if ((Host = gethostbyname(hname)) == NULL) {
fprintf(stderr, " gethostbyname error for host: %s:",hname);
exit(1);
}
hSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket == INVALID_SOCKET)
{
printf("Socket failed.\n");
exit(1);
}
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET; // IP v4
servAddr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)*Host->h_addr_list));
servAddr.sin_port = htons(80);
if (connect(hSocket, (SOCKADDR*)&servAddr, sizeof(servAddr)) == SOCKET_ERROR)
{
printf("Connect failed.\n");
exit(1);
}
printf("Connected\n\n");
process_http(hSocket, page_0, hname, data, auKey);
closesocket(hSocket);
WSACleanup;
return 0;
}

Multithreaded winsock server not accepting clients c++

So I have a winsock application in which the computer tries to accept a client, and then starts a new thread with the a pointer to the class object with client info which it copies and then deletes with the delete keyword.
The backbone is copied but the beginning of the OOP-structure is my work and the current implementation seems to be giving me some issues.
I noticed the socket might not copy in the correct format, but have no idea on how to fix this. As the client tries to connect it throws me the error message of that the server refused the connection. Anyway, here is the code of the server.
UPDATE:
It's working.
// main.h
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <process.h>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <memory>
#pragma comment(lib,"ws2_32.lib" )
class clientData{
private:
SOCKET clientSocket;
std::string name;
public:
clientData(SOCKET &clientSock, const char *tName);
clientData(clientData *cliDat);
~clientData();
inline SOCKET *getClientSocket(){
return &clientSocket;
}
inline std::string *getName(){
return &name;
}
};
And here is the main cpp file
// main.cpp
#include "main.h"
unsigned int __stdcall ServClient(void *data);
int main(int argc, char *argv[])
{
WSADATA wsaData;
int iResult;
sockaddr_in addr;
SOCKET sock, client[10];
addr.sin_family = AF_INET;
addr.sin_port = htons(13337);
addr.sin_addr.S_un.S_addr = INADDR_ANY;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);//2.2
if (iResult)
{
printf("WSA startup failed");
return 0;
}
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{
printf("Invalid socket");
return 0;
}
int addrlen = sizeof(sockaddr_in);
iResult = bind(sock, (sockaddr*)&addr, sizeof(sockaddr_in));
if (iResult)
{
printf("bind failed %u", GetLastError());
return 0;
}
iResult = listen(sock, SOMAXCONN);
if (iResult)
{
printf("iResult failed %u", GetLastError());
return 0;
}
int iterationCount = 0;
while ((iterationCount < 10 &&
(client[iterationCount] = accept(sock, (SOCKADDR*)&addr, &addrlen)) != SOCKET_ERROR))
{
if (client[iterationCount] == INVALID_SOCKET){
printf("invalid client socket",GetLastError());
continue;
}
++iterationCount;
char tempName[100] = { '\0' };
sprintf_s(tempName, sizeof(tempName), "Client %u", iterationCount);
clientData *tempCLdata = new clientData(client[iterationCount - 1], tempName);
_beginthreadex(0, 0, ServClient, (void*)tempCLdata, 0, 0);
tempCLdata = nullptr;
}
return 0;
}
unsigned int __stdcall ServClient(void *data)
{
clientData cliDat((clientData*)data);
delete (clientData*)data;
printf("Client connected\n");
int recvLen = 1;
char chunk[1024] = { '\0' };
while ((recvLen = recv(*cliDat.getClientSocket(), chunk, 1024, 0)) > 0){
printf("%.*s", recvLen, chunk);
}
if (recvLen == -1)
perror("Socket recv() problem..\n");
else
printf("End of data on socket, closing..\n");
closesocket(*cliDat.getClientSocket());
return 0;
}
clientData::clientData(SOCKET &clientSock, const char *tName){
clientSocket = clientSock;
name.assign(tName);
}
clientData::clientData(clientData *cliDat){
SOCKET *clientSocketPtr = cliDat->getClientSocket();
clientSocket = *clientSocketPtr;
name.assign(cliDat->getName()->c_str());
}
clientData::~clientData(){
}
Also here is the relevant bit of client code, as requested.
char buffer[1023] = { '\0' };
int heartbeatCount = 0;
while (true)
{
sprintf_s(buffer, sizeof(buffer), "Heartbeat %d", heartbeatCount + 1);
send(sock, buffer, strlen(buffer) + 1, 0);
++heartbeatCount;
Sleep(1000);
}

C++ - Windows - recvfrom() failing with "No such file or directory" on simple server

Here is the code of the very simple server I'm trying to make:
Network.h:
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <system_error>
#include <string>
#include <iostream>
#pragma once
class WSASession
{
public:
WSASession()
{
int ret = WSAStartup(MAKEWORD(2, 2), &data);
if (ret != 0)
throw std::system_error(WSAGetLastError(), std::system_category(), "WSAStartup Failed");
}
~WSASession()
{
WSACleanup();
}
private:
WSAData data;
};
class UDPSocket
{
public:
UDPSocket()
{
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
throw std::system_error(WSAGetLastError(), std::system_category(), "Error opening socket");
}
~UDPSocket()
{
closesocket(sock);
}
void SendTo(const std::string& address, unsigned short port, const char* buffer, int len, int flags = 0)
{
sockaddr_in add;
add.sin_family = AF_INET;
add.sin_addr.s_addr = inet_addr(address.c_str());
add.sin_port = htons(port);
int ret = sendto(sock, buffer, len, flags, reinterpret_cast<SOCKADDR *>(&add), sizeof(add));
if (ret < 0)
throw std::system_error(WSAGetLastError(), std::system_category(), "sendto failed");
}
void SendTo(sockaddr_in& address, const char* buffer, int len, int flags = 0)
{
int ret = sendto(sock, buffer, len, flags, reinterpret_cast<SOCKADDR *>(&address), sizeof(address));
if (ret < 0)
throw std::system_error(WSAGetLastError(), std::system_category(), "sendto failed");
}
sockaddr_in RecvFrom(char* buffer, int len, int flags = 0)
{
sockaddr_in from;
int size = sizeof(from);
int ret = recvfrom(sock, buffer, len, flags, reinterpret_cast<SOCKADDR *>(&from), &size);
if (ret < 0)
throw std::system_error(WSAGetLastError(), std::system_category(), "recvfrom failed");
// make the buffer zero terminated
buffer[ret] = 0;
return from;
}
void Bind(unsigned short port)
{
sockaddr_in add;
add.sin_family = AF_INET;
add.sin_addr.s_addr = htonl(INADDR_ANY);
add.sin_port = htons(port);
int ret = bind(sock, reinterpret_cast<SOCKADDR *>(&add), sizeof(add));
if (ret < 0)
throw std::system_error(WSAGetLastError(), std::system_category(), "Bind failed");
}
private:
SOCKET sock;
};
Listener server:
try
{
WSASession Session;
UDPSocket Socket;
char buffer[512];
Socket.Bind(22);
while (1)
{
sockaddr_in add = Socket.RecvFrom(buffer, sizeof(buffer));
std::string srecv(buffer);
}
}
catch (std::system_error& e)
{
std::cout << e.what();
}
All is perfect until it accepts a connection from a client. The recvfrom function fails with error "No such file or directory"
Does anyone see the problem? I know the port isn't problematic because I was listening on the same port from a .net application which works perfectly.
You have:
const std::string& address, unsigned short port, const char* buffer, int len, int flags = 0)
and
int ret = sendto(sock, buffer, len, flags, reinterpret_cast<SOCKADDR *>(&add)
The cast is invalid. A std::string isn't a SOCKADDR. You have to convert, probably by calling getaddrinfo().

C++ UDP recvfrom is acting strange, WSAGetLastError = 10014

My code is behaving very strange on Windows, but working on Linux...
This is my server.cpp:
#include <cstdio>
#include "packet.h"
#include "socket.h"
int main(int argc, char *argv[])
{
Socket s;
s.bindAt(1337);
for (int i = 0; i < 20; i++) {
Packet p;
int32_t a;
char *b;
int abc = s.receive();
printf("abc = %d\n", abc);
printf("error = %d\n", WSAGetLastError());
p.getInt(&a);
p.getString(&b);
printf("int = %d\nstring = %s\n", a, b);
delete[] b;
}
return 0;
}
and here is the socket.cpp:
Socket::Socket()
{
#ifdef _WIN32
WSADATA wsa;
if (sockNum == 0 && WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
throw 1;
#endif
sock = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef _WIN32
if (sock == INVALID_SOCKET)
#else
if (sock == -1)
#endif
throw 2;
addrlen = 0;
sockNum++;
}
int Socket::bindAt(unsigned short port)
{
struct sockaddr_in sa = { 0 };
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(port);
sa.sin_addr.s_addr = htonl(INADDR_ANY);
return bind(sock, (struct sockaddr *) &sa, sizeof(sa));
}
ssize_t Socket::receive()
{
ssize_t n;
#ifdef _WIN32
char msg[100];
n = recvfrom(sock,msg, sizeof(msg), 0,(SOCKADDR*) &addr, &addrlen);
#else
n = recvfrom(sock, p->buf, p->bufSize, 0,
(struct sockaddr *) &addr, &addrlen);
#endif
/*if (n < 0)
p->bufSize = 0;
else
p->bufSize = n;*/
return n;
}
and basically the header of it:
typedef SOCKET socket_t;
typedef int ssize_t;
class Socket
{
public:
socket_t sock;
socklen_t addrlen;
struct sockaddr_in addr;
Socket();
~Socket();
int connect(const char *ip, unsigned short port);
int bindAt(unsigned short port);
ssize_t send(Packet *p);
ssize_t receive();
};
If I change the last 2 parameters of the recvfrom, the (SOCKADDR*) &addr, and &addrlen to NULL it works, but what is wrong with these 2 parameters?
instead of this :
addrlen = 0;
do this:
addrlen = sizeof(sockaddr_in)
it should work, because you are identifying correctly the size of the output address buffer pointer.
From MSDN description of WSAEFAULT (10014):
The system detected an invalid pointer address in attempting to use a pointer argument of a call. This error occurs if an application passes an invalid pointer value, or if the length of the buffer is too small. For instance, if the length of an argument, which is a sockaddr structure, is smaller than the sizeof(sockaddr).
So if you supply an uninitialized value of addrlen, it may be too small, and result in this error.
If addr is NULL, it means you don't want the address filled in, so addrlen is ignored.