Yes I have made a client server before in blender 2.49b. check my previous thread.
I will also include the server if anyone wants to try them. don’t forget to look for the code and change the IP address to whatever is yours.
from socket import *
from pickle import *
import select
sock = socket(AF_INET, SOCK_STREAM)
sock.bind(("192.168.1.3", 1993))
sock.setblocking(0)
sock.listen(5)
ID = 0
updatelist = []
newusers = []
users = []
chat = []
clients = []
deadclients = []
while True:
try:
client, addr = sock.accept()
print("connection from...", addr)
read, write, error = select.select([client],[],[],1)
if(len(read) > 0):
for x in read:
try:
username = x.recv(1024)
username = loads(username)
user = [ID, username, client, addr, 0.1,0.1]
users.append(user)
updatelist.append(user)
newusers.append(user)
clients.append(client)
ID = ID + 1
except:
client.close()
except:
pass
if(len(clients) > 0):
read, write, error = select.select(clients, [],clients,1)
if len(error) > 0:
for x in error:
x.close()
for y in users:
if x == y[2]:
users.remove(y)
if (len(read) > 0):
for x in read:
try:
data = x.recv(1024)
if(not data):
for y in users:
if y[2] == x:
x.close()
users.remove(y)
clients.remove(x)
data = [-1,y[0]]
data = dumps(data)
for u in users:
try:
u[2].send(data)
except:
print("couldn't send killed connections")
print("removed client")
break
data = loads(data)
if(data[0] == 1):
c = [data[1],data[2],data[3]]
chat.append(c)
if(data[0] == 2):
if(data[3] == 1):
for it in users:
if x == it[2]:
it[5] = it[5] + 0.25
updatelist.append(it)
break
elif(data[3] == 2):
for it in users:
if x == it[2]:
it[4] = it[4] - 0.25
updatelist.append(it)
break
elif(data[3] == 3):
for it in users:
if x == it[2]:
it[5] = it[5] - 0.25
updatelist.append(it)
break
elif(data[3] == 4):
for it in users:
if x == it[2]:
it[4] = it[4] + 0.25
updatelist.append(it)
break
except:
pass
if (len(clients) > 0):
read, write, error = select.select([],clients,clients,1)
if len(error) > 0:
for x in error:
x.close()
for y in users:
if x == y[2]:
users.remove(y)
if(len(write)>0):
for x in clients:
for iter in newusers:
if x == iter[2]:
try:
x.send(dumps(str(iter[0])))
for u in users:
d = [2,u[0],u[1],u[4],u[5]]
d = dumps(d)
iter[2].send(d)
print("data send")
newusers.remove(iter)
#break
except:
x.close()
for y in users:
if x == y[2]:
users.remove(y)
break
clients.remove(x)
print("closed connection because ID not send")
for iter in updatelist:
update = [2,iter[0],iter[1],iter[4],iter[5]]
try:
x.send(dumps(update))
except:
x.close()
for y in users:
if x == y[2]:
users.remove(y)
break
clients.remove(x)
print("closed connection because failed to update")
for iter in chat:
mess = [1,iter[0],iter[1],iter[2]]
try:
x.send(dumps(mess))
except:
x.close()
for y in users:
if x == y[2]:
users.remove(y)
break
clients.remove(x)
print("closed connection because faailed to chat")
updatelist = []
chat = []
# if len(users) > 0:
# for x in users:
# print(x)
sock.close()
I know that I could improve the server to be faster and also the client but I did this as a short project.