How can I send data from Matlab UDP client to a python server on to different PCs?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to send data from one PC running a Matlab udp client to another PC(running ubuntu) which has a python udp server. But nothing happens. I've tried disabling the firewall and looking through the forums but havent found a solution yet. Hope someone can asisst me with this issue. Its a crucial part of my capstone project. Thanks in advance!
This is the code for the Matlab udp client:
ipA = '155.69.124.203', portA = 5005; % PC running Matlab
ipB = '10.27.109.80', portB = 5006; % PC running python server
udpA = udp(ipB,portB,'LocalPort',portA);
fopen(udpA);
tstart = datevec(now);
while etime(datevec(now),tstart)<30
fprintf(udpA, 'Great')
pause(3)
end
fclose(udpA);
clear ipb portB ipA portA udpA
This is the code for the python server:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Bind the socket to the port
server_address = ('localhost', 5006)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)
while True:
print('\nwaiting to receive message')
data, address = sock.recvfrom(4096)
print('received {} bytes from {}'.format(
len(data), address))
print(data)
4 comentarios
Alvaro Torrejón Padilla
el 8 de Jul. de 2022
Hi Izzat,
I want to connect matlab with python, sending data via ip network. I have my python script which acts as a server using socket. I'm trying to use your code so i can use my matlab script as a client and send string data to python. But nothing works.
PYTHON SERVER SCRIPT
# Echo server program
import socket
import threading
HEADER= 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
print(SERVER)
ADDR = (SERVER,PORT)
FORMAT ='utf-8'
DISCONNECT_MESSAGE="!DISCONNECT"
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client (conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected =True
while connected:
msg_length= conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected=False
print(f"[{addr}] {msg}")
conn.close()
def start():
server.listen()
print(f"[LISTENING] Server is listenning on {SERVER}")
while True:
addr, conn = server.accept()
thread= threading.Thread(target=handle_client,args=(conn,addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() -1}")
print("[STARTING] server is starting...")
start()
MATLAB CLIENT SCRIPT
ipA = 'IP MATLAB CLIENT', portA = 5051; % PC running Matlab
ipB = 'IP PYTHON SERVER', portB = 5050; % PC running python server
udpA = udp(ipB,portB,'LocalPort',portA);
fopen(udpA);
tstart = datevec(now);
while etime(datevec(now),tstart)<30
fprintf(udpA, 'Great')
pause(3)
end
fclose(udpA);
clear ipb portB ipA portA udpA
%%%%
I would be very grateful if you could help me with that.
Respuestas (0)
Ver también
Categorías
Más información sobre Instrument Control Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!