Len tak skúšám sockety a spravil som niečo takéto:
import socket
HOST = '127.0.0.1' #raw_input('Nzov hostu: ')
PORT = 50000 #raw_input('port: ')
cs = raw_input('client server(c-s)? ')
print HOST
print PORT
print cs
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if cs == 's':
s.bind((HOST, PORT))
cislo = '0'
koniec = 0
while koniec != 1:
if cs == 's':
s.listen(5)
print 'server vytvoreny a caka na pripojenie'
while 1:
conn, addr = s.accept()
print 'pripojeny klient: ', addr
while 1:
data = conn.recv(1024)
if not data:
break
elif data == 'koniec':
koniec = 1
else:
data = int(data)
data += 0.01
data = str(data)
print data
s.send(''.join(data))
conn.close()
if cs == 'c':
s.connect((HOST, PORT))
print 'posielam data'
if int(cislo) < 1.0:
data = cislo
s.send(data)
else:
s.send('koniec')
koniec = 1
data = s.recv(1024)
print data
cislo = data
s.close()
A client ma problem s pripojenim:
client server(c-s)? c
127.0.0.1
50000
c
posielam data
Traceback (most recent call last):
File "socket.c", line 41, in ?
s.connect((HOST, PORT))
File "<string>", line 1, in connect
File "/usr/lib/python2.4/socket.py", line 136, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: (9, 'Bad file descriptor')
Prosím, neviete čo to hlása?
client a server spúšťam na tom istom kompe.