1 / 14

Network Programming with C#

Network Programming with C#. Exceed Camp #2, CPE, KU Day 3. Outline. Socket Programming Threading in C#. Socket Overview. Socket Class Provides a rich set of methods and properties for network communications. User Datagram Protocol (UDP)

monet
Télécharger la présentation

Network Programming with C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Network Programming with C# Exceed Camp #2, CPE, KUDay 3

  2. Outline • Socket Programming • Threading in C#

  3. Socket Overview • Socket Class • Provides a rich set of methods and properties for network communications. • User Datagram Protocol (UDP) • Connectionless, unreliable transport layer protocol Application UDP IP

  4. Internet Proc4 Ports Proc3 Proc2 Proc1 Proc5 UDP Ports • 16-bit unsigned integers associated with UDP connection • Used to distinguish different processes running on the same host A B IP Address 1 IP Address 2

  5. Using UDP Sockets To receive data To send data Create a UDP socket Create a UDP socket Create a local endpoint(Local IP + Local Port) Send data Close socket Bind socket tothe endpoint Receive data Close socket

  6. Creating UDP Socket • The following namespaces should be used • Use new keyword to create an object of class Socket using System.Net; using System.Net.Sockets; using System.Text; Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

  7. Creating Local Endpoint • Resolve local IP addresses using functions Dns.Resolve() and Dns.GetHostName() • Create an endpoint at the first IP address and port 11000 IPHostEntry hostEntry = Dns.Resolve(Dns.GetHostName()); IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000); Note: port number can be anything above 1023

  8. Binding Socket to Endpoint • Make the socket wait for incoming data at the endpoint created previously s.Bind(endPoint);

  9. Receiving Data • Another endpoint is prepared to store remote endpoint information • ReceiveFrom() will block until data is received IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderRemote = (EndPoint)sender; byte[] msg = new Byte[256]; Console.WriteLine("Waiting for data..."); s.ReceiveFrom(msg, ref senderRemote);

  10. Sending Data • Another endpoint is created to specify the receiver IP address and UDP port byte[] msg = Encoding.ASCII.GetBytes("This is a test"); Console.WriteLine("Sending data."); s.SendTo(msg, new IPEndPoint(IPAddress.Parse("10.0.0.2"), 11000));

  11. Closing Socket • When no longer used, the socket must be closed s.Close();

  12. Threading • ReceiveFrom() is blocking • I.e., it waits until data is received Start Start blocks blocks ReceiveFrom() new Thread : : ReceiveFrom() : One thread of control Two threads of control

  13. Creating Thread • For convenient, use System.Threading namespace using System.Threading; void ThreadProc() { : while (true) { s.ReceiveFrom(...); } } void MainProc() { : Thread t = new Thread(new ThreadStart(ThreadProc)); t.IsBackground = true; t.Start(); : }

  14. Assignment • โปรแกรม NetBall • ทำงานคล้ายโปรแกรมเดาะบอล แต่เล่นสองคน • แต่ละคนผลัดกันเดาะบอล ใครพลาดก่อนแพ้

More Related