python3.7中的Socket recv()错误

python3.7中的Socket recv()错误,python,sockets,send,recv,Python,Sockets,Send,Recv,客户: #程序应发送两个字符串,服务器应返回类间字符串 导入套接字 导入时间 导入系统 HEADERSIZE=10 firstString=输入(“第一个字符串:”) 第二个字符串=输入(“第二个字符串:”) host=“127.0.0.1” 端口=8888 firstString=f'{len(firstString):您的服务器在conn.recv(16)处阻塞,等待更多输入。我认为,您可以在最后一条消息通过s.shutdown(socket.shutdown\u WR)发送后,显式关闭客户

客户:

#程序应发送两个字符串,服务器应返回类间字符串
导入套接字
导入时间
导入系统
HEADERSIZE=10
firstString=输入(“第一个字符串:”)
第二个字符串=输入(“第二个字符串:”)
host=“127.0.0.1”
端口=8888

firstString=f'{len(firstString):您的服务器在
conn.recv(16)
处阻塞,等待更多输入。我认为,您可以在最后一条消息通过
s.shutdown(socket.shutdown\u WR)发送后,显式关闭客户端套接字的写端,从而使此示例起作用
,但对于一个稍微复杂的示例,您需要在TCP之上实现自己的协议。不,这里演示的
recv()
中没有错误。服务器不会被阻止。客户端会被阻止。正如我所说,在我尝试从服务器发送任何内容之前,所有工作都正常。位于客户端msg.recv()是的,我可能表达错了。谢谢,无论如何!!=)客户端在接收时阻塞,因为服务器没有发送任何数据。服务器没有发送任何数据,因为它在我说的位置阻塞。以及为什么服务器在我放置recv()之前不在conn.recv()阻塞在客户端中?这是我需要了解的。为什么您认为服务器会退出while循环?什么情况会导致
conn.recv(16)
返回零长度
msg
#The program should send two strings and the server should return the interclassed string back

import socket
import time
import sys


HEADERSIZE=10
firstString = input("First string: ")
secondString = input("Second string: ")
host = "127.0.0.1"
port = 8888
firstString = f'{len(firstString):<{HEADERSIZE}}'+firstString
secondString = f'{len(secondString):<{HEADERSIZE}}'+secondString

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(bytes(firstString, 'utf-8'))
s.send(bytes(secondString, 'utf-8'))
fullMsg=''
while 1:
    msg = s.recv(8)
    if len(msg)<=0:
        break
    fullMsg += msg.decode('utf-8')
print(fullMsg)
s.close()
#trebuie trimisa si dimensiunea
import socket


def interclass(firstString , secondString):
    i =0 
    j =0
    interString=''
    while i < len(firstString) and j < len(secondString):
        if firstString[i] < secondString[j]:
            interString+=firstString[i]
            i=i+1
        else: 
            interString+=secondString[j]
            j=j+1
    while i < len(firstString):
        interString += firstString[i]
        i=i+1
    while j < len(secondString):
        interString+=secondString[j]
        j=j+1
    return interString

host = "127.0.0.1"
port = 8888;
HEADERSIZE=10

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
while True:
    conn, addr = s.accept()
    messages=[]
    newMsg = True
    fullMsg = ''
    msgLength = -1
    while True:
        msg = conn.recv(16)
        if len(msg)<=0:
            break
        msg = msg.decode('utf-8')
        fullMsg += msg
        if len(fullMsg)>=HEADERSIZE and newMsg == True:
            newMsg = False
            msgLength = int(fullMsg[:HEADERSIZE-1])
            fullMsg = fullMsg[HEADERSIZE:HEADERSIZE+1+msgLength]
        if not newMsg and len(fullMsg)>=msgLength:
            messages.append(fullMsg[:msgLength])
            fullMsg = fullMsg[msgLength:]
            newMsg = True
    interString = interclass(messages[0], messages[1])
    conn.sendall(bytes(interString,'utf-8'))
conn.close()