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

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.

Related

Sending struct over tcp socket in C++

I am trying to send struct over tcp socket. I am a newbie in socket programming, I did try the options suggested here already but those did not serve my purpose. Could someone pls help?
I have written Server.cpp and Client.cpp and both are compiling properly. However, when I am executing my Server to listen to the Client, I am not sure if the Server is able to recieve the structure from Client or not. Also, how can I read this structure once it is received?
Server.cpp
#include<iostream>
#include <cstring>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
struct UE
{
string Net;
int imsi;
} ;
UE UE2;
//cout<<UE1.imsi<<"\n"<<UE1.Net<<"\n";
int sock, cli, receive;
sockaddr_in server, client;
unsigned int len;
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket:");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(10000);
//cout<<htons(10000);
server.sin_addr.s_addr = INADDR_ANY;
//cout<<INADDR_ANY;
memset(&(server.sin_zero), '\0', 8);
len = sizeof(struct sockaddr_in);
if((bind(sock, (struct sockaddr *)&server, len)) == -1)
{
perror("Bind:");
exit(-1);
}
if((listen(sock, 5)) == -1)
{
perror("Listen:");
exit(-1);
}
while(1)
{
cli = accept(sock,(struct sockaddr *)&client, &len);
if(cli == -1)
{
perror("Accept");
exit(-1);
}
receive = recv(sock, (void*)&UE2, sizeof(UE2), NULL);
cout<<UE2.imsi;
//cout<<UE2.imsi<<"\n"<<UE2.Net;
//int sent = send(cli, (const void*)&mesg, sizeof mesg, 0);
//cout<<"Sent"<<sent<<" bytes to client :<<inet_ntoa(client.sin_addr)";
close(cli);
}
}
Client.cpp
#include <arpa/inet.h>
#include<iostream>
#include <cstring>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string>
#include<stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
struct UE
{
string Net;
int imsi;
} ;
UE UE1;
UE1.Net = "4G";
UE1.imsi = htons(8649);
int sock, receive;
struct sockaddr_in server;
char mesg[200];
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Socket:");
exit(-1);
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
//cout<<server.sin.port;
server.sin_addr.s_addr = inet_addr(argv[1]);
//cout<<server.sin_addr.s_addr;
memset(&(server.sin_zero), '\0', 8);
if(connect(sock, (struct sockaddr*)&server, sizeof(server)) == -1)
{
perror("Connect:");
exit(-1);
}
int count = 0;
while(count = 0)
{
send(sock, &UE1, sizeof(UE1), 0);
//receive = recv(sock, (void*)&mesg, sizeof mesg, NULL);
count++;
}
cout<<"Sent "<<UE1.imsi<<" and "<<UE1.Net<<" to Server \n";
close(sock);
}
There are at least 2 problems in your code:
You cannot send objects like std::string and anything that contains it this way (formally non-POD data), you need to marshall your data. There are plenty of libraries around (like google proto buffers) or you can write your own. This topic is too wide to cover it in the answer here.
You cannot expect that you receive data from TCP stream by the same chunk you sent it, you must write code that can handle receiving data by pieces (and send it that way as well).
You should never ever write a whole struct to a file or a socket.
Always write each field separately, and read them the same way.
When doing it this way you pay some memory overhead, but it's generally a good design for performance reasons because you don't want to do a write of each value to the socket.
When sending binary data you should always take care for the following things:
Different endianness
Different padding
Differences in the bye-sizes of intrinsic types
You need some functions like the following:
virtual MESSAGE_BUFFER * GetMessageAsBinaryPtr()
{
MESSAGE_BUFFER * binaryMsg = new MESSAGE_BUFFER;
UINT8 * ptrBuffer = &(*binaryMsg)[0];
ptrBuffer = this->serializeUInt16(ptrBuffer, this->m_majorVersion);
ptrBuffer = this->serializeUInt16(ptrBuffer, this->m_minorVersion);
ptrBuffer = this->serializeUInt32(ptrBuffer, (UINT32)this->m_messageType);
ptrBuffer = this->serializeUInt64(ptrBuffer, this->m_packetID);
ptrBuffer = this->serializeDouble(ptrBuffer, this->m_timestamp);
return binaryMsg;
}
virtual void CreateFromBinary(MESSAGE_BUFFER buffer)
{
UINT8 * ptrBuffer = &buffer[0];
ptrBuffer = this->deserializeUInt16FromBuffer(ptrBuffer, &this->m_majorVersion);
ptrBuffer = this->deserializeUInt16FromBuffer(ptrBuffer, &this->m_minorVersion);
UINT32 messageType = 0;
ptrBuffer = this->deserializeUInt32FromBuffer(ptrBuffer, &messageType);
this->SetMessageType((MessageTypes)messageType);
ptrBuffer = this->deserializeUInt64FromBuffer(ptrBuffer, &this->m_packetID);
ptrBuffer = this->deserializeDoubleFromBuffer(ptrBuffer, &this->m_timestamp);
}
inline UINT8 * serializeUInt16(UINT8 * buffer, UINT16 value)
{
buffer[1] = value;
buffer[0] = value >> 8;
return buffer + 2;
}
inline UINT8 * deserializeUInt16FromBuffer(UINT8 * buffer, UINT16 * pOutput)
{
*pOutput = (*pOutput << 8) + buffer[0];
*pOutput = (*pOutput << 8) + buffer[1];
return buffer + 2;
}
When you have such functions you can serialize and deserialize your structs to a buffer and then send this buffer over your socket.
A few points to note:
The struct to send is first serialized, field by field into a buffer
MESSAGE_BUFFER is of type UINT8[1024]
The serialization routine returns a pointer to the next free byte in the buffer, which we use to compute how many bytes it serialized to
Theres no protection against buffer overflows in my routines
There are few bugs in your code.
In server.cpp
sockaddr_in --> struct sockaddr_in
Once connection request is accepted by server using accept() call, it returns new file descriptor, with that new fd you should do read and write operation not with old one.
Replace below statement
receive = recv(sock, (void*)&UE2, sizeof(UE2), NULL); /** you are receiving with old fd called sock **/
with
receive = recv(cli, (void*)&UE2, sizeof(UE2), NULL);
client.cpp
using namespace std;
int main(int argc, char *argv[])
{
struct UE
{
string Net;
int imsi;
} ;
UE UE1;
UE1.Net = "4G";
UE1.imsi = htons(8649);
int sock, receive;
struct sockaddr_in server;
char mesg[200];
sock = socket(PF_INET, SOCK_STREAM, 0);
perror("Socket:");
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[2]));
server.sin_addr.s_addr = inet_addr(argv[1]);
memset(&(server.sin_zero), '\0', 8);
connect(sock, (struct sockaddr*)&server, sizeof(server));
perror("Connect:");
int count = 0;
send(sock, &UE1, sizeof(UE1), 0);
perror("send");
cout<<"Sent "<<UE1.imsi<<" and "<<UE1.Net<<" to Server \n";
close(sock);
}
server.cpp
using namespace std;
int main()
{
struct UE
{
string Net;
int imsi;
} ;
UE UE2;
int sock, cli, receive;
struct sockaddr_in server, client;
unsigned int len;
sock = socket(AF_INET, SOCK_STREAM, 0);
perror("Socket:");
server.sin_family = AF_INET;
server.sin_port = htons(10001);
server.sin_addr.s_addr = INADDR_ANY;
memset(&(server.sin_zero), '\0', 8);
len = sizeof(server);
bind(sock, (struct sockaddr *)&server, len);
perror("Bind:");
listen(sock, 1);
perror("Listen:");
cli = accept(sock,(struct sockaddr *)&client, &len);
perror("accept");
receive = recv(cli, ( void*)&UE2, sizeof(UE2), 0);
perror("recv");
cout << "rec = "<<receive<<endl;
cout<<UE2.imsi<<"\n";
close(sock);
perror("close");
}

Memory leak on socket

I'm writing a tcp proxy and while it seem to work it leaves a memory leak behind. I manipulated the code to forward the incoming packet to itself to create 10000 sockets and close them to see where the leak is. However I can't figure it out. I've used deleaker and it doesn't shows any leak(besides a small one that I don't care.)
But then I untick the two boxes and this comes out.
Any help would be appreciated!
Code:
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <tchar.h>
#include <process.h> /* _beginthread() */
// Need to link with Ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")
#define PORT "1234" /* Port to listen on */
#define BUF_SIZE 4096 /* Buffer for transfers */
typedef struct {
char *host;
char *port;
SOCKET sock;
}
HandleStruct;
unsigned int S2C(SOCKET from, SOCKET to)
{
char buf[BUF_SIZE];
unsigned int disconnected = 0;
size_t bytes_read, bytes_written;
bytes_read = recv(from, buf, BUF_SIZE, 0);
if (bytes_read == 0) {
disconnected = 1;
}
else {
bytes_written = send(to, buf, bytes_read, 0);
if (bytes_written == -1) {
disconnected = 1;
}
}
return disconnected;
}
unsigned int C2S(SOCKET from, SOCKET to)
{
char buf[BUF_SIZE];
unsigned int disconnected = 0;
size_t bytes_read, bytes_written;
bytes_read = recv(from, buf, BUF_SIZE, 0);
if (bytes_read == 0) {
disconnected = 1;
}
else {
bytes_written = send(to, buf, bytes_read, 0);
if (bytes_written == -1) {
disconnected = 1;
}
}
return disconnected;
}
void handle(void *param)
{
HandleStruct *args = (HandleStruct*) param;
SOCKET client = args->sock;
const char *host = args->host;
const char *port = args->port;
SOCKET server = -1;
unsigned int disconnected = 0;
fd_set set;
unsigned int max_sock;
struct addrinfo *res = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;
/* Get the address info */
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(host, port, &hints, &res) != 0) {
perror("getaddrinfo");
closesocket(client);
return;
}
/* Create the socket */
server = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (server == INVALID_SOCKET) {
perror("socket");
closesocket(client);
return;
}
/* Connect to the host */
if (connect(server, res->ai_addr, res->ai_addrlen) == -1) {
perror("connect");
closesocket(client);
return;
}
if (client > server) {
max_sock = client;
}
else {
max_sock = server;
}
/* Main transfer loop */
while (!disconnected) {
FD_ZERO(&set);
FD_SET(client, &set);
FD_SET(server, &set);
if (select(max_sock + 1, &set, NULL, NULL, NULL) == SOCKET_ERROR) {
perror("select");
break;
}
if (FD_ISSET(client, &set)) {
disconnected = C2S(client, server);
}
if (FD_ISSET(server, &set)) {
disconnected = S2C(server, client);
}
}
closesocket(server);
closesocket(client);
fprintf(stderr, "Sockets Closed: %d/%d", server, client);
_endthread();
return;
}
int _tmain(int argc)
{
WORD wVersion = MAKEWORD(2, 2);
WSADATA wsaData;
int iResult;
SOCKET sock;
struct addrinfo hints, *res;
int reuseaddr = 1; /* True */
/* Initialise Winsock */
if (iResult = (WSAStartup(wVersion, &wsaData)) != 0) {
fprintf(stderr, "WSAStartup failed: %dn", iResult);
return 1;
}
char * host = "127.0.0.1";
char * port = "1234";
/* Get the address info */
ZeroMemory(&hints, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(NULL, PORT, &hints, &res) != 0) {
perror("getaddrinfo");
WSACleanup();
return 1;
}
/* Create the socket */
sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sock == INVALID_SOCKET) {
perror("socket");
WSACleanup();
return 1;
}
/* Enable the socket to reuse the address */
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr,
sizeof(int)) == SOCKET_ERROR) {
perror("setsockopt");
WSACleanup();
return 1;
}
/* Bind to the address */
if (bind(sock, res->ai_addr, res->ai_addrlen) == SOCKET_ERROR) {
perror("bind");
WSACleanup();
return 1;
}
/* Listen */
if (listen(sock, 6500) == SOCKET_ERROR) {
perror("listen");
WSACleanup();
return 1;
}
freeaddrinfo(res);
int i = 0;
HandleStruct *arg;
arg = (HandleStruct *)malloc(sizeof( HandleStruct));
/* Main loop */
while(1) {
int size = sizeof(struct sockaddr);
struct sockaddr_in their_addr;
SOCKET newsock;
ZeroMemory(&their_addr, sizeof (struct sockaddr));
newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
if (newsock == INVALID_SOCKET) {
perror("acceptn");
}
else {
arg->sock = newsock;
arg->host = host;
arg->port = port;
if (i < 10000) {
_beginthread(handle, 0, (void*) arg);
i++;
}
}
}
closesocket(sock);
WSACleanup();
return 0;
}
I'm not familiar with reading the program in the screenshots you posted; however, you should probably be concerned about this line:
arg = (HandleStruct *)malloc(sizeof( HandleStruct));
Here you are allocating memory for a HandleStruct via malloc() which doesn't appear to be cleaned up anywhere with a subsequent call to free(). You pass arg into handle() but still don't deallocate the memory.
It doesn't appear to be handle()'s responsibility to clean arg up, so you should probably have a call to free() after the while loop, or you could allocate the HandleStruct at the beginning of each loop and deallocate it at the end.
Or you could save yourself the hassle and use std::unique_ptr, and optionally change your threads to std::thread, which self-documents who owns the memory etc:
void handle(std::unique_ptr<HandleStruct> args)
{
// Manipulate args
...
}
int main()
{
std::unique_ptr<HandleStruct> pHandle = std::make_unique<HandleStruct>();
for (;;)
{
...
pHandle->sock = newsock;
pHandle->host = host;
pHandle->port = port;
// Create thread:
std::thread t(&handle, pHandle);
// Wait for thread to finish so pHandle doesn't change while we are using it on another thread
// t.join();
}
}
Every socket uses some memory in the operating system.
Here the description in Linux : accept
ENOBUFS, ENOMEM
Not enough free memory. This often means that the memory
allocation is limited by the socket buffer limits, not by the
system memory.
The OS might not clean them up.
You are also trying to create 10000 threads, these might also take some memory, if the creation doesn't fail long before with errno set to EAGAIN.

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);
}

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

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)];

c++ tcp server (windows OS) - recv() delays when there is more then 1 connection to the server

When I'm trying to connect to the server with only 1 client, the recv() function on the server does not delay.
But when I'm starting the client console more then 1 time (something like 7 times), there is a delay of something like 2000ms after you send to the server packet with the function send() until the server will print the packet in is console.
Is there any solution without starting a thread for each client? (Windows limits the number of threads for each process).
The code is compiled with Visual Studio 2008, and this is the full server code:
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#include <Windows.h>
#include <stdio.h>
struct sslv3
{
#define max_clients 1024
private:
int cClient;
public:
SOCKET fd;
int CurrentClient()
{
return cClient;
}
struct client
{
client()
{
Valid = false;
}
bool Valid;
DWORD ip;
WORD port;
char ipstr[33];
char portstr[33];
SOCKET fd;
void StrGen()
{
wsprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip & 0xFF00)/0x100, (ip & 0xFF0000)/0x10000, (ip & 0xFF000000)/0x1000000);
wsprintf(portstr, "%d", port);
}
} clients[max_clients];
//
sslv3(bool server_client)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
cClient = 0;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//
DWORD timeout = 1;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
}
int Bind(WORD port)
{
int ret = 0;
sockaddr_in local;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(port);
if((ret = bind(fd, (struct sockaddr *)&local, sizeof(local)))
!= SOCKET_ERROR)
listen(fd, SOMAXCONN);
return ret;
}
int Accept()
{
SOCKET clientfd;
sockaddr_in client;
int addrlen = sizeof(client);
clientfd = accept(fd, (struct sockaddr *)&client, &addrlen);
if(clientfd == -1)
return -1;
clients[cClient].ip = client.sin_addr.S_un.S_addr;
clients[cClient].port = client.sin_port;
clients[cClient].StrGen();
clients[cClient].fd = clientfd;
clients[cClient].Valid = true;
//
DWORD timeout = 1;
setsockopt(clients[cClient].fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
cClient++;
if(cClient >= max_clients)
{
cClient = 0;
return max_clients - 1;
}
return cClient - 1;
}
int Connect(char ip[], WORD port)
{
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
return connect(fd, (const struct sockaddr*)&addr, sizeof(addr));
}
int Send(SOCKET sfd, void* buffer, int length)
{
return send(sfd, (char*)buffer, length, 0);
}
int Read(SOCKET sfd, void* buffer, int length)
{
return recv(sfd, (char*)buffer, length, 0);
}
};
sslv3 cssl(true);
DWORD WINAPI ReadThread(void* args)
{
while(true)
{
for(int j = 0; j <= cssl.CurrentClient(); j++)
{
if(cssl.clients[j].Valid)
{
char rpack[1024];
for(int i = 0; i < sizeof(rpack); i++)
rpack[i] = 0;
if(cssl.Read(cssl.clients[j].fd, rpack, sizeof(rpack)) > 0){
printf("%s:%s says: %s\n", cssl.clients[j].ipstr, cssl.clients[j].portstr, rpack);
}
}
}
Sleep(1);
}
return TRUE;
}
int main()
{
cssl.Bind(1234);
CreateThread(0,0,ReadThread,0,0,0);
while(true)
{
Sleep(1);
int cid = cssl.Accept();
if(cid != -1){
printf("%s:%s connected!\n", cssl.clients[cid].ipstr, cssl.clients[cid].portstr);
}
}
return 0;
}
The following is a full client code:
#include <WinSock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "ws2_32.lib")
#include <Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
struct sslv3
{
#define max_clients 1024
private:
int cClient;
public:
SOCKET fd;
int CurrentClient()
{
return cClient;
}
struct client
{
client()
{
Valid = false;
}
bool Valid;
DWORD ip;
WORD port;
char ipstr[33];
char portstr[33];
SOCKET fd;
void StrGen()
{
wsprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip & 0xFF00)/0x100, (ip & 0xFF0000)/0x10000, (ip & 0xFF000000)/0x1000000);
wsprintf(portstr, "%d", port);
}
} clients[max_clients];
//
sslv3(bool server_client)
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
cClient = 0;
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//
DWORD timeout = 1;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
}
int Bind(WORD port)
{
int ret = 0;
sockaddr_in local;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_family = AF_INET;
local.sin_port = htons(port);
if((ret = bind(fd, (struct sockaddr *)&local, sizeof(local)))
!= SOCKET_ERROR)
listen(fd, SOMAXCONN);
return ret;
}
int Accept()
{
SOCKET clientfd;
sockaddr_in client;
int addrlen = sizeof(client);
clientfd = accept(fd, (struct sockaddr *)&client, &addrlen);
if(clientfd == -1)
return -1;
clients[cClient].ip = client.sin_addr.S_un.S_addr;
clients[cClient].port = client.sin_port;
clients[cClient].StrGen();
clients[cClient].fd = clientfd;
clients[cClient].Valid = true;
//
DWORD timeout = 1;
setsockopt(clients[cClient].fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(DWORD));
cClient++;
if(cClient >= max_clients)
{
cClient = 0;
return max_clients - 1;
}
return cClient - 1;
}
int Connect(char ip[], WORD port)
{
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
return connect(fd, (const struct sockaddr*)&addr, sizeof(addr));
}
int Send(SOCKET sfd, void* buffer, int length)
{
return send(sfd, (char*)buffer, length, 0);
}
int Read(SOCKET sfd, void* buffer, int length)
{
return recv(sfd, (char*)buffer, length, 0);
}
};
sslv3 cssl(false);
int main()
{
cssl.Connect("127.0.0.1", 1234);
while(true)
{
printf("say: ");
char buf[1024];
for(int i = 0; i < sizeof(buf); i++)
buf[i] = 0;
cin >> buf;
int len = strlen(buf);
cssl.Send(cssl.fd, buf, len);
}
return 0;
}
The server seems 'idle' for 2 seconds, because some clients are handled after 2 sleeps, 1 second each.
This is clearly not the right way to handle more than one client on a server. You may want to check on select() - reference.
A very good tutorial for socket programming is Beej's