TCP server

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
andrea182
Posts: 0
Joined: Sun May 19, 2013 9:49 pm

TCP server

Post by andrea182 » Sun May 19, 2013 9:57 pm

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

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Mon May 20, 2013 6:01 pm

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.

andrea182
Posts: 0
Joined: Sun May 19, 2013 9:49 pm

Post by andrea182 » Tue May 21, 2013 7:59 pm

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

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Tue May 21, 2013 10:42 pm

I'm sitting, waiting, wishing, building Blender in superstition...

andrea182
Posts: 0
Joined: Sun May 19, 2013 9:49 pm

Post by andrea182 » Wed May 22, 2013 12:37 am

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

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Wed May 22, 2013 1:00 am

game engine is whole different story, maybe look at blenderartists.org for examples how to thread a server in BGE
I'm sitting, waiting, wishing, building Blender in superstition...

andrea182
Posts: 0
Joined: Sun May 19, 2013 9:49 pm

Post by andrea182 » Wed May 22, 2013 1:33 am

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

Post Reply