Слайд 2
Со́кеты (англ. socket — углубление, гнездо, разъём) — программный интерфейс для обеспечения обмена
данными между процессами.
Слайд 3
Сокет Беркли
Серверные
Клиентские
Слайд 5
System.Net.Sockets
Windows Sockets
Дуплексное взаимодействие
Слайд 6
Socket
Синхронная передача
Асинхронная передача
Слайд 7
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
Конструктор
Слайд 8
Асинхронные методы
ConnectAsync
SendAsync
ReceiveAsync
Shutdown
Close
Слайд 10
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
…
Слайд 11
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
...
Слайд 12
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
…
Слайд 13
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
…
Слайд 15
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
…
Слайд 16
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
…
Слайд 17
…
SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
DnsEndPoint hostEntry =
new DnsEndPoint("http://www.google.com", 80);
socketEventArg.Completed += new EventHandler(SocketEventArg_Completed);
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.UserToken = sock;
sock.ConnectAsync(socketEventArg);
…
Слайд 18
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 20
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 21
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 22
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 23
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 24
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 25
private static void ProcessConnect(SocketAsyncEventArgs e)
{
…
byte[] buffer
= Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length);
Socket sock =
e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ProcessSend(e);
}
…
}
Слайд 26
private static void ProcessConnect(SocketAsyncEventArgs e)
{
…
byte[] buffer
= Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length);
Socket sock =
e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ProcessSend(e);
}
…
}
Слайд 27
private static void ProcessConnect(SocketAsyncEventArgs e)
{
…
byte[] buffer
= Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length);
Socket sock =
e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ProcessSend(e);
}
…
}
Слайд 28
private static void ProcessConnect(SocketAsyncEventArgs e)
{
…
byte[] buffer
= Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length);
Socket sock =
e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ProcessSend(e);
}
…
}
Слайд 29
private static void ProcessConnect(SocketAsyncEventArgs e)
{
…
byte[] buffer
= Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length);
Socket sock =
e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ProcessSend(e);
}
…
}
Слайд 30
private static void ProcessConnect(SocketAsyncEventArgs e)
{
…
byte[] buffer
= Encoding.UTF8.GetBytes("Hello World"); e.SetBuffer(buffer, 0, buffer.Length);
Socket sock =
e.UserToken as Socket;
bool willRaiseEvent = sock.SendAsync(e);
if (!willRaiseEvent)
{
ProcessSend(e);
}
…
}
Слайд 31
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 32
private static void ProcessSend(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket;
bool willRaiseEvent = sock.ReceiveAsync(e);
if
(!willRaiseEvent)
{
ProcessReceive(e);
}
…
}
Слайд 33
private static void ProcessSend(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket;
bool willRaiseEvent = sock.ReceiveAsync(e);
if
(!willRaiseEvent)
{
ProcessReceive(e);
}
…
}
Слайд 34
private static void ProcessSend(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket;
bool willRaiseEvent = sock.ReceiveAsync(e);
if
(!willRaiseEvent)
{
ProcessReceive(e);
}
…
}
Слайд 35
private static void ProcessSend(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket;
bool willRaiseEvent = sock.ReceiveAsync(e);
if
(!willRaiseEvent)
{
ProcessReceive(e);
}
…
}
Слайд 36
static void SocketEventArg_Completed(object sender,
SocketAsyncEventArgs e) {
switch (e.LastOperation)
{
case SocketAsyncOperation.Connect:
ProcessConnect(e);
break;
case SocketAsyncOperation.Receive:
ProcessReceive(e);
break;
case
SocketAsyncOperation.Send:
ProcessSend(e);
break;
default: throw new Exception("Invalid operation completed"); }
}
Слайд 37
private static void ProcessReceive(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket; sock.Shutdown(SocketShutdown.Send);
sock.Close();
…
}
Слайд 38
private static void ProcessReceive(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket; sock.Shutdown(SocketShutdown.Send);
sock.Close();
…
}
Слайд 39
private static void ProcessReceive(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket; sock.Shutdown(SocketShutdown.Send);
sock.Close();
…
}
Слайд 40
private static void ProcessReceive(SocketAsyncEventArgs e)
{
…
Socket sock
= e.UserToken as Socket; sock.Shutdown(SocketShutdown.Send);
sock.Close();
…
}
Слайд 41
Этапы выполнения асинхронной операции
Выделите SocketAsyncEventArgs
Задайте свойства объекта SocketAsyncEventArgs,
Вызовите
соответствующий метод сокета.
Функции обратного вызова сделайте запрос состояния завершения и
результатов операций.
Повторно используйте объект SocketAsyncEventArgs для другой операции.