私はSslStreamを介して通信しているTcpClientとServerを作成しました。 通信は機能しますが、クライアントからサーバーにメッセージを送信すると、最初にサーバーは1バイトを読み取り、次のステップで残りのメッセージを読み取ります。例:私はクライアントを経由して「テスト」を送信すると、サーバーは最初の「T」を受信した後、「EST」SslStream EndReadは最初の1バイトを取得します
ここでは
public void Send(string text) {
byte[] message = Encoding.UTF8.GetBytes(text);
SecureStream.BeginWrite(message, 0, message.Length, new AsyncCallback(WriteCallback), null);
}
private void WriteCallback(IAsyncResult AR) {
}
そして、ここにコードを送信するために、クライアントのコードですサーバーは読み取りに使用します
private SslStream CryptedStream = ...;
private byte[] buffer = new byte[1024];
public void BeginReadCallback(IAsyncResult AsyncCall) {
// initialize variables
int bytesRead = 0;
try {
// retrieve packet
bytesRead = CryptedStream.EndRead(AsyncCall);
// check if client has disconnected
if (bytesRead > 0) {
// copy buffer to a temporary one
var temporaryBuffer = buffer;
Array.Resize(ref temporaryBuffer, bytesRead);
string read = Encoding.ASCII.GetString(temporaryBuffer);
SetText(read);
// read more data
CryptedStream.BeginRead(buffer, 0, 1024, new AsyncCallback(BeginReadCallback), null);
// client is still connected, read data from buffer
//ProcessPacket(temporaryBuffer, temporaryBuffer.Length, helper);
} else {
// client disconnected, do everything to disconnect the client
//DisconnectClient(helper);
}
} catch (Exception e) {
// encountered an error, closing connection
// Program.log.Add(e.ToString(), Logger.LogLevel.Error);
// DisconnectClient(helper);
}
}
私は何かを見逃しましたか? 助けてくれてありがとう
ここでの質問は何ですか? A *ストリーミング接続*は決してバイトのパケットを送信しません。バイトのストリームを送信します。読み込み時には、パッケージを考慮した小さなものを取得してから、これを一貫したパッケージに再アセンブルする必要があります必要があります。 –
私はストリームに精通していないので、私の未熟な私を許してください。 それは、サーバーがはるかに高速であれば、私は "t" "e" "s" "t"を受け取ることを意味するでしょうか? –
あなたはそれを受け取ることもできますし、 'tes'、' t'、 't'、' est'や 'te'、' st'を受け取ることもできます。これは、多くのこと、速度、ハードウェア(コンピュータ、ルータ、2つのエンドポイント間のスイッチなど)とソフトウェアに依存します。要するに、あなたのコードはこれを処理できる必要があります。 –