Server and Client Socket problem

J

José Carlos

Hi.

I am starting a server socket program. I send data from client socket but
the server don´t recived anything.

This is my python code:

Server:

import socket
def run():
servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servidor.bind(('localhost', 6969))
servidor.listen(1)
while 1:
(conexion, direccion) = servidor.accept()
datos = conexion.recv(4069)
print "Servidor Recibe", datos
if datos == "quit":
servidor.close()
break

run()


Client:

import socket
def run():
cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cliente.connect(('localhost', 6969))
except:
print "No he podido Conectar con el Servidor"
return
datos = '12345678'
cliente.send(datos)
#cliente.close()

run()


I am a new python user. what can i do?

Thank you.

Regards.
José Carlos
 
D

Diez B. Roggisch

Hi,

import socket
def run():
servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servidor.bind(('localhost', 6969))
servidor.listen(1)
while 1:
(conexion, direccion) = servidor.accept()
datos = conexion.recv(4069)
print "Servidor Recibe", datos
if datos == "quit":
servidor.close()
break

run()

This code can obviously not run, as its not properly indented. I'm sure that
indentation got somehow lost when you create the posting, but as long as
you don't provide it in a executable fashion, we can't help you.

However, as someone beeing recently converted from hand-made socket
programming to twisted, I suggest instead of working with sockets yourself,
you better investigate into the twisted framework. Its very well
documented.

Regards,

Diez B. Roggisch
 
J

Josiah Carlson

This code can obviously not run, as its not properly indented. I'm sure that
indentation got somehow lost when you create the posting, but as long as
you don't provide it in a executable fashion, we can't help you.
Agreed.

However, as someone beeing recently converted from hand-made socket
programming to twisted, I suggest instead of working with sockets yourself,
you better investigate into the twisted framework. Its very well
documented.

After having written both synchronous and asynchronous servers and
clients using Python sockets, I've been very happy with the asyncore
standard library. I should probably give twisted a shot, but writing
with asyncore is so easy that I'm hard-pressed to even bother. I also
get a sick satisfaction in implementing protocols...but maybe that is
just me.

- Josiah
 
P

Pierre Quentel

Apart from indentation, you had forgotten the "try" statement in client
This should work better :

Client

---------------------
import socket

def run():
try:
cliente = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
cliente.connect(('localhost', 6969))
except:
print "No he podido Conectar con el Servidor"
return
datos = '12345678'
cliente.send(datos)
#cliente.close()

run()

---------------------

Server

---------------------
import socket

def run():
servidor = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servidor.bind(('localhost', 6969))
servidor.listen(1)
while 1:
(conexion, direccion) = servidor.accept()
datos = conexion.recv(4069)
print "Servidor Recibe", datos
if datos == "quit":
servidor.close()
break

run()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top