Saikat Chakraborty
Saikat Chakraborty

Reputation: 108

How to send messages properly from python server to c++ client?

I have a python server running on the following code:

import socket
ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ss.bind(('0.0.0.0',8080))
ss.listen(5)
cs,adr=ss.accept()
print("Got a connection from:"+str(adr))
while 1:
    msg=input('Enter your message:')
    cs.send(msg.encode('ascii'))
cs.close()

For client I'm using C++.
client.h:

#include<WinSock2.h>
#include<WS2tcpip.h>
#include<iostream>
#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)
class client {
private:
    const char* IP;
    int PORT;
    SOCKET sock;
public:
    client(char* ip, int port){
        IP = ip;
        PORT = port;
    }
    void createSession();//Creates socket and connects with the server.
    void sendData(char* message);//Sends message to the server, it works fine
    char* receive()
    {
        char message[2000];
        if (recv(sock, message, 2000, 0) == SOCKET_ERROR)
        {
            WSACleanup();
            return (char*)"Failed to receive Message";
        }
        return message;
    }
};

client.cpp:

#include"client.h"
#include<iostream>
#define IP "127.0.0.1"
#define port 8080
using namespace std;
int main()
{
    client c1((char*)IP, port);
    c1.createSession();
    while (1)
    {
        char* message;
        message = c1.receive();
        cout << IP << ':' << port << " Says: " << message << endl;
    }
    return 0;
}

but the message that I'm receiving is garbage. I'm guessing it's a conversion error because for python clients, I'd use recv(length).decode('ascii') to decode the message first when receiving. How do I fix this?

Upvotes: 0

Views: 732

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

char* receive()
{
    char message[2000];
    if (recv(sock, message, 2000, 0) == SOCKET_ERROR)
    {
        WSACleanup();
        return (char*)"Failed to receive Message";
    }
    return message;
}
...
message = c1.receive();
cout << IP << ':' << port << " Says: " << message << endl;

This is wrong in multiple places:

  • You are reading the data into a buffer allocated on the function stack (message[2000]). This buffer will be automatically freed after the functions exits, so what you return from the function is no longer there.
  • When printing you treat the message as a \0-terminated char[] even though its not \0 terminated. This means that garbage after the received message will be printed though, until a \0 gets found. It might well be that no \0 gets found within the buffer in which case it will continue to read, possibly outside allocated memory and thus crash.
  • You assume that the send in Python will send the full message but this is not guaranteed. Check the return value of send on how much was actually send or use sendall.
  • You assume that recv in C will receive the full message but this is not guaranteed. TCP has no concept of a message and recv will return just return the bytes already received. Especially with large messages or small TCP windows this might not be the complete message you intended.

Upvotes: 2

Related Questions