我想寫一個小的C#應用程序,它將能夠以客戶端/服務器方式發送和接收加密的消息。C#通過NetworkStream發送和接收消息 - 簡單的代碼,但不能按預期工作
這MSDN的例子是非常接近的東西,我需要(只是爲了獲得基本的功能) http://msdn.microsoft.com/en-us/library/as0w18af.aspx http://msdn.microsoft.com/en-us/library/te15te69.aspx
所以客戶端對消息進行加密,將其發送到服務器,服務器將其解密並將明文寫入控制檯。
一切工作正常,在這一個方向
但是當我嘗試發送郵件從服務器到客戶端(如加密或明文無所謂),它失敗。
Server代碼
using System;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;
using System.Security.Cryptography;
class Class1
{
static void Main(string[] args)
{
try
{
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};
string ipAddress = "127.0.0.1";
TcpListener TCPListen = new TcpListener(IPAddress.Parse(ipAddress),11000);
TCPListen.Start();
while(!TCPListen.Pending())
{
Console.WriteLine("Still listening. Will try in 5 seconds.");
Thread.Sleep(5000);
}
TcpClient TCP = TCPListen.AcceptTcpClient();
NetworkStream NetStream = TCP.GetStream();
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CStream_READ = new CryptoStream(NetStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
StreamReader SReader = new StreamReader(CStream_READ);
Console.WriteLine("The decrypted original message: {0}", SReader.ReadToEnd());
// so far so good, but the next portion of the code does not run properly
CryptoStream CStream_WRITE = new CryptoStream(NetStream, RMCrypto.CreateEncryptor(Key,IV),CryptoStreamMode.Write);
StreamWriter SWriter = new StreamWriter(CStream_WRITE);
SWriter.WriteLine("message from server");
SWriter.Flush();
Console.WriteLine("The message was sent.");
SReader.Close();
SWriter.Close();
NetStream.Close();
TCP.Close();
}
//Catch any exceptions.
catch
{
Console.WriteLine("The Listener Failed.");
}
}
}
客戶端代碼
using System;
using System.IO;
using System.Security.Cryptography;
using System.Net.Sockets;
public class main
{
public static void Main(string[] args)
{
try
{
TcpClient TCP = new TcpClient("localhost",11000);
NetworkStream NetStream = TCP.GetStream();
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 CStreamWRITE = new CryptoStream(NetStream, RMCrypto.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
StreamWriter SWriter = new StreamWriter(CStreamWRITE);
SWriter.WriteLine("Hello World!");
SWriter.Flush();
Console.WriteLine("The message was sent.");
// so far so good, but the next portion of the code does not run properly
CryptoStream CStreamREAD = new CryptoStream(NetStream, RMCrypto.CreateDecryptor(Key,IV),CryptoStreamMode.Read);
StreamReader SReader = new StreamReader(CStreamREAD);
Console.WriteLine("od servera som dostal: {0}", SReader.ReadToEnd());
SWriter.Close();
SWriter.Close();
CStreamWRITE.Close();
NetStream.Close();
TCP.Close();
}
catch
{
Console.WriteLine("The connection failed.");
}
}
}
我做什麼,我只是反映我認爲相關的執行功能 所以服務器應該是第一個代碼接收來自客戶端的消息,然後發送消息給客戶端
如果我註釋掉客戶端發送消息到服務器,那麼服務器successf ully發送消息給客戶端
請問您能否幫我解決,並告訴我爲什麼它不能同時在兩個方向上工作,但是分開就行了?
在此先感謝
你收到了什麼錯誤? – 2012-03-11 23:21:47
我沒有收到任何錯誤消息。 問題是當服務器發送消息的代碼時,不僅代碼不會執行(而是一步一步地釋放代碼),而且服務器也不會(可能)接收消息。雖然該行執行 當我註釋掉它沒有被寫在控制檯,它解密收到的meesage精細 – Martin 2012-03-12 06:40:38