I have been working with python sockets for a while and I have written some simple simple programs.
The problem that I encounter every time concerns sending and receiving methods on sockets! Providing you with a simple and simple example:
This is the receiver (server!):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 4001))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.listen(5)
while True:
conn, addr = s.accept()
print conn, addr
data1 = conn.recv(64)
data2 = conn.recv(64)
print 'uname is %s , password is: %s' %(data1, data2, )
conn.close()
And this is the sender (or client!):
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 4001))
uname = raw_input('enter username>')
passw = raw_input('enter password>')
s.send(uname)
s.send(passw)
print 'exiting ...'
s.close()
So, the problem is why the server receives both uname and passw in the first s.recv () method? This means that data2 is always empty!
I really don't know what happens when the client uses s.send (). I thought that each s.send () sends a "packet" to the destination (ip, port) !!
And can someone explain to me why this second code (another client) works?
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 4001))
uname = raw_input('enter username>')
s.send(uname)
passw = raw_input('enter password>')
s.send(passw)
print 'exiting ...'
s.close()