Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
UDP Socket Programming For Distributed Computing in C#.Net
Rating: 4.5 out of 5(170 ratings)
1,177 students

UDP Socket Programming For Distributed Computing in C#.Net

Socket programming tutorial, make chat application in C# .Net, IoT/VoIP distributed applications & network programming
Created byNaeem Akram
Last updated 8/2020
English

What you'll learn

  • UDP socket programming in C# .Net, synchronous and aynchronous
  • Foundations of distributed chat/IoT applications, communication components of multiplayer games, VoIP applications in C# .Net
  • Using Newtonsoft JSON in client server socket based applications

Course content

8 sections50 lectures3h 14m total length
  • Socket Programming in C#: Host in computer network1:02

    We must have a sound understanding of a few networking concepts before writing a network application’s code.

    We’ll start with the term Host.

    A host is a fancy term for any computing device connected to a computer network.

    This machine can be a laptop, a smartphone, your home router, Amazon Alexa, or a server running in the cloud.

    A machine is called a host for a reason.

    It is called a host because it is hosting software that can communicate over the network.

    Every host on a network is accessible through a unique address within that network.

    This address is called Internet Protocol Address, or IP Address for short.

    It is written in dotted decimal notation.

    A hostname in computer networking is the unique name assigned to a device connected to a network. It is used to identify and distinguish a specific device from other devices on the same network. Host names are commonly used in the Domain Name System (DNS) to map domain names to IP addresses. They are also used in various networking protocols, such as the File Transfer Protocol (FTP) and the Simple Mail Transfer Protocol (SMTP), to establish connections between devices on a network.

  • Socket Programming in C#: IP Address in computer network1:27

    In this video, I’ll explain what is an IP address.

    We’ll use IP V4 in this course.

    Another version of IP specification is IP V6, which is catching up these days.

    An IP V4 address is a group of 4 separate 8-bit numbers.

    The total length of the IP Address is 32 bits.

    An individual number inside the IP Address can have a value between 0 and 255

    When printing an IP address, the four numbers are separated by a dot to make reading easier.

    This method of printing the IP address is called the dotted decimal notation.

    In many cases, a hostname is used instead of an IP address.

    A hostname gets translated into an IP address by a Domain Name Server, a.k.a DNS.

    An IP address is a unique numeric identifier assigned to each device connected to a network. It allows devices to communicate with each other over the network using a standardized set of rules and protocols. In socket programming, an IP address is often used in conjunction with a port number to establish a connection between two devices. The combination of the IP address and port number is known as a socket. This allows programs to communicate with each other across the network, even if they are running on different devices or operating systems. Overall, IP addresses and socket programming are essential components of modern computer networking, enabling devices to communicate and share information seamlessly.


  • Socket Programming in C#: Port Numbers in a computer network2:24

    To understand port numbers, we will use an analogy.

    Let’s consider that your PC is like an apartment building.

    This building can be reached through a specific street address.

    In the case of a PC, it will be the IP address.

    This building is further divided into apartments where a unique apartment number identifies every apartment.

    This apartment number is similar to the port number.

    The computer contains a large but finite number of ports.

    Each apartment in this building is occupied by only one thread or process.

    Only one thread or process can send or receive data from a port number.

    When we need to send data to a specific thread or process, we need to know which port number is being used by it.

    We also need to know the PC's IP address on which the thread or the process is running.

    The combination of an IP address and a port number is known as an EndPoint.

    There are a total of 65536 ports on a computer.

    Port numbers from 0 to 1023 are reserved for operating system usage.

    You can say the building administration occupies these apartments.

    These are called well-known ports or system ports.

    A few examples are port 20 is used for FTP.

    Port 23 for telnet and port 25 for emails using SMTP.

    And port 53 for DNS.

    In socket programming and networking, a port number is a unique identifier that helps to direct network traffic to a specific destination. It is a 16-bit integer that ranges from 0 to 65535. The port number is combined with the IP address to create a socket, which is a combination of an IP address and a port number. This socket enables two endpoints to establish a connection and communicate with each other. The port number is used to identify the application or service that is running on the host. For example, HTTP traffic typically uses port 80, while HTTPS traffic uses port 443. Understanding port numbers is crucial for network administrators and software developers who need to configure firewalls, routers, and other network devices to allow traffic to flow smoothly between hosts.

  • Socket Programming in C#: Fundamentals of User Datagram Protocol5:20

    This video compares UDP to TCP/IP, another popular internet communication protocol. Understanding the difference will help you in doing UDP socket programming.

    The synonym UDP stands for User Datagram Protocol.

    It is a connectionless IP data transfer protocol.

    But… what is connectionless?

    Lemme explain, in a connectionless protocol, the sender and the receiver don’t need to establish a connection before they can start a data transfer.

    For example, if you want to talk to a stranger in real life, you must shake hands and introduce yourself before starting the conversation.

    This is the way TCP/IP protocol works.

    If you and the stranger were computers communicating through UDP, you wouldn’t need to shake hands and have an introduction. You can start talking.  

    The heart of user datagram protocol is “the datagram.”

    It is the basic unit of data transfer in the UDP

    A UDP datagram contains information about the IP address & port number of the host machine which is supposed to receive it and a payload that has to be delivered.

    Everything is in binary format and… represented by bytes.

    A host computer sends a datagram as one unit, and it is received by the target computer as one unit.

    A good example will be like putting a letter in an envelope, writing an address and port number as well on the envelope.

    Giving it to the postal service so that the intended receiver would pick it up.

    An envelope here is the datagram.

    Now let’s talk about a unique UDP capability, Datagram Broadcasting.

    Since UDP is a connectionless protocol, as I explained earlier.

    No handshakes are needed.

    A host can send a packet to all hosts in the network on a specific port number at once in broadcast mode.

    This would be akin to picking up a megaphone and talking to everybody in a hall simultaneously.

    In the case of broadcast, The IP address part of the datagram is set to all 1s, and a port number is also supplied.

    Any other host in the same local area network listening on the specific port number can receive this packet.

    It will be akin to putting a letter in an envelope and writing the address “everybody ” on it.

    UDP broadcast is a feature used by many network protocols, such as Simple Network Monitoring Protocols or SNMP.

    I must also tell you about some other important details of the UDP protocol.

    First, the delivery of a datagram is not guaranteed in UDP.

    This means the network will not be flooded with re-transmission packets if the client does not receive a packet.

    That’s why UDP is used for video and audio conferencing and IP TV applications.

    A few missed packets will cause some squeaks in the voice or maybe a few distorted pictures.

    But the network won’t be congested with additional packets to ensure you don’t miss a frame while binging on Netflix.

    This is different from TCP/IP, in which case there’s an elaborate system of acknowledgments and retransmissions to ensure that all data sent is delivered successfully.

    Secondly, the arrival sequence of datagrams on the receiver can be different from the sending sequence.

    These characteristics make UDP a faster protocol than TCP/IP.

    Anyhow, both TCP/IP and UDP have their areas of application.

    Their strengths and weaknesses.

    And one size doesn't fit all in this case.

    Since we’re already talking about TCP/IP protocol, it is worth mentioning that in the case of TCP/IP protocol, the sender and receiver must establish a connection before they can start a data exchange.

    TCP/IP can’t do broadcasting and multicasting.

    Multicasting happens when a sender can send one datagram to multiple well-known hosts; this is useful in IPTV scenarios.

    Now that you got the basics covered about network programming. Let’s move on to the next section, where we’d crank some code.

Requirements

  • Visual Studio 2017
  • C# .Net
  • Programming

Description

Welcome to this tutorial on UDP socket programming! This guide will explore the fundamentals of User Datagram Protocol (UDP) socket programming. Whether you are a beginner or an experienced programmer, this tutorial will provide you with a comprehensive understanding of UDP socket programming and how to use it to create efficient network applications. So, let's get started!

UDP, or User Datagram Protocol, is a layer-4 protocol in the OSI model. It is part of the transport layer along with TCP (Transmission Control Protocol). UDP is a connectionless protocol that provides a simple way to send packets of data without the need for a connection. It is often used for applications where speed is more important than reliability, such as video streaming or online gaming. While UDP does not provide any error-checking or retransmission of lost packets, it is still an important part of the OSI model and plays a vital role in many network applications.

This course teaches C# .Net socket programming with UDP sockets in a hands-on and easy fashion. It is a carefully planned and crafted online course that takes the student by the hand and teaches them how to create quality distributed network applications using C# .Net step by step.


In socket programming, connection-less sockets, which include UDP sockets, are often used for applications that require fast and efficient communication. Unlike connection-oriented sockets, which establish a dedicated connection between two endpoints before any data is transmitted, connection-less sockets send data packets to the intended recipient without any prior setup. This makes them ideal for applications prioritizing speed over reliability, such as real-time video streaming or online gaming. However, because there is no guarantee that the packets will arrive in the correct order or even arrive at all, developers must take extra precautions to ensure the accuracy and completeness of their data.


The first sections of this course cover the basics of computer networking and UDP socket programming in CSharp dot Net necessary for network and socket programming.


The following two sections will teach how to send and receive UDP broadcast in a command prompt C# .Net application using synchronous sockets with method calls such as Socket.Send and Socket.Receive. You will also learn how to convert between string and byte data types.

Once you're familiar with distributed network applications, the course takes you toward asynchronous socket programming. You will also learn about callback methods and how you can implement callbacks in C# .Net. I will also show you the publisher-subscriber model, AKA pub/sub model. We will implement the pub/sub model in our library project and the client/server applications using the library as well. We'll create event handlers and delegates and raise events as well.

We will use an Object Oriented approach for this course, following the principles of OOP. The next section will implement a publisher/subscriber model in C# .Net to bridge the gap between the API solution classes and the WinForms-based front end.

Next, you will learn JSON (JavaScript Object Notation) and how to add JSON functionality to your C# applications with Newtonsoft JSON .Net. Finally, we'll start implementing an application-level communication protocol in this section.

In the next section, you'll learn how to transmit binary data over a UDP socket in C# .Net. Then, for example, I will show you how to transfer png image files between multiple clients of your applications through the server. Again, the JSON-based protocol implemented in the previous section will play a key role here.

After watching this short C tutorial course, you can write your own distributed applications that communicate through the network, either WiFi or Ethernet. You will have the foundation knowledge needed to create C# .Net-based applications for IoT, computer telephony, VoIP, online gaming(e.g., Unity), and more.

This course also shows you how to use String Interpolation, a C# 6 feature. You'll also learn how to debug network applications in Visual Studio and write code faster.

You don't need to learn C to work on this course.[Related: Socket Java, python socket UDP]


Pros and Cons of UDP

UDP (User Datagram Protocol) is a simple and fast network protocol that is commonly used for real-time applications such as video conferencing, online gaming, and streaming multimedia content. However, it also has some drawbacks that should be considered.


Pros:

- Speed: UDP is faster than TCP because it doesn't have the overhead of error checking and retransmission of lost packets.

- Low latency: UDP is a connectionless protocol, which means that it doesn't establish a dedicated connection before transmitting data. This results in lower latency and faster response times.

- Simplicity: UDP is a simple protocol that is easy to implement and maintain.


Cons:

- Unreliable: UDP is an unreliable protocol because it doesn't guarantee the delivery of packets. This means that some packets may be lost or arrive out of order, which can affect the quality of the application.

- Security: UDP doesn't provide any built-in security features, which means that it is vulnerable to attacks such as packet spoofing and tampering.

- Congestion: UDP doesn't have any congestion control mechanisms, which means that it could potentially overload a network with too much traffic.


Who this course is for:

  • Beginner/intermediate C# .Net developers working to complete a socket programming & network communications project
  • University students learning distributed application programming