How can I send data from Matlab UDP client to a python server on to different PCs?

조회 수: 3 (최근 30일)
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
Izzat Adnan
Izzat Adnan 2020년 2월 2일
Its my mistake. I put the wrong ip adress of the server PC. Its working now. Thanks.
Alvaro Torrejón Padilla
Alvaro Torrejón Padilla 2022년 7월 8일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by