[不是问题] TCPListener套接字/客户端在单个连接上读取多条消息! [英] [Not a question] TCPListener socket/client reading multiple messages on single connection!

查看:108
本文介绍了[不是问题] TCPListener套接字/客户端在单个连接上读取多条消息!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,必须在套接字上建立到设置IP和端口的连接。在此套接字连接的生命周期中,我需要侦听特定端口并在同一端口收到消息时进行响应。



应用程序是其中的一部分一个复杂的多线程环境,以及其他线程在此过程中运行以启用用户输入。



我的应用程序在端口X上进行初始连接,并发送一条消息。

在端口Y上启动'listener'线程。

主执行线程很愉快。

一旦应用程序收到端口响应X,过程结束了。



端口Y需要保持打开状态并在端口X的生命周期内接收消息并发送响应,但是我尝试线程化客户端在端口Y上,一旦发送消息,它就会自动关闭。



我一直在看这几天,我看不到解决方案!



监听器是在一个单独的线程上启动的。一旦在端口Y上建立连接,我创建一个TCPClient来处理异步调用,然后在异步方法中我在网络流上工作(读取和写回)。此时我认为我需要netstream保持打开状态,但调用线程打开一个新流并关闭现有流!第三方提供商告诉我,我的应用程序正在关闭端口,我无法看到如何保持监听器线程网络流打开!



任何逻辑帮助赞赏,或者我将在圣诞节工作!!



问候



马特

I'm writing an application that has to make a connection on a socket to a set IP and port. During the lifetime of this socket connection, I'm required to listen on a specific port and respond when a message is received on the same port.

The application is part of a complex multi threaded environment, and other threads are running during this process to enable user input.

My Application makes the initial connection on port X, and sends a message.
Starts the 'listener' thread on port Y.
Main execution thread carries on happily.
Once the Application receives a response on port X, the process is over.

Port Y needs to stay open and receive messages and send responses during the life time of port X, however I try and thread the client on port Y it closes itself once a message has been sent.

I've been looking at this for a couple of days, and I can't see a solution!

The listener is started on a seperate thread. Once a connection is made on Port Y, I create a TCPClient to handle the asynchronous call, then in the async method I do my work on a net stream (read and write back). At this point I think I need the netstream to stay open, but the calling thread opens a new stream and closes the existing one! The third party providers have told me the port is being closed by my application, and I can't see how to keep the listener thread netstream open!

Any logical help appreciated, or I'm going to be working over Christmas!!

regards

Matt

推荐答案

在没有看到实际代码的情况下,很难提供帮助。

但是,本文将帮助多线程客户端/服务器套接字类 [ ^ ]?
Without seeing the actual code it's quite difficult to assist.
However will this article help Multi-threaded Client/Server Socket Class[^]?


这种听起来像你可能在不同的端口上意外使用相同的套接字。检查以确保每个人都有自己的套接字,并且它们不会以某种方式使用相同的套接字标识符。另外,请确保您的听力线程没有因为某种原因提前终止。



正如Chill60在解决方案1中所述,如果没有提供更明确的指导,很难任何代码都可以。



编辑:忘了提一件事,在Windows实现套接字(winsock库)中,如果套接字被删除/断开,端口将关闭。确保您没有意外删除/断开插座。
This sort of sounds like you may be accidentally using the same socket on different ports. Check to make sure each one has his own socket and they're not somehow using the same socket identifier. Also, make sure that your listening thread isn't terminating early for one reason or another.

As Chill60 stated in solution 1, it's hard to give more definitive guidance without any code at all.

Forgot to mention one thing, in the Windows implementation of sockets (winsock library), if a socket gets deleted/disconnected, the port will close. Make sure you're not deleting/disconnecting the socket by accident.


这篇关于[不是问题] TCPListener套接字/客户端在单个连接上读取多条消息!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆