MaryKathryn / monkeyLogic

Original ML code + TCP functions & UDK functionality
1 stars 0 forks source link

Questions on multi-threading in Matlab #3

Open Qifan94 opened 6 years ago

Qifan94 commented 6 years ago

Hi Mary,

I am using MonkeyLogic to do the experiment, but it cannot use TCP/IP during the task animation (no multithreading in Matlab). So I wonder does this code work to set up a Server by using python in Matlab to communicate with other external devices during the experiment trails?

I tried to write a demo to try that:

`from socket import * import threading def Server(): fo = open("foo.txt", "w") nn = 0 address='10.10.44.107'
port=31500
buffsize=1024
s = socket(AF_INET, SOCK_STREAM) s.bind((address,port)) s.listen(1)
print('Ready to get in') while True: nn += 1 fo.write(str(nn)+"\n") fo.flush() print('wait for connect') clientsock,clientaddress=s.accept() print('connect from:',clientaddress)

while True:

    recvdata=clientsock.recv(buffsize).decode('utf-8')
    fo.write(str(recvdata)+"\n")
    fo.write(str(not recvdata)+"\n")
    fo.flush()
    print(recvdata)
    if recvdata=='exit': 
        break
    clientsock.close()
s.close()`

and use this line in Matlab: py.server_test.Server() It can start a TCP server in MATLAB and connect with the client, but it is still blocked there. When I close the connection, all things are printed finally. It is quite weird. So I wonder, does the TCP server really work with Matlab? And does it still block the MonkeyLogic thread? Thanks!

Qifan94 commented 6 years ago

Sorry, the python Server demo is attached here

from socket import *
import threading
def Server():

    fo = open("foo.txt", "w")
    nn = 0

    address='10.10.44.107'     
    port=31500             
    buffsize=1024          
    s = socket(AF_INET, SOCK_STREAM)
    s.bind((address,port))
    s.listen(1)    
    print('Ready to get in')
    while True:
        nn += 1
        fo.write(str(nn)+"\n")
        fo.flush()
        print('wait for connect')
        clientsock,clientaddress=s.accept()
        print('connect from:',clientaddress)
        #while True:  
        recvdata=clientsock.recv(buffsize).decode('utf-8')
        fo.write(str(recvdata)+"\n")
        fo.write(str(not recvdata)+"\n")
        fo.flush()
        print(recvdata)
        if recvdata=='exit': 
            break
        clientsock.close()
    s.close()