开发者

Check for incoming data

开发者 https://www.devze.com 2023-01-05 08:35 出处:网络
I\'m developing a encrypted chatting program that works in LAN it also should be able to send file.here is the problems:

I'm developing a encrypted chatting program that works in LAN it also should be able to send file.here is the problems: 1-For chatting part it only send 1 message and close the connection, the other party should run the program again in order to receive further messages, I tried to put it in a loop like this ( receive close and receive again), but the program crashed,

I have the same problem for receiving files,

Any idea how can I solve this problem? This is the code IP = textBox2.Text.ToString(); int port = int.Parse(textBox1.Text); IPAddress IP2 = IPAddress.Parse(IP); TcpListener TCPListen = new TcpListener(IP2, port);

                TCPListen.Start();

            TcpClient TCP = TCPListen.AcceptTcpClient();
            //bool a = false;

            NetworkStream NetStream = TCP.GetStream();



            //while (!a)
            //{

            RijndaelManaged RMCrypto = new RijndaelManaged();


            byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };




            CryptoStream CryptStream = new CryptoStream(NetStream,
               RMCrypto.CreateDecryptor(Key, IV),
               CryptoStreamMode.Read);

            StreamReader SReader = new StreamReader(CryptStream);




            message = SReader.ReadToEnd();

            textBox3.Clear();

            textBox3.Text = message;

            CryptStream.Flush();

            SReader.Close();


            //encryption(NetStream);




            NetStream.Flush();

            NetStream.Close();

            TCPListen.Stop();
            TCP.Close();

An开发者_如何学God it is in a loop like bool a = false; while(!a) it means continuously do it.


First, you're making this far more complicated than it has to be. There isn't really any need to deal directly with sockets or even the TcpClient for things like this anymore. Use WCF to have clients communicate with each other. It would be extremely simple to do, and you don't have to worry about all of this tediousness. WCF handles all of the piping for you.

Secondly, if you were going to do it the way you are doing - you can open a NetworkStream or SslStream and "read" and "write" to that stream to handle all your communication. The easiest way is going to be by doing everything over one connection/stream. That would mean you can't chat, while your transferring files though. As soon as you bring another stream into the picture, that is another order of magnitude more complicated, because now you have to manage the connection and lifetime of that stream, in addition to your main stream. It seems to me, this approach could be very brittle - because most of your code would have to be around recovering from problems. What if someone reboots a router while you are communicating? What happens if the sending of your file fails? Because you are handling this at such a low level, this would require a lot of code - just to fail gracefully.

Again, I would positively use WCF for this. Hope that helps.

0

精彩评论

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

关注公众号