Thread: Receiving data, Hexidecimal array, from python script over Socket in C

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    4

    Receiving data, Hexidecimal array, from python script over Socket in C

    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.

    Code:
    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.

    Code:
    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.cloe()
    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.
    Receiving data, Hexidecimal array, from python script over Socket in C-correct-png
    Now if I change the python script to look like this.

    Code:
    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 this as my return

    Receiving data, Hexidecimal array, from python script over Socket in C-wrong-png

    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.
    Last edited by Salem; 05-27-2020 at 01:20 PM. Reason: removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,064
    packet.append(0xBF)
    packet.append(0xAB)
    packet.append(0xCD)
    packet.append(0xEF)
    Well since these aren't printable characters, what were you expecting?

    Instead of
    Code:
    printf("%s",msg);
    Code:
    char *p = msg;
    while ( *p ) {
      printf("%x\n", *p++);
    }
    Do you see the raw bytes you sent?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Overflow by sending / receiving ACK's
    By MaSSaSLaYeR in forum C Programming
    Replies: 13
    Last Post: 11-24-2011, 02:38 AM
  2. boost::python embedding external script file
    By Elkvis in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2011, 11:51 AM
  3. Sending/Receiving packets over socket
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 10-22-2010, 10:01 AM
  4. Calling a python script and obtaining a return value
    By bhdavis1978 in forum C Programming
    Replies: 3
    Last Post: 09-22-2010, 09:08 AM
  5. Issue receiving data from socket (multicast)
    By JessH in forum C++ Programming
    Replies: 11
    Last Post: 08-31-2010, 09:01 AM

Tags for this Thread