Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rx from python #113

Open
mhmodayman opened this issue Mar 26, 2023 · 7 comments
Open

Rx from python #113

mhmodayman opened this issue Mar 26, 2023 · 7 comments

Comments

@mhmodayman
Copy link

mhmodayman commented Mar 26, 2023

I am trying to receive data from Tx using a python script as the Rx (both on the same device)

Here is the code for Rx, I wrote a code that greps over available ports
It can grep over all ports except for 10001, and 44387

import socket

def create_socket(port_number):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(('127.0.0.1', port_number))
    server_socket.listen(1)

    return server_socket

socket_list = []

for port_number in range(1025,65536):
    try:
        socket_list.append(create_socket(port_number))
    except Exception: 
        print("didn't listen to ", port_number)

I am not sure how to receive from Tx, am I doing something wrong?

This is the configuration for Tx

<<<<<<<<<<<<<<< 42: InterProcess Comm Configuration File >>>>>>>>>>>>>>>>
1                                       ! Number of Sockets
**********************************  IPC 0   *****************************
TX                                      ! IPC Mode (OFF,TX,RX,TXRX,ACS,WRITEFILE,READFILE)
0                                       ! AC.ID for ACS mode
"State00.42"                            ! File name for WRITE or READ
SERVER                                  ! Socket Role (SERVER,CLIENT,GMSEC_CLIENT)
localhost     10001                     ! Server Host Name, Port 
TRUE                                    ! Allow Blocking (i.e. wait on RX)
TRUE                                    ! Echo to stdout
1                                       ! Number of TX prefixes
"SC"                                    ! Prefix 0
@ericstoneking
Copy link
Owner

Hi Mahmoud,

I see two possible problems. First, try making the IPC Mode be TX instead of RX. Then, make the number of TX prefixes be 1 instead of zero, and add the line
"SC" ! Prefix 0

It may help if I explain that "Tx" is slang for "transmit" and "Rx" is slang for "receive". If both ends of the socket are in Rx mode, then everyone is listening but no one is talking :-) Also, a non-zero number of TX prefixes are needed for Tx to actually transmit something. If "SC" works for you, then we can talk about how to refine what is being sent.

Your python looks plausible, but I'm not expert enough either in python or sockets to say with confidence.

Regards,
-Eric

@mhmodayman
Copy link
Author

Okay will try this fix, but not both ends of the socket are TX. one is TX, and the other are RX, but the thing is that Tx and Rx ends are on the same PC device, would that be an issue?

@ericstoneking
Copy link
Owner

No, both ends of the socket can be on the same device with no problem. You have run the TxRx example problem, right?

@mhmodayman
Copy link
Author

mhmodayman commented Mar 28, 2023

Yes I tried TxRx example, and it works

Here is the new configuration for Inp_IPC of Tx, but still the same issue with Python being the Rx side.

<<<<<<<<<<<<<<< 42: InterProcess Comm Configuration File >>>>>>>>>>>>>>>>
1                                       ! Number of Sockets
**********************************  IPC 0   *****************************
TX                                      ! IPC Mode (OFF,TX,RX,TXRX,ACS,WRITEFILE,READFILE)
0                                       ! AC.ID for ACS mode
"State00.42"                            ! File name for WRITE or READ
SERVER                                  ! Socket Role (SERVER,CLIENT,GMSEC_CLIENT)
localhost     10001                     ! Server Host Name, Port 
TRUE                                    ! Allow Blocking (i.e. wait on RX)
TRUE                                    ! Echo to stdout
1                                       ! Number of TX prefixes
"SC"                                    ! Prefix 0

And here is the python code

import socket

client_port = 10001
client_host = "127.0.0.1"
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.settimeout(10)
client_socket.bind((client_host, client_port))

read_bytes, read_address = client_socket.recvfrom(1)  # buffer size is 1024 bytes
read_byte = read_bytes

client_socket.close()

And this is the error I get

OSError: [Errno 98] Address already in use

The error mentions that address 127.0.0.1 is already in use by Tx, but I think this is the case with TxRx example that both server and client are bound to same address and port, correct?

@ericstoneking
Copy link
Owner

Hi Mahmoud,

Yes I tried TxRx example, and it works

Okay, good. I was confused by your question about both ends of the socket being on the same machine. The TxRx problem demonstrates that.

I can't help you troubleshoot your python code. What I can suggest is that you start with TxRx, then replace the Rx end with your code. Configure your end as much like Rx as you can. See 42ipc.c, Kit/Source/iokit.c, and Source/IPC/*.c for details. Once you get that interface working, the rest is easy ;-)

Happy coding,
-Eric (he/him)

@mhmodayman
Copy link
Author

Okay sure.

Meanwhile, could you please confirm the following configuration for the TxRx example?

Tx:

  • it is the socket server
  • its host is 127.0.0.1
  • its port is 10001

Rx:

  • it is the socket client
  • its host is 127.0.0.1
  • its port is 10001

Correct?

@mhmodayman
Copy link
Author

It is working now

I had to replace this line: client_socket.bind((client_host, client_port))
with this: client_socket.connect((client_host, client_port))

import socket

client_port = 10001
client_host = "127.0.0.1"
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM , 0)  # AF_INET: IPv4, SOCK_DGRAM: UDP, SOCK_STREAM: TCP
client_socket.setblocking(False)
client_socket.settimeout(10)
client_socket.connect((client_host, client_port))

try:
    read_bytes = client_socket.recv(1)
except socket.timeout:
    print('WARNING Timedout on receiving bytes from ethernet')
except OSError:
    print('Server not connected yet')

client_socket.close()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants