开发者

C# Receiving using stream reader

开发者 https://www.devze.com 2023-03-02 05:18 出处:网络
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)

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.


You'll want to take a look at using a TcpListener object in conjunction with the TcpClient. This link has a good example:


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消