本文整理汇总了C#中System.Net.Sockets.NetworkStream.BeginWrite方法的典型用法代码示例。如果您正苦于以下问题:C# NetworkStream.BeginWrite方法的具体用法?C# NetworkStream.BeginWrite怎么用?C# NetworkStream.BeginWrite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Sockets.NetworkStream
的用法示例。
在下文中一共展示了NetworkStream.BeginWrite方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendPacket
public void SendPacket(LSASendPacket packet)
{
packet._Client = this;
if (!LSOpcode.Send.ContainsKey(packet.GetType()))
{
Log.Warn("UNKNOWN GS packet opcode: {0}", packet.GetType().Name);
return;
}
try
{
packet.WriteH(LSOpcode.Send[packet.GetType()]); // opcode
packet.WriteH(0); // packet len
packet.Write();
byte[] Data = packet.ToByteArray();
BitConverter.GetBytes((short)(Data.Length - 4)).CopyTo(Data, 2);
//if (Configuration.Setting.Debug) Log.Debug("Send: {0}", Data.FormatHex());
_stream = _client.GetStream();
_stream.BeginWrite(Data, 0, Data.Length, new AsyncCallback(WriteCallback), (object)null);
}
catch (Exception ex)
{
Log.Warn("Can't send GS packet: {0}", GetType().Name);
Log.WarnException("GSASendPacket", ex);
return;
}
}
示例2: WriteHeader
private void WriteHeader(NetworkStream stream)
{
batchesSent++;
if (batchesSent > TotalBatches)
{
//Mark end of transfer.
byte[] b = new byte[sizeof(long)];
b = BitConverter.GetBytes((long)0);
stream.Write(b, 0, sizeof(long));
sendDone.Set();
return;
}
Log.LogMessage("Sending request " + batchesSent.ToString());
TcpClient2OutputStreamContext ctx = new TcpClient2OutputStreamContext();
ctx.OutputStream = new MemoryStream();
ctx.ClientStream = stream;
StreamUtil.GenerateBigRequest(ctx.OutputStream, false, recordCount, recordCount + (BatchSize - 1));
ctx.OutputStream.Seek(0, SeekOrigin.Begin);
byte[] header = BitConverter.GetBytes(ctx.OutputStream.Length);
stream.BeginWrite(header, 0, header.Length, BeginWriteCallback, ctx);
recordCount += BatchSize;
}
示例3: Send
public void Send(byte[] bytes)
{
_stream = _client.GetStream();
_stream.BeginWrite(bytes, 0, bytes.Length, WriteCallback, null);
}
示例4: ServerSideClientThreadFuction
/**
* @brief Server側Cleint通信処理スレッド
* @param[in] void
* @return void
*/
void ServerSideClientThreadFuction()
{
int rsize = 0;
stream = myClient.GetStream();
stream.ReadTimeout = 5000; // 5sec
while (true)
{
try
{
// 受信処理
rsize = stream.Read(oneRcvBytes, 0, oneRcvBytes.Length);
if (rsize == 0) // 切断された
{
CloseStreamClient();
break;
}
else
{
rcvDataMemStream.Write(oneRcvBytes, 0, rsize); // 受信DataをBufferring
int leng = (int)rcvDataMemStream.Length;
if (leng >= 2) // 最後がCRLFか?
{
string rcvStr = Encoding.ASCII.GetString(rcvDataMemStream.GetBuffer(), 0, leng);
if ((string.Compare(rcvStr.Substring(leng - 2, 1), "\r") == 0) &&
(string.Compare(rcvStr.Substring(leng - 1, 1), "\n") == 0))
{
// 受信データ解析
string sendData = AnalyzeRcvData(rcvStr);
sendData = sendData + "\r\n";
// 以下の2行は、強制切断で Client側に切断が伝わるのを
// 確認するのに使用した。ここに breakかけて、
// Step実行後、デバッガを停止して確認終了。
//stream.Close();
//myClient.Close();
// Ack/Nack送信
byte[] sendBytes = Encoding.ASCII.GetBytes(sendData);
//stream.Write(sendBytes, 0, sendBytes.Length);
stream.BeginWrite(sendBytes, 0, sendBytes.Length, OnWriteComplete, null);
break; // exit while(true)
}
}
}
}
catch (System.IO.IOException ioerr)
{
// Read待ち終了
if (endReqFlag == true)
{
CloseStreamClient();
break;
}
// ReadTimeOut処理
else if ((ioerr.InnerException as System.Net.Sockets.SocketException).ErrorCode == 10060)
{
// ReadTimeout なので、再度Timeout時間設定し、再Readへ
stream.ReadTimeout = 5000;
Console.WriteLine("ReadTimeout. StartReRead.");
}
// 切断等の例外処理
else
{
Console.WriteLine(ioerr.Message);
CloseStreamClient();
break;
}
}
} // end of while
}