C# 使用流读取器接收
任何人都可以给我一些关于如何创建仅接收消息的小程序的提示或指南吗?
我可以编写一个可以接收的代码,但前提是我正在发送消息(并且我期待回复)
但是程序只是在等待怎么样?
我收到此代码:
client.Connect("192.168.1.100",80);
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
String r = "";
sw.AutoFlush = true;
while(true)
{
r = sr.ReadLine();
Console.WriteLine(r);
Debug.WriteLine(r);
if (sr.Peek() < 0) break;
}
这只能像我说的那样起作用。如果我先发送消息,然后就会回复。
Can anyone please give me a tip or guide on how to create a mini program that only receive messages?
I can do a code that can receive but only if I'm sending a message (and I'm expecting a reply)
But how about the program is just waiting?
I got this code on receiving:
client.Connect("192.168.1.100",80);
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
String r = "";
sw.AutoFlush = true;
while(true)
{
r = sr.ReadLine();
Console.WriteLine(r);
Debug.WriteLine(r);
if (sr.Peek() < 0) break;
}
This only work like I said. If I'm sending a message first, then there's a reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要了解如何将 TcpListener 对象与 TcpClient 结合使用。 此链接有一个很好的示例:
You'll want to take a look at using a TcpListener object in conjunction with the TcpClient. This link has a good example:
您可以使用 TcpListener 类中实现的服务器套接字,但是当您的计算机位于本地网络中时,使用此连接会出现一些问题。
我认为你应该写3个程序。第一个服务器将消息发送到服务器,第二个服务器将放置在互联网上始终可用的位置 - 该服务器将负责将消息排队,第三个服务器将向服务器询问问题。
You can use Server Socket that is implemented in TcpListener class, but there are some problem with connecting with this when your computer is in local network.
I think that you should write 3 programms. First which sends messages to serwer, second server which would be placed in always-avaible place in internet -this one would be responsible for enqueue messages, and third one which would ask server for questions.