Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

14
Network Programming Network Programming with C# with C# Exceed Camp #2, CPE, KU Exceed Camp #2, CPE, KU Day 3 Day 3

Transcript of Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Page 1: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Network Programming Network Programming with C#with C#

Exceed Camp #2, CPE, KUExceed Camp #2, CPE, KUDay 3Day 3

Page 2: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

OutlineOutline

Socket ProgrammingSocket Programming Threading in C#Threading in C#

Page 3: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Socket OverviewSocket Overview

Socket ClassSocket Class Provides a rich set of methods and Provides a rich set of methods and

properties for network communications. properties for network communications.

User Datagram Protocol (UDP)User Datagram Protocol (UDP) Connectionless, unreliable transport Connectionless, unreliable transport

layer protocollayer protocolApplicationApplication

UDPUDP

IPIP

Page 4: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

UDP PortsUDP Ports

16-bit unsigned integers associated 16-bit unsigned integers associated with UDP connectionwith UDP connection

Used to distinguish different Used to distinguish different processes running on the same hostprocesses running on the same host

InternetInternetAA BB

Proc1Proc1 Proc2Proc2 Proc3Proc3Proc4Proc4

Proc5Proc5

IP Address 1IP Address 1 IP Address 2IP Address 2

PortsPorts

Page 5: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Using UDP SocketsUsing UDP Sockets

Create a UDP socketCreate a UDP socket

Create a local endpointCreate a local endpoint(Local IP + Local Port)(Local IP + Local Port)

Bind socket toBind socket tothe endpointthe endpoint

Receive dataReceive data

Close socketClose socket

Create a UDP socketCreate a UDP socket

Send dataSend data

Close socketClose socket

To receive dataTo receive data To send dataTo send data

Page 6: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Creating UDP SocketCreating UDP Socket

The following namespaces should be The following namespaces should be usedused

Use Use newnew keyword to create an object keyword to create an object of class of class SocketSocket

Socket s = new Socket(AddressFamily.InterNetwork,Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,SocketType.Dgram, ProtocolType.Udp);ProtocolType.Udp);

using System.Net;using System.Net;using System.Net.Sockets;using System.Net.Sockets;using System.Text;using System.Text;

Page 7: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Creating Local EndpointCreating Local Endpoint

Resolve local IP addresses using Resolve local IP addresses using functions functions Dns.Resolve()Dns.Resolve() and and Dns.GetHostName()Dns.GetHostName()

Create an endpoint at the first IP Create an endpoint at the first IP address and port 11000address and port 11000

IPHostEntry hostEntry = Dns.Resolve(Dns.GetHostName());IPHostEntry hostEntry = Dns.Resolve(Dns.GetHostName());IPEndPoint endPoint = IPEndPoint endPoint =

new IPEndPoint(hostEntry.AddressList[0], 11000); new IPEndPoint(hostEntry.AddressList[0], 11000);

Note: port number can be anything above 1023Note: port number can be anything above 1023

Page 8: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Binding Socket to Binding Socket to EndpointEndpoint Make the socket wait for incoming Make the socket wait for incoming

data at the endpoint created data at the endpoint created previouslypreviously

s.Bind(endPoint); s.Bind(endPoint);

Page 9: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Receiving DataReceiving Data

Another endpoint is prepared to Another endpoint is prepared to store remote endpoint informationstore remote endpoint information

ReceiveFrom() will ReceiveFrom() will blockblock until data is until data is receivedreceived

IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);EndPoint senderRemote = (EndPoint)sender;EndPoint senderRemote = (EndPoint)sender;

byte[] msg = new Byte[256];byte[] msg = new Byte[256];Console.WriteLine("Waiting for data...");Console.WriteLine("Waiting for data...");s.ReceiveFrom(msg, ref senderRemote);s.ReceiveFrom(msg, ref senderRemote);

Page 10: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Sending DataSending Data

Another endpoint is created to Another endpoint is created to specify the receiver IP address and specify the receiver IP address and UDP portUDP port

byte[] msg = Encoding.ASCII.GetBytes("This is a test");byte[] msg = Encoding.ASCII.GetBytes("This is a test");Console.WriteLine("Sending data.");Console.WriteLine("Sending data.");s.SendTo(msg, s.SendTo(msg, new IPEndPoint(IPAddress.Parse("10.0.0.2"), 11000));new IPEndPoint(IPAddress.Parse("10.0.0.2"), 11000));

Page 11: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Closing SocketClosing Socket

When no longer used, the socket When no longer used, the socket must be closedmust be closed

s.Close();s.Close();

Page 12: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

ThreadingThreading ReceiveFrom() is blockingReceiveFrom() is blocking

I.e., it waits until data is receivedI.e., it waits until data is received

blockblockss blockblock

ssReceiveFrom()ReceiveFrom()

StartStart

::

new Threadnew Thread

StartStart

:: ReceiveFrom()ReceiveFrom()

::

One thread One thread of controlof control

Two threads Two threads of controlof control

Page 13: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

Creating ThreadCreating Thread For convenient, use For convenient, use System.ThreadingSystem.Threading

namespacenamespace

void ThreadProc()void ThreadProc(){{ :: while (true)while (true) {{ s.ReceiveFrom(...);s.ReceiveFrom(...); }}}}void MainProc()void MainProc(){{ :: Thread t = new Thread(new ThreadStart(ThreadProc));Thread t = new Thread(new ThreadStart(ThreadProc)); t.IsBackground = true;t.IsBackground = true; t.Start();t.Start(); ::}}

using System.Threading;using System.Threading;

Page 14: Network Programming with C# Exceed Camp #2, CPE, KU Day 3.

AssignmentAssignment

โปรแกรม โปรแกรม NetBallNetBall ทำงนคล้�ยโปรแกรมเดะบอล้ แต่�เล้�นสองคนทำงนคล้�ยโปรแกรมเดะบอล้ แต่�เล้�นสองคน แต่�ล้ะคนผล้�ดก�นเดะบอล้ ใครพล้ดก�อนแพ�แต่�ล้ะคนผล้�ดก�นเดะบอล้ ใครพล้ดก�อนแพ�