Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm attempting to receive data over a socket connection from a python script. I seem to be failing in how to receive this data.

intreadETH(intsock, char **msg){
    printf("Read\n");
    int n;
    *msg = (char *)malloc(sizeof(char) * 256);
    bzero(*msg, 256);
    n = read(sock, *msg, 255);
    return n;
}


Its a simple read function.

I've successfully passed a message from the host computer(python) to the server(C) with a simple message. My python code is just for testing right now.

import socket
import time

HOST = '192.168.99.149'  # The server's hostname or IP address
PORT = 8080        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    time.sleep(2)
    s.sendall(b'Hello, world\r')
     s.detach()
     s.close()


This works, the server receives the full message "Hello, world" with no issues. I can then send the message as many times as I want with no issues.

What I have tried:

Now if I change the python script to look like this.
import socketimport time

HOST = '192.168.99.149'  # The server's hostname or IP address
PORT = 8080        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

s.connect((HOST, PORT))
    time.sleep(2)
    packet = bytearray()
    packet.append(0xBF)
    packet.append(0xAB)
    packet.append(0xCD)
    packet.append(0xEF)
    packet.append(0x0D)
    
    bytePacket = bytes(packet)
    s.sendall(bytePacket)
    time.sleep(2)
    s.detach()
    s.close()


Both the original script and this updated script is sending messages of type bytes with a return carriage. So they look, and think, they are identical on the python side. However, I feel like on the server(C) side, they are different.

when I send the message I get what looks like four overlappting diamonds with ? in each of them.

I've attempted to make changes to the print statement, but I get warnings if I change the print statement to %d, %x, or %c. Thinking the formatter just needed to be changed.

Does anyone have any insight as to what I'm missing.
Posted
Updated 27-May-20 8:50am
v3

1 solution

When using sendall you should the correct data type for the input parameter. And this is bytes and not bytesarray. This may a small but important difference.

Best is to create the send buffer outside the sendall call and not inline.
 
Share this answer
 
Comments
Nightpoison 27-May-20 13:43pm    
@karsenk

Hey, thanks for the tip. while this fixed the looping issue, I'm still not getting anything readable on the C side. I changed up the python script to do everything outside of sendall, but what I receive on the other end it garbled up.
Rick York 27-May-20 15:41pm    
The data you are sending is not exactly readable in text console. It is just a bunch of characters. Try sending something that is an actual text string like "abcdefg". If that is not exactly what you want then display the values received in hexadecimal format since that is how you load them.

One thing needs to be made clear : you are not sending a hexadecimal array. You are sending a set of five characters. The characters can range in value from 0 to 255. You are loading the characters into the array in hexadecimal format but that is irrelevant. You could have loaded the characters 0x31, 0x32, and 0x33 if you wanted to. Those happen to be equivalent to '1', '2', and '3' in ASCII format.
Nightpoison 28-May-20 8:05am    
Ok, that makes sense. I've updated my code and I'm now seeing the correct characters when I use '1', '2', etc. Now that I see that I'm able to pull out the original data, 0xBF, etc and display that using %x. I'm set. Thank you for your insight and help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900