通过套接字进行多个顺序通信的正确程序是什么?(What is the correct procedure for multiple, sequential communications over a

编程入门 行业动态 更新时间:2024-10-18 18:22:26
通过套接字进行多个顺序通信的正确程序是什么?(What is the correct procedure for multiple, sequential communications over a socket?)

我一直在努力与插座一起努力,取得良好的进展,但我一直遇到问题,并且觉得我必须做错事情才能做到这一点。

有很多教程可以实现TCP客户端和服务器,通常在:

服务器以无限循环运行,监听并回送客户端的数据。 客户端连接到服务器,发送消息,接收相同的内容, 然后退出

我可以处理。 但是,似乎没有人详细介绍您应该和不应该使用相同的两台机器/进程之间的顺序通信。

我是在执行多个消息的函数调用的一般序列之后,但是为了提出一个真正的问题,这里有一些约束:

每个事件都是单个消息客户端 - >服务器,以及单个字符串响应。 消息很短,最多100个字符。 事件发生的速度相对较慢,例如每5秒1次,但通常不到速度的一半。

以及一些具体问题:

服务器应该在响应后关闭连接,还是尝试挂断连接直到下一次通信? 同样,客户端应该在收到响应后关闭连接,还是尝试重用连接? 闭合连接(通过close()还是通过某些错误)是指通信结束还是整个对象的生命周期结束? 我可以通过再次连接重用该对象吗? 我可以在服务器的同一端口上这样做吗? 或者我是否已通过重新调用socket.socket()重新实例化另一个套接字对象? 我应该怎么做以避免“使用中的地址”错误? 如果recv()超时,套接字是否可重用,或者我应该丢弃它? 我可以再次使用相同的套接字对象启动新连接,还是需要一个全新的套接字?

I've been struggling along with sockets, making OK progress, but I keep running into problems, and feeling like I must be doing something wrong for things to be this hard.

There are plenty of tutorials out there that implement a TCP client and server, usually where:

The server runs in an infinite loop, listening for and echoing back data to clients. The client connects to the server, sends a message, receives the same thing back, and then quits.

That I can handle. However, no one seems to go into the details of what you should and shouldn't be doing with sequential communication between the same two machines/processes.

I'm after the general sequence of function calls for doing multiple messages, but for the sake of asking a real question, here are some constraints:

Each event will be a single message client->server, and a single string response. The messages are pretty short, say 100 characters max. The events occur relatively slowly, max of say, 1 every 5 seconds, but usually less than half that speed.

and some specific questions:

Should the server be closing the connection after its response, or trying to hang on to the connection until the next communication? Likewise, should the client close the connection after it receives the response, or try to reuse the connection? Does a closed connection (either through close() or through some error) mean the end of the communication, or the end of the life of the entire object? Can I reuse the object by connecting again? Can I do so on the same port of the server? Or do I have reinstantiate another socket object with a fresh call to socket.socket()? What should I be doing to avoid getting 'address in use' errors? If a recv() times out, is the socket reusable, or should I throw it away? Again, can I start a new connection with the same socket object, or do I need a whole new socket?

最满意答案

如果您知道将很快再次在两个进程之间进行通信,则无需关闭连接。 如果您的服务器也必须处理其他连接,那么您希望将其设置为多线程。 一样。 你知道两者都必须做同样的事情,对吗? 您必须在客户端上创建一个新套接字,并且您也不能在服务器端重用套接字:您必须使用由下一个(clientsocket, address) = serversocket.accept()调用返回的新套接字。 您可以使用相同的端口。 (想想网络服务器,他们总是接受来自数千个客户端的同一端口的连接)

在这两种情况下(关闭或不关闭),您应该有一个消息终止符号,例如\n 。 然后你必须从插座读取,直到你到达标志。 这种用法很常见,python有一个构造: socket.makefile和file.readline

更新:

发布代码。 可能您没有正确关闭连接。 你可以再次调用recv()。

更新2:您永远不应该假设连接是可靠的,但包括在出现错误时重新连接的机制。 因此,即使间隙较长,也可以尝试使用相同的连接。 至于你得到的错误:如果你需要特定的代码帮助,你应该发布小(但完整)的例子。

If you know that you will communicate between the two processes soon again, there is no need for closing the connection. If your server has to deal with other connections as well, you want to make it multithreaded, though. The same. You know that both have to do the same thing, right? You have to create a new socket on the client and you can also not reuse the socket on the server side: you have to use the new socket returned by the next (clientsocket, address) = serversocket.accept() call. You can use the same port. (Think of webservers, they always accept connections to the same port, from thousands of clients)

In both cases (closing or not closing), you should however have a message termination sign, for example a \n. Then you have to read from the socket until you have reached the sign. This usage is so common, that python has a construct for that: socket.makefile and file.readline

UPDATE:

Post the code. Probably you have not closed the connection correctly. You can call recv() again.

UPDATE 2: You should never assume that the connection is reliable, but include mechanisms to reconnect in case of errors. Therefore it is ok to try to use the same connection even if there are longer gaps. As for errors you get: if you need specific help for your code, you should post small (but complete) examples.

更多推荐

本文发布于:2023-04-28 03:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   顺序   正确   通信   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!