这是an earlier question的延续。我正在开始一个新线程,以便我可以显示最新的完整服务器和客户端代码,包括我在该线程和Reddit上收到的所有建议。
我在Windows 7专业版下运行。
我有一个用Python编写的非常简单的TCP服务器。它创建一个socketserver对象并等待消息。当一个人到达时,服务器将其打印到其控制台并通过同一端口发送一个简单的确认。
客户端向服务器发送编号的消息,等待确认并显示它。然后它询问用户是否应该发送另一条消息。
第一条消息已成功发送和确认。在客户端,似乎第二条消息发送成功;对网络流的Write()方法的调用成功。但是,当调用Read()消息来获取确认时,会抛出异常:“已建立的连接已被主机中的软件中止。”
这是服务器代码:
import json
import threading
import socketserver
import time
with open('CAPS_TWMS_config.json', 'rt') as c:
caps_config = json.load(c)
# We are listening on this port and all defined IP addresses
# listenPort = 5001
listenPort = caps_config["listen_port"]
# Were to send the information to.
clientIPAddress = '127.0.0.1' # socket.gethostbyname('client')
# clientPort = 12345
clientPort = caps_config["send_port"]
dsnName = caps_config["dsn_name"]
# Message sequence number
sequence_num = 1
exit_app = False
class ListenSocketHandler(socketserver.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def __init__(self, request, client_address, this_server):
socketserver.BaseRequestHandler.__init__(self, request, client_address, this_server)
self.timeout = 10
def handle(self):
try:
data = self.request.recv(1024).decode()
# print (str.format("dataString[21]: {0}; dataString[24:26]: {1}", data[21], data[24:26]))
print ('ListenSocketHandler recv()-> "%s"' % data)
print ('ListenSocketHandler recv().length-> "%d"' % len(data))
if len(data) > 0:
self.request.send("I got a message!".encode())
return
except Exception as value:
print('ListenSocketHandler - %s' % str(value))
return
class ListenServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
"""
The multi-threaded server that will spawn threads running the Socket Handler for each
connection
"""
pass
if __name__ == '__main__':
try:
# Create the Server Handler for connections to this computer listening on all IP addresses,
# change '' to 'x.x.x.x' to listen on a specific IP network. This class will listen for messages # from CAPS.
server = ListenServer(('', listenPort), ListenSocketHandler)
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.setDaemon(True)
server_thread.start()
while not exit_app:
time.sleep(1)
print ('Out of main loop.')
server.shutdown()
server.server_close()
except Exception as value:
print("Failed to do something: %s", str(value))
以下是客户端代码:
private void button1_Click(object sender, EventArgs e)
{
TcpClient client = new TcpClient();
try
{
client.Connect("127.0.0.1", 5001);
NetworkStream stream = client.GetStream();
int messageCount = 1;
while (true)
{
// Translate the passed message into ASCII and store it as a Byte array.
string message = string.Format("This is message {0}", messageCount++);
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[1024];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 receivedCount = 0;
int sleepCount = 0;
while (receivedCount == 0)
{
receivedCount = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, receivedCount);
if (receivedCount == 0)
{
Thread.Sleep(250);
if (sleepCount++ > 20)
{
MessageBox.Show("Response timeout.");
break;
}
}
}
if (MessageBox.Show("Reply: " + responseData + " Try again?", "Try again?", MessageBoxButtons.YesNo) == DialogResult.No)
{
break;
}
}
client.Close();
}
catch (Exception ex)
{
MessageBox.Show("Failed to do something with TcpClient: " + ex.Message);
}
}
答案 0 :(得分:0)
在您的代码中,每次连接新客户端时,它都会读取数据并向客户确认并且处理程序在以下语句后立即关闭(即返回)
self.request.send("I got a message!".encode())
return
因此连接已关闭。来自客户端的进一步命令未发送。您可以在循环中等待接收和确认。