通过套接字接收数据时的缓冲区大小? [英] Buffer size when receiving data through a socket?
问题描述
我再次编写了一些非常基本的套接字编程,但我仍然对使用buffer_size变量发生的事情感到困惑。以下是服务器和客户端程序:
--------------
来自套接字导入的
*
host =''''
port = 51567
地址=(主机,端口)
buffer_size = 1024
server_socket = socket(AF_INET,SOCK_STREAM)
server_socket.bind(地址)
server_socket.listen (5)
而True:
print''等待连接...''
client_socket,client_address = server_socket .accept()
打印''...连接自:'',client_address
而True:
data = client_socket.recv(buffer_size)
如果不是数据:
break
client_socket.send(''%s%s''%(' '你键入:'',数据))
client_socket.close()
server_socket.close()
------------
来自套接字导入的
*
host ='' localhost''
port = 51567
地址=(主机,端口)
buffer_size = 1024
client_socket = socket(AF_INET,SOCK_STREAM)
client_socket.connect(地址)
而True:
data = raw_input('''')
如果不是数据:
break
client_socket.send(数据)
data = client_socket.recv(buffer_size)
如果不是数据:
休息
打印数据
client_socket.close()
---------------
我尝试将buffer_size更改为10并获得此输出:
john @ john-laptop:〜$ python myclient.py
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.bind(address)
server_socket.listen(5)
while True:
print ''waiting for connection...''
client_socket, client_address = server_socket.accept()
print ''...connected from:'', client_address
while True:
data = client_socket.recv(buffer_size)
if not data:
break
client_socket.send(''%s %s'' % (''You typed:'', data))
client_socket.close()
server_socket.close()
------------
from socket import *
host = ''localhost''
port = 51567
address = (host, port)
buffer_size = 1024
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(address)
while True:
data = raw_input('''')
if not data:
break
client_socket.send(data)
data = client_socket.recv(buffer_size)
if not data:
break
print data
client_socket.close()
---------------
I tried changing buffer_size to 10 and I got this output:
john@john-laptop:~$ python myclient.py
hello
hello
您键入:
You typed:
something
something
hello
hello
这是一个长字符串
this is a long string
您键入:
You typed:
为什么这不合适
why doesn''t this work right
something
something
>
>
john @ john-laptop:〜$
我的第一个问题是,不是buffer_size的字节数被送到一次?如果是这样,在服务器将数据返回给客户端之后为什么不打印?不是'你好''只需要5个字节吗?
其次,一旦我输入一个新的字符串(例如''something''),然后是服务器将数据返回给客户端,它打印* previous * string,(即''hello'')?数据变量是否会被值覆盖,或者此时值是否存储在其他地方?
谢谢!
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!
推荐答案
python myclient.py
python myclient.py
hello
hello
您输入:
You typed:
something
something
hello
hello
这是一个长字符串
this is a long string
您键入:
You typed:
为什么这不正常
why doesn''t this work right
something
something
>
>
john @ john-laptop:〜
john@john-laptop:~
我的第一个问题是,是不是buffer_size一次发送的字节数?如果是这样,在服务器将数据返回给客户端之后为什么不打印?不是'你好''只需要5个字节吗?
其次,一旦我输入一个新的字符串(例如''something''),然后是服务器将数据返回给客户端,它打印* previous * string,(即''hello'')?数据变量是否会被值覆盖,或者此时值是否存储在其他位置?
谢谢!
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!
>
En Mon,2008年6月16日21:21:35 -0300,John Salerno< jo ****** @NOSPAMgmail.comescribió:
En Mon, 16 Jun 2008 21:21:35 -0300, John Salerno <jo******@NOSPAMgmail.comescribió:
我再次编写了一些非常基本的套接字编程,但我仍然对使用buffer_size变量发生的事情感到困惑。以下是服务器和客户端程序:
--------------
来自套接字导入的
*
host =''''
port = 51567
地址=(主机,端口)
buffer_size = 1024
server_socket = socket(AF_INET,SOCK_STREAM)
server_socket.bind(地址)
server_socket.listen (5)
而True:
print''等待连接...''
client_socket,client_address = server_socket .accept()
打印''...连接自:'',client_address
而True:
data = client_socket.recv(buffer_size)
如果不是数据:
break
client_socket.send(''%s%s''%(' '你键入:'',数据))
client_socket.close()
server_socket.close()
------------
来自套接字导入的
*
>
host =''localhost''
port = 51567
地址=(主机,端口)
buffer_size = 1024
client_socket = socket(AF_INET,SOCK_STREAM)
client_socket.connect(地址)
而True:
data = raw_input('''')
如果不是数据:
break
client_socket.send(data)
data = client_socket.recv(buffer_size)
如果没有数据:
break
打印数据>
client_socket.close()
---------------
我尝试将buffer_size更改为10并获得此输出:
john @ john-laptop:〜
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.bind(address)
server_socket.listen(5)
while True:
print ''waiting for connection...''
client_socket, client_address = server_socket.accept()
print ''...connected from:'', client_address
while True:
data = client_socket.recv(buffer_size)
if not data:
break
client_socket.send(''%s %s'' % (''You typed:'', data))
client_socket.close()
server_socket.close()
------------
from socket import *
host = ''localhost''
port = 51567
address = (host, port)
buffer_size = 1024
client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect(address)
while True:
data = raw_input('''')
if not data:
break
client_socket.send(data)
data = client_socket.recv(buffer_size)
if not data:
break
print data
client_socket.close()
---------------
I tried changing buffer_size to 10 and I got this output:
john@john-laptop:~
这篇关于通过套接字接收数据时的缓冲区大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!