2012-07-30 89 views
0

我有套接字編程的問題,並且花費很多時間來查找錯誤,但是沒有找到。請有人幫助我。爲什麼在python中阻塞模式不會阻塞recv函數

Server代碼:

import socket 
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
server_socket.bind(("localhost",5001) 
server_socket.listen(10) 
client_socket, address = server_socket.accept() 
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
server_socket.bind(("localhost",5001)) 
server_socket.listen(10) 
client_socket, address = server_socket.accept() 
while True: 
    temp = client_socket.recv(100) 
    print (len(temp)) 
    print ("Recv isn't blocked") 
client_socket.close() 

客戶端代碼:

import socket 
import io 

client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 
client_socket.connect(("localhost",5001)) 
print("send data finised") 

,當我運行程序,但客戶端不發送任何內容的服務器,但的recv()在服務器仍然運行的功能,我不明白爲什麼,請幫助我。

回答

0

因爲你的客戶端程序退出了,socket是關閉的嗎?

+0

非常感謝你!我解決了它:)。這裏發生錯誤是因爲我的客戶端在服務器接收數據之前退出。 – nguyenngoc101 2012-07-30 09:25:49