I got the following two functions for sending and receiving strings in C. When I use sendStrBuffer()
in my client maching and recvStrBuffer()
in my server, it works fine. But when I try to use the sendStrBuffer()
on server and recvStrBuffer()
in client, it won't work. It throws me an error code: 10014 which means:
Bad address. 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).
When I don't use these function but the normal send() and recv() (wihtout the while loops), I don't get any errors. So I'm doing something wrong in my one of my functions (probably buffer overflow) but can't point at it.
int sendStrBuffer(char* buffer, int sizeBuffer)
{
// send buffer size
uint32_t num = htonl(sizeBuffer);
char* converted_num = (char*)#
int res = send(ClientSocket, converted_num, sizeof(uint32_t), 0);
if (res == SOCKET_ERROR)
{
printf("error send\n");
}
// send buffer
int totalSent = 0;
int sent = 0;
while (totalSent < sizeBuffer)
{
sent = send(ClientSocket, buffer + totalSent, sizeBuffer - totalSent, 0);
if (sent == SOCKET_ERROR)
{
printf("error sending buffer\n");
return SOCKET_ERROR;
}
totalSent += sent;
//printf("sent: %d\n", sent);
//printf("totalSent: %d\n", totalSent);
}
}
int recvStrBuffer(SOCKET s, char* buffer, int bzeroSize)
{
bzero(buffer, bzeroSize);
int totalReceived = 0;
int received = 0;
// recv buffer size
char b[sizeof(uint32_t)];
int r = recv(s, b, sizeof(uint32_t), 0);
if (r == SOCKET_ERROR)
{
printf("error recv\n");
}
uint32_t sizeBuffer = ntohl_ch(&b[0]);
// recv buffer
while (totalReceived < sizeBuffer)
{
received = recv(s, buffer + totalReceived, sizeBuffer - totalReceived, 0);
if (received == SOCKET_ERROR)
{
printf("error receiving buffer\n");
return SOCKET_ERROR;
}
totalReceived += received;
//printf("received: %d\n", received);
//printf("totalReceived: %d\n", totalReceived);
}
//printf("%s", buffer);
}
I'm busy with this for 2 days now, if someone can help I will appreciate it. Thank you. How can I use both functions properly to send and receive strings without getting any errors?
This is the code. It does work when I keep it like this. When I change /LINE 1/ and /LINE 2/ to my own functions, it throws the 10014 error and closes the program.
Server:
char x[20];
printf("Give command: "); // give command to client
scanf_s("%s", x, 20);
/*LINE 1*/
int r = send(s, x, 20, 0); // let's say I send "test"
if (r == SOCKET_ERROR)
{
printf("1 error: %d\n", WSAGetLastError());
goto jump;
}
if(strncmp(buff, "test", 4) == 0)
{
sendStrBuffer(s, "Hello friend!", 20); // this works
}
Client:
char buff[20];
/*LINE 2*/
int ress = recv(ClientSocket, buff, 20, 0);
if (ress == SOCKET_ERROR)
{
printf("Server disconnected1 %d\n", WSAGetLastError());
break;
}
if(strncmp(buff, "test", 4) == 0)
{
char buffer[20];
recvStrBuffer(ClientSocket, buffer, 20); // this works
printf("%s\n", buffer);
}
Edit (changed my code to this):
const char* recvStrBuffer(SOCKET s)
{
char buffer[18384];
bzero(buffer, 18384);
int totalReceived = 0;
int received = 0;
// recv buffer size
char b[sizeof(uint32_t)];
int r = recv(s, b, sizeof(uint32_t), 0);
if (r == SOCKET_ERROR)
{
printf("error recv\n");
return "error";
}
uint32_t sizeBuffer = ntohl_ch(&b[0]);
// recv buffer
while (totalReceived < sizeBuffer)
{
received = recv(s, buffer + totalReceived, sizeBuffer - totalReceived, 0);
if (received == SOCKET_ERROR)
{
printf("error receiving buffer %d\n",WSAGetLastError());
return "return";
}
totalReceived += received;
//printf("received: %d\n", received);
//printf("totalReceived: %d\n", totalReceived);
}
//printf("%s", buffer);
return buffer;
}