2017-01-11 4 views
0

私はソケットC#で作業しています。ソケットを使用してクライアントサーバーアプリケーションを実装しましたが、問題はクライアントがサーバーから送信されたすべてのデータを受信しないことです。クライアントサーバーソケットC#

ここにクライアントアプリケーションコードがあります。サーバーから送信されたすべてのデータを受信するためにはどうすればよいですか?

strRecieved = ""; 
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9001); 
soc.Connect(endPoint); 
byte[] msgBuffer = Encoding.Default.GetBytes(path); 
soc.Send(msgBuffer, 0, msgBuffer.Length, 0); 
byte[] buffer = new byte[2000]; 
int rec = soc.Receive(buffer); 

strRecieved = String.Format(Encoding.Default.GetString(buffer)); 
+0

自分で送信するIPソケットを作成しましたか? ..あなたの例のパスは未定義と思われます。パスはどれくらいですか?より正確には、msgbufferの大きさはどれくらいですか?あなたの受信バッファが2000バイトを受け取っただけです。メッセージはより大きいですか? – BugFinder

+0

正確には、受信するメッセージが大きければ、メッセージ全体を受信しません。受信したメッセージを最大にするにはどうすればよいですか?ありがとう – user7394882

+0

なぜ2000年にバッファを設定していますか? – BugFinder

答えて

1

まずは、何らかのストリーミング機能(tcp/udp/file)を実装する場合は、プロトコルのようなものを使用することを検討する必要があります。

プロトコルとは何ですか?これは、データをストリーミングするときに使用するスキームです。例:

[4バイト - 長さ] [lengthBytes - メッセージ] [1バイト - 終了インジケータ]

あなたは、単にような着信バイトのすべてを読むことができるプロトコルを知る:

byte[] buffer = new byte[4]; 
stream.ReadBytes(buffer, 0, 4); // cast that to int and read the rest 

int packetLen = BitConverter.ToInt32(buffer, 0); 
buffer = new byte[packetLen]; 
stream.ReadBytes(buffer, 0, buffer.Length); // all bytes that was sent 

メッセージを送信する前に、長さの4バイトを減算する必要があることに注意してください。

EDIT:共有プロトコルを使用してデータを送受信する方法について

簡単な例。

この例であろう所望のプロトコルを使用するには、この技術力

それは簡単作る

// sender.cs 
string _stringToSend = "some fancy string"; 
byte[] encodedString = Encoding.UTF8.GetBytes(_stringToSend); 
List<byte> buffer = new List<byte>(); 
buffer.AddRange(BitConverter.GetBytes(encodedString.Length)); 
buffer.AddRange(encodedString); 
netStream.WriteBytes(buffer.ToArray(), 0, buffer.Count); 
// netStream sent message in protocol [@LEN - 4Bytes][@MSG - @LENBytes] 
// simply speaking something like: 5ABCDE 

// receiver.cs 
byte[] buffer = new byte[sizeof(int)]; 
netStream.ReadBytes(buffer, 0, buffer.Length); 
// receiver got the length of the message eg. 5 
int dataLen = BitConverter.ToInt32(buffer, 0); 
buffer = new byte[dataLen]; 
// now we can read an actual message because we know it's length 
netStream.ReadBytes(buffer, 0, buffer.Length); 
string receivedString = Encoding.UTF8.GetString(buffer); 
// received string is equal to "some fancy string" 

最初の4バイトsizeof(int)着信パケット の長さを指示していますすべてのバイトは、最後まであなたのパケットです。あなたが送信し、あなたのメッセージを受信する必要が(あなたがネットワークから読み取るために選択した方法に応じて)今

public static class ProtocolHelper 
{ 
    public byte[] PackIntoProtocol(string message) 
    { 
     List<byte> result = new List<byte>(); 
     byte[] messageBuffer = Encoding.UTF8.GetBytes(message); 
     result.AddRange(BitConverter.GetBytes(messageBuffer.Length), 0); // this is the first part of the protocol (length of the message) 
     result.AddRange(messageBuffer); // this is actual message 
     return result.ToArray(); 
    } 

    public string UnpackProtocol(byte[] buffer) 
    { 
     return Encoding.UTF8.GetString(buffer, 0, buffer.Length); 
    } 
} 

だから今、あなたはProtocolHelperオブジェクトを作成する必要があります。

// sender.cs 
string meMessage = "network message 1"; 
byte[] buffer = ProtocolHelper.PackIntoProtocol(meMessage); 
socket.Send(buffer, 0, buffer.Length, 0); 

// receiver.cs 
string message = string.Empty; 
byte[] buffer = new byte[sizeof(int)]; // or simply new byte[4]; 
int received = socket.Receive(buffer); 
if(received == sizeof(int)) 
{ 
    int packetLen = BitConverter.ToInt32(buffer);// size of our message 
    buffer = new byte[packetLen]; 
    received = socket.Receive(buffer); 
    if(packetLen == received) // we have full buffer 
    { 
     message = PacketHelper.UnpackProtocol(buffer); 
    } 
} 
Console.WriteLine(message); // output: "network message 1" 
+0

私はtcp IPプロトコルを使用しています。ソースコードを確認してください。 – user7394882

+0

変数netStreamはsokcet.rceive(バッファ)を使用しています。どうすれば私のコードをpdateできますか? – user7394882

+0

@ m.rogaski in class protocol helper変数の結果はどういうものなのですか? – user7394882