-1

I've been working on a project for school , where we've been assigned the task to make Roulette game with 2 clients and a server (TCP). So i figured i'd make a void function for both the server and the client, to realize the communication ,but I met this error, and couldn't find where I messed up , if anyone knows , please let me know ! :)

Client:

void bet(int client,const int size,char message[])
{
    char to_server[size];
    if (recv(client,message,size,0) < 0)
    {
        Receive_Error obj;
        throw obj;
    }
    std::cout << message << std::endl;
    std::cin >>to_server;
    if (send(client,to_server,sizeof(to_server),0) < 0)
    {
        Send_Error obj;
        throw obj;
    }
}

Server :

void bet(int client,char message[],const int size)
{
    char to_client[size] = "Mire fogad ?\nColor[b/r] + Number [0-9] + Bet\n";
    if (send(client,to_client,sizeof(to_client),0) < 0)
    {
        Send_Error obj;
        throw obj;
    }
    if (recv(client,&message,size,0) < 0)
    {
        Receive_Error obj;
        throw obj;
    }
    std::cout << message ;

}

In both parties , the "message" variable looks like this :

char* message = new char[size];
for ( not important )
{
   bet(client_socket,message,size)
}
delete[] message;

size is a const int with the value of 256; P.S:The reason I tagged both C and C++ , is because it is a requirement to use the C functions (included in sys/socket.h,sys/types.h,netinet/in.h) but I mainly studied C++ , so I am trying to cut corners.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
Gameerik
  • 5
  • 4
  • 5
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Nick May 07 '18 at 14:22
  • 2
    There's a small but very important difference in the `recv` between the first `bet` and the second `bet` (the former is correct, the latter is not). – molbdnilo May 07 '18 at 14:25
  • @molbdnilo Thank you ! I corrected it , however i still get the same error message . – Gameerik May 07 '18 at 14:27
  • Looks like you have a buffer overflow in `char to_server[size]` and/or in `char to_client[size]`. Check that first. – Jabberwocky May 07 '18 at 14:29
  • You can't ignore the return value of rcve(). It is only the exact length of the message by accident. If it is not then pretty ugly stuff happens, beyond the corrupt data, it will not be a zero-terminated string. A simple protocol on top of TCP is to first transmit the message length so the receiver always knows when to stop calling rcve(). – Hans Passant May 07 '18 at 14:30
  • @MichaelWalz I tried removing the ‘\n’ from the server message , I did go further , now both clients failed to receive the message , and on the client side I still got the stack smash error – Gameerik May 07 '18 at 14:33
  • @HansPassant I added ‘\0’ to the end of the server message , however I still get the same the error Edit: I think I misunderstood you , could you explain a bit more detailed sir ? – Gameerik May 07 '18 at 14:36

1 Answers1

0

Okay, after a long time, even 100 was too much of a buffer , so i lowered it to 50 .

Gameerik
  • 5
  • 4