좋아, 내 게임을 위해서 필자는 서버/클라이언트 피어 투 피어 (peer to peer) 연결을 설정하고 위치 등을 앞뒤로 보냈다.비동기 TCP 서버/클라이언트 신뢰할 수없는 패킷?
비록 내 메시지가 실제로는 빠르지 만 안정적이지는 않지만. 여기
public void RecieveAsync()
{
if (netStream == null) netStream = Server.GetStream();
if (netStream.DataAvailable == false) return;
netStream.BeginRead(ReadBuffer, 0, ReadBuffer.Length, new AsyncCallback(recieveCallBack), netStream);
}
public void recieveCallBack(IAsyncResult ar)
{
//try
//{
String content = String.Empty;
Console.WriteLine("Stuck trying to get data");
int rec = netStream.EndRead(ar);
if (rec > 0)
{
Console.WriteLine(Encoding.ASCII.GetString(
ReadBuffer, 0, rec));
string packet = Encoding.ASCII.GetString(
ReadBuffer, 0, rec);
bool completedPacket = false;
int appendTo = rec;
if (packet.Contains("<eof>"))
{
appendTo = packet.IndexOf("<eof>");
packet.Replace("<eof>", "");
completedPacket = true;
}
SB.Append(packet, 0, appendTo);
// Check for end-of-file tag. If it is not there, read
// more data.
content = SB.ToString();
if (completedPacket)
{
// All the data has been read from the
// client. Display it on the console.
if (DataRecieved != null)
{
string RecievedData = SB.ToString();
DataRecieved(RecievedData);
netStream.Flush();
Array.Clear(ReadBuffer, 0, ReadBuffer.Length);
ReadBuffer = new byte[1024];
}
SB.Clear();
// Echo the data back to the client.
}
else
{
// Not all data received. Get more.
Array.Clear(ReadBuffer, 0, ReadBuffer.Length);
ReadBuffer = new byte[1024];
netStream.BeginRead(ReadBuffer, 0, ReadBuffer.Length, recieveCallBack, netStream);
}
}
}
그리고 내 보내는 코드 :
을 .T 어쨌든 내 잡 코드가 여기에 문자열의 일부와 같이 (왜 확실하지)없는, 때로는 전송은 중지하고 스레드 나던 계속된다 :
public void Send(byte[] data, int index, int length)
{
//add data as state
//socket.NoDelay = true;
if (netStream == null) netStream = TcpClient.GetStream();
netStream.BeginWrite(data, 0, length, sendCallback, netStream);
}
private void sendCallback(IAsyncResult ar)
{
//try
//{
netStream.EndWrite(ar);
//if (ar.AsyncState != null)
//{
// byte[] buffer = (byte[])ar.AsyncState;
// socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, sendCallback, null);
// return;
//}
if (OnSend != null)
{
OnSend(this);
}
netStream.Flush();
//catch (Exception ex)
//{
// System.Windows.Forms.MessageBox.Show(ex.ToString());
// return;
//}
}
패킷은 Encoding.ASCII.Getbytes에 있습니다.
그리고 서버와 클라이언트가 while (true) 스레드에서 Thread.Sleep (1)로 업데이트 중입니다.
도 패킷 끝을 표시합니다. – user1965626
또한 전송 된 패킷을 병합하고 프레이밍 ('')을 제거합니다. 'DataReceived'는 하나의 "패킷"(기본 네트워크 용어가 아닌 용어로) 이상을 제공 할 수 있으며이를 해독 할 방법이 없습니다. 근본적으로 여기서해야 할 일은받는 모든 것을 기록하고 처리하는 모든 것을 기록하는 것입니다. 그러면 어떤 일이 잘못되는지 알 수 있습니다. –