Page 1 of 1

TCP server

Posted: Sun May 19, 2013 9:57 pm
by andrea182
Hi to everyone,

i've a python script that create a TCP server and when i use that script on python without blender it works fine,but when i try to use it in blender it crash...

the code is below:

Code: Select all

# a simple tcp server

import socket

HOST = '127.0.0.1'      # Symbolic name meaning the local host
PORT = 50007            # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

print ('Connected by', addr)

while 1:
        data = conn.recv(1024)
        print('dati ricevuti: ',data)
        if not data:
                break
        conn.send(data)

conn.close()
i think it crash at this line

Code: Select all

conn, addr = s.accept()
but i don't understand why....
can someone help me to fix it or can give me a script that work in blender that create a TCP server so i can comunicate with a c++ program?

Thanks a lot,
Andrea182

Posted: Mon May 20, 2013 6:01 pm
by stiv
By 'crash', I'm guessing you mean throws an exception. Seeing the exact message will help solve the problem. Try running blender from the command line if necessary.

Posted: Tue May 21, 2013 7:59 pm
by andrea182
i've fix some error but i don't understand why i press "run script" it works fine but when i press "P" button on the game engine it crash...
the code is below and the projects is at the url at the end on the page

Code: Select all

from threading import Thread
import socket 
import time
import sys
import bpy

def myfunction(mystring,*args):
    bpy.data.objects['Text.001'].data.body = 'Inizio thread'
    print (mystring)
    HOST = '127.0.0.1'      # Symbolic name meaning the local host  
    PORT = 50007            # Arbitrary non-privileged port 
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    s.bind((HOST, PORT)) 
    s.listen(1) 
    conn, addr = s.accept() 
   
    print ('Connected by', addr) 

    while 1: 
        data = conn.recv(1024) 
        sys.stdout.write("Dati ricevuti: %s%%   \r" % (data.decode('UTF-8')) )
        azione = data.decode('UTF-8')
        if azione == '0':
            bpy.data.objects['Text.001'].data.body = 'Azione Neutral'
            
        elif azione == '1':
            bpy.data.objects['Text.001'].data.body = 'Azione Gamba Destra' 
            
        elif azione == '2':
            bpy.data.objects['Text.001'].data.body = 'Azione Gamba Sinistra'
           
        else:
            bpy.data.objects['Text.001'].data.body = 'Azione ricevuta non valida'
            
        
        if not data: 
                break 
        conn.send(data) 
        time.sleep(1.0)
        
    conn.close() 


if __name__ == '__main__':

    try:
        bpy.data.objects['Text.001'].data.body = 'TRY'
        t = Thread(None,myfunction,None,('MyStringHere',1))
        t.start()
                   
 
    except Exception as errtxt:
        print (errtxt)
the link of the file is this

https://mega.co.nz/#!Fpo0FTwQ!RbP_-xno- ... 6Wjyg4SH8

Posted: Tue May 21, 2013 10:42 pm
by CoDEmanX

Posted: Wed May 22, 2013 12:37 am
by andrea182
first thanks a lot for the answer,but i can't understand where is the error...

why it works when i use run script but it doesn't when i press "P" button...how can i fix it?

thanks a lot,
Andrea182

Posted: Wed May 22, 2013 1:00 am
by CoDEmanX
game engine is whole different story, maybe look at blenderartists.org for examples how to thread a server in BGE

Posted: Wed May 22, 2013 1:33 am
by andrea182
CoDEmanX wrote:game engine is whole different story, maybe look at blenderartists.org for examples how to thread a server in BGE
there is no way to run the server outside the game engine?i see that in earlier version of blender there was the possibility of lunch the server by "enable script links" and then associate it "onload"...now i see that bendler dosen't support it anymore...

my target is that,i want to lunch a server that can retrive the data passed from a c++ program and than move a character...(the data i pass is only 1 integer) and it works well if i don't use game engine but i must use game engine,so someone please help me?

Thanks a lot