本文整理汇总了C#中Socket.Write方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.Write方法的具体用法?C# Socket.Write怎么用?C# Socket.Write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.Write方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Handshake
private void Handshake(Socket socket)
{
socket.ReadBuffer(16)
.ContinueWith(initMsgTask =>
{
byte[] result;
try
{
result = initMsgTask.Result.Array;
}
catch (AggregateException e)
{
log.Error("Could not handshake properly", e);
socket.Write(new JObject
{
{"Type", "Error"},
{"Error", "Could not read BSON value"},
{"Details", string.Join(Environment.NewLine, e.InnerExceptions.Select(x => x.Message))}
})
.IgnoreExceptions()
.ContinueWith(_ => socket.Dispose());
return;
}
catch (Exception e)
{
log.Error("Could not handshake properly", e);
socket.Write(new JObject
{
{"Type", "Error"},
{"Error", "Could not read BSON value"},
{"Details", e.Message}
})
.IgnoreExceptions()
.ContinueWith(writeErrorTask => socket.Dispose());
return;
}
if (new Guid(result) != RequestHandshakeSignature)
{
log.Error("Could not handshake properly because client didn't pass value RequestHandshakeSignature");
socket.Write(new JObject
{
{"Type", "Error"},
{"Error", "Invalid server signature"}
})
.IgnoreExceptions()
.ContinueWith(_ => socket.Dispose());
return;
}
socket.Write(ResponseHandshakeSignature.ToByteArray())
.ContinueWith(responseMsgTask =>
{
if (responseMsgTask.Exception != null)
{
socket.Dispose();
return;
}
log.InfoFormat("New connection added from {0}", socket.RemoteEndPoint);
AddConnection(socket);
});
})
.ContinueWith(overallResponseTask =>
{
if (overallResponseTask.Exception != null)
socket.Dispose();
});
}
示例2: ProcessClient
public static void ProcessClient(Socket client)
{
var hostname = "";
var port = 80;
try
{
/// Check client for data to be read
if (!client.Poll(50000, SelectMode.SelectRead))
{
return;
}
/// Read data
using (var packetData = new MemoryStream())
{
client.ReadToStream(packetData);
if (packetData.Length == 0)
{
return;
}
/// Analyze received data
string packetText = Encoding.ASCII.GetString(packetData.ToArray());
if (packetText.IndexOf("CONNECT") != 0)
{
Regex regex = new Regex("Host: (.*)\r\n", RegexOptions.IgnoreCase);
if (regex.Match(packetText).Success)
{
hostname = regex.Match(packetText).Groups[1].Value;
}
}
else
{
Regex regex = new Regex("CONNECT (.*):(.*) HTTP", RegexOptions.IgnoreCase);
if (regex.Match(packetText).Success)
{
hostname = regex.Match(packetText).Groups[1].Value;
port = Convert.ToInt32(regex.Match(packetText).Groups[2].Value);
}
}
if (hostname.Length == 0)
{
return;
}
/// Connect to the server
using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
server.Connect(hostname, port);
if (!server.Connected)
{
return;
}
if (packetText.IndexOf("CONNECT") != 0)
{
/// Send initial packet to server
server.Write(packetData);
}
else
{
/// Send "200 Connection Established" to client
client.Write(Encoding.ASCII.GetBytes("HTTP/1.1 200 Connection Established\r\n\r\n"));
}
/// Simple tunneling
while (client.Connected && server.Connected)
{
bool exchanged = false;
/// Client -> Server exchange
if (client.Poll(1000, SelectMode.SelectRead))
{
using (var data = new MemoryStream())
{
client.ReadToStream(data);
if (data.Length != 0)
{
server.Write(data);
exchanged = true;
}
}
}
/// Server -> Client exchange
if (server.Poll(1000, SelectMode.SelectRead))
{
using (var data = new MemoryStream())
{
server.ReadToStream(data);
//.........这里部分代码省略.........