I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs:
--------------
from socket import *
host = ''
port = 51567
address = (host, port)
buffer_size = 1024
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.b ind(address)
server_socket.l isten(5)
while True:
print 'waiting for connection...'
client_socket, client_address = server_socket.a ccept()
print '...connected from:', client_address
while True:
data = client_socket.r ecv(buffer_size )
if not data:
break
client_socket.s end('%s %s' % ('You typed:', data))
client_socket.c lose()
server_socket.c lose()
------------
from socket import *
host = 'localhost'
port = 51567
address = (host, port)
buffer_size = 1024
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.c onnect(address)
while True:
data = raw_input('')
if not data:
break
client_socket.s end(data)
data = client_socket.r ecv(buffer_size )
if not data:
break
print data
client_socket.c lose()
---------------
I tried changing buffer_size to 10 and I got this output:
john@john-laptop:~$ python myclient.py
You typed:
hello
You typed:
something
john@john-laptop:~$
My first question is, isn't buffer_size the number of bytes being sent at one time? If so, why doesn't 'hello' get printed after the server returns the data to the client? Isn't 'hello' just 5 bytes?
Secondly, how is it working that once I type in a new string (e.g. 'something') and then the server returns data to the client, it prints the *previous* string, (i.e. 'hello')? Wouldn't the data variable get overwritten with the value, or is the value being stored somewhere else at this point?
Thanks!
--------------
from socket import *
host = ''
port = 51567
address = (host, port)
buffer_size = 1024
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.b ind(address)
server_socket.l isten(5)
while True:
print 'waiting for connection...'
client_socket, client_address = server_socket.a ccept()
print '...connected from:', client_address
while True:
data = client_socket.r ecv(buffer_size )
if not data:
break
client_socket.s end('%s %s' % ('You typed:', data))
client_socket.c lose()
server_socket.c lose()
------------
from socket import *
host = 'localhost'
port = 51567
address = (host, port)
buffer_size = 1024
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.c onnect(address)
while True:
data = raw_input('')
if not data:
break
client_socket.s end(data)
data = client_socket.r ecv(buffer_size )
if not data:
break
print data
client_socket.c lose()
---------------
I tried changing buffer_size to 10 and I got this output:
john@john-laptop:~$ python myclient.py
hello
something
this is a long string
why doesn't this work right
>
My first question is, isn't buffer_size the number of bytes being sent at one time? If so, why doesn't 'hello' get printed after the server returns the data to the client? Isn't 'hello' just 5 bytes?
Secondly, how is it working that once I type in a new string (e.g. 'something') and then the server returns data to the client, it prints the *previous* string, (i.e. 'hello')? Wouldn't the data variable get overwritten with the value, or is the value being stored somewhere else at this point?
Thanks!
Comment