Сергеев Н.-.NET Socket.pptx
- Количество слайдов: 42
. NET SOCKET Сергеев Николай КБ-401
Со кеты (англ. socket — углубление, гнездо, разъём) — программный интерфейс для обеспечения обмена данными между процессами.
Сокет Беркли Серверные • Клиентские •
System. Net
System. Net. Sockets Windows Sockets • Дуплексное взаимодействие •
Socket Синхронная передача • Асинхронная передача •
Конструктор Socket sock = new Socket(Address. Family. Inter. Network, Socket. Type. Stream, Protocol. Type. Tcp);
Асинхронные методы • • • Connect. Async Send. Async Receive. Async Shutdown Close
Socket. Async. Event. Args
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); …
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); . . .
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); …
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); …
Функция обратного вызова
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); …
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); …
… Socket. Async. Event. Args socket. Event. Arg = new Socket. Async. Event. Args(); Dns. End. Point host. Entry = new Dns. End. Point("http: //www. google. com", 80); socket. Event. Arg. Completed += new Event. Handler<Socket. Async. Event. Args>(Socket. Event. Arg_Completed); socket. Event. Arg. Remote. End. Point = host. Entry; socket. Event. Arg. User. Token = sock; sock. Connect. Async(socket. Event. Arg); …
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
Process. Send Call. Back Process. Receive Process. Connect
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
private static void Process. Connect(Socket. Async. Event. Args e) { … byte[] buffer = Encoding. UTF 8. Get. Bytes("Hello World"); e. Set. Buffer(buffer, 0, buffer. Length); Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Send. Async(e); if (!will. Raise. Event) { Process. Send(e); } … }
private static void Process. Connect(Socket. Async. Event. Args e) { … byte[] buffer = Encoding. UTF 8. Get. Bytes("Hello World"); e. Set. Buffer(buffer, 0, buffer. Length); Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Send. Async(e); if (!will. Raise. Event) { Process. Send(e); } … }
private static void Process. Connect(Socket. Async. Event. Args e) { … byte[] buffer = Encoding. UTF 8. Get. Bytes("Hello World"); e. Set. Buffer(buffer, 0, buffer. Length); Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Send. Async(e); if (!will. Raise. Event) { Process. Send(e); } … }
private static void Process. Connect(Socket. Async. Event. Args e) { … byte[] buffer = Encoding. UTF 8. Get. Bytes("Hello World"); e. Set. Buffer(buffer, 0, buffer. Length); Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Send. Async(e); if (!will. Raise. Event) { Process. Send(e); } … }
private static void Process. Connect(Socket. Async. Event. Args e) { … byte[] buffer = Encoding. UTF 8. Get. Bytes("Hello World"); e. Set. Buffer(buffer, 0, buffer. Length); Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Send. Async(e); if (!will. Raise. Event) { Process. Send(e); } … }
private static void Process. Connect(Socket. Async. Event. Args e) { … byte[] buffer = Encoding. UTF 8. Get. Bytes("Hello World"); e. Set. Buffer(buffer, 0, buffer. Length); Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Send. Async(e); if (!will. Raise. Event) { Process. Send(e); } … }
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
private static void Process. Send(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Receive. Async(e); if (!will. Raise. Event) { Process. Receive(e); } … }
private static void Process. Send(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Receive. Async(e); if (!will. Raise. Event) { Process. Receive(e); } … }
private static void Process. Send(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Receive. Async(e); if (!will. Raise. Event) { Process. Receive(e); } … }
private static void Process. Send(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; bool will. Raise. Event = sock. Receive. Async(e); if (!will. Raise. Event) { Process. Receive(e); } … }
static void Socket. Event. Arg_Completed(object sender, Socket. Async. Event. Args e) { switch (e. Last. Operation) { case Socket. Async. Operation. Connect: Process. Connect(e); break; case Socket. Async. Operation. Receive: Process. Receive(e); break; case Socket. Async. Operation. Send: Process. Send(e); break; default: throw new Exception("Invalid operation completed"); } }
private static void Process. Receive(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; sock. Shutdown(Socket. Shutdown. Send); sock. Close(); … }
private static void Process. Receive(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; sock. Shutdown(Socket. Shutdown. Send); sock. Close(); … }
private static void Process. Receive(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; sock. Shutdown(Socket. Shutdown. Send); sock. Close(); … }
private static void Process. Receive(Socket. Async. Event. Args e) { … Socket sock = e. User. Token as Socket; sock. Shutdown(Socket. Shutdown. Send); sock. Close(); … }
Этапы выполнения асинхронной операции 1. 2. 3. 4. 5. Выделите Socket. Async. Event. Args Задайте свойства объекта Socket. Async. Event. Args, Вызовите соответствующий метод сокета. Функции обратного вызова сделайте запрос состояния завершения и результатов операций. Повторно используйте объект Socket. Async. Event. Args для другой операции.
СПАСИБО ЗА ВНИМАНИЕ!
Сергеев Н.-.NET Socket.pptx