Buying for a Team? Gift This Course
Wishlisted Wishlist

Please confirm that you want to add TCP/IP Socket Programming in C# .Net For Coders & Students to your Wishlist.

Add to Wishlist

TCP/IP Socket Programming in C# .Net For Coders & Students

Learn network programming quickly, go from novice to expert in little over one hour
4.4 (164 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
5,378 students enrolled
Created by Naeem Akram
Last updated 1/2017
$15 $20 25% off
30-Day Money-Back Guarantee
  • 1.5 hours on-demand video
  • 7 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
Have a coupon?

This course helps you to learn socket programming and become a better professional programmer.

Each video lecture is focused on one important aspect about socket programming & network communication. Concise code examples are supplied, and a demo after every couple of lectures will show you practical implementation of the concepts described earlier.

By the end of this course, you will be able to create C# .Net software capable of sending and receiving data over TCP/IP sockets on peer to peer basis in an asynchronous fashion. The course will make you a better programmer

Why take this course?

You should take this course if you're a professional(or student) with some coding experience in the past but lack understanding of how computer networks work on a software level.

You will learn techniques which are useful in real life scenarios commonly faced by programmers.

Section 1 contains very useful information anybody can benefit from, whether they're familiar with C#.Net or not.

Minimum upfront theory

A lot of courses tend to pile up theory ahead of the real code, this course is going take a minimum theory first approach.

You will learn essentials of network programming and start writing code in under 15 minutes.

Use of Windows Forms

From start to the end this course will use Windows Forms, an approach different from many others taking the command line route. This teaching methodology makes the course lot less boring and non-classroom like, practically useful and really suitable for professionals.

Short, sweet, to the point

Yes sir, entire course is designed with the busy professionals in mind and the videos were created to make your online learning experience fruitful and easy.

Asynchronous Sockets, modern real world solution

In traditional socket programming scenarios, (multi) threading is use to create a responsive server or client. I have bypassed that route and shown you how to use asynchronous sockets directly.

Reinforced learning

Each section contains a quiz on the end which is very helpful to ensure that you review and retain important bits of information imparted in relevant course section.

Open Source Project

All source code shown in this course is also made available as a Git repository through GitHub. The Visual Studio 2010 C# solution is out there! Students can create their own branches, and push changes back for community benefit.

Join an active community

Become a part of the 4000 + programmers who have already taken this course. Your questions will be answered by the fellow students and the course instructor as well. Really a wonderful place to start learning!

Learn something new

Sockets are considered an advanced topic, a danger zone in programming parlance. Knowing it means you take your trade seriously.


You will also learn how to implement various sanity checks using tryparse and try/catch.

Who is the target audience?
  • Take this course if you have tiniest C# or Java programming experience
  • Network communications is a very interesting topic, if you're looking to study something off-beat go for this course
  • Don't take this course if you have never written code
Students Who Viewed This Course Also Viewed
What Will I Learn?
Create enterprise grade TCP/IP socket communication software
Develop a sound understanding of how networking applications communicate
View Curriculum
  • C# .Net programming language, basic skills
  • Visual Studio 2010, 2012, 2013, 2015 or above will be needed to write code shown in this course
  • A PC which is connected to a network, be it your home WiFi
Curriculum For This Course
Expand All 29 Lectures Collapse All 29 Lectures 01:35:44
Essential Networking Theory
8 Lectures 18:25

This course is for programming professionals who have some programming experience but never tried network programming

Many courses and books about network & socket programming pile up tonnes of theory before the real code. That's not the way programmers do it. I have tried to take a different approach and we will get started with only the essential theory and jump into code ASAP.

I am a rookie coder, I've been out there for so many years and written a lot of code (C++, C# .Net, Java) until so far. Because of my approach, will be in a sound position to write software capable of sending and receiving data over the network before the end of this course.

Preview 03:20

The Host

A computer network is made up of hosts, which are also called nodes. A host can be a laptop, a smart phone, a router, or anything and everything that is capable of connecting to the TCP/IP network.

Preview 01:25

An Internet Protocol Address is also called IP Address for short. Every host(computer/phone/router etc.) in a computer can be reached by a unique IP Address.

We will use IP addresses conforming to Internet Protocol Specifications version 4 AKA IPv4, a newer version of IP specifications called IPv6 is also available but it is catching up with the widely used IPv4.

An IPv4 address is basicallly a group of four 8 bit numbers. Each number in the group can have a value between 0 to 255.

When printing an IP address the 4 numbers mentioned above are seperated dot character . to make an IP address easily readable.

Preview 01:04

Note: You may watch the video first and take a look at the description later on if you forget something.

We will use command prompt to find the IP address of your computer.

ipconfig /all is the command which will be used for this purpose. You will need to launch the command prompt and type tie command, then press enter key. All information about the IP configuration of your PC will be dumped on the console as a result. It includes the host name and IPv4/IPv6 addresses of your PC along with the MAC address of various network cards.

If you are connected to the network using Wi-Fi search for "Wireless LAN Adapter Wi-Fi" text on command prompt window. The IPv4 address printed under that heading is the IP address of your PC.

If you are using an ethernet cable, the IP address will be shown under Ethernet group.

It is going to be helpful for later usages to dump IP configurations of your PC to a text file. The syntax of IPConfig command is going to be little different, and its shown below

ipconfig /all > ipc.txt

Now you will notice that nothing gets printed to the screen this time. That's because everything is saved into the text file ipc.txt which we mentioned after > character. If you will type the name of the text file, it will be opened in Notepad where you will be able to see all information about IP configuraiton, search it and copy/paste useful bits around.

How to find your IP Address manually? - Networking Essentials

Note: You may watch the video first and take a look at the description later on if you forget something.

In order to understand port numbers we consider that our PC is an apartment building. This apartment building can be reached using a specific street address. In our case it will be an IP address.

The apartment building is further divided into apartment numbers. Every apartment is identified by a unique apartment number.

Just like apartments a computer contains a large but finite number of ports, each identified by a numeric value.

A single port can be used to read/write or send/receive data by only one process at any time.

When we need to send data to a software process running on another computer or even on the same computere we need to know the IP address of the remote peer and the port number being used by the peer software process.

A combination of IP and port is called an EndPoint.

There are total 65536 ports on a computer. Port numbers from 0 to 1023 are reserved for operating system usage. These are also called well-known ports or system ports.

The Port Numbers - Networking Essentials

Note: You may watch the video first and take a look at the description later on if you forget something.

This lecture explains how a server process and a client process work together using TCP/IP stream sockets.

The client and server are two separate processes which might be running on two different computers, or on the same computer.

The server proess must start first and perform an accept connections operation. It will use a specific IP Address & port number AKA EndPoint for this purpose.

The client process will be started afterwards. In order t connect with a server the client process will need to know the IP address and the port number on which the server is listening for incoming connections.

If an attempt to connect fails, an exception will occur in client process.

Connection attempts can fail for various reasons, common reasons of client/server connectivity failure include

  1. Windows Firewall blocked either the client process or the server process
  2. The IP address supplied a server IP address to the client is incorrect.
  3. The port number supplied to the client process as server port number is wrong
  4. The server process was not started
  5. The server process was started but it crashed

Once a connection is established, both client and server can perform read and write or send and receive operations in order to send and receive data. Remember, a peer(server or client) can perform a write data or send operation only when the other end has already performed a read data or receive operation.

The client and server can receive and send data to each other as long as both are running and a network connection is available.

The client or server might close the connection whenever they want. A client or server may exit for any reason.

If one end goes offline, the other end will receive an exception.

Preview 03:39

This video shows you how to enable Telnet Client Windows utility, telnet will play an instrumental role in section 2 of this lecture.

Telnet is a feature of Windows OS, its disabled on most machines but users might enable it whenever they want.

Preview 01:18

Its a good idea to make sure that the students have clearly understood the concepts presented in the first section.

When going through this quiz students will have to go back to various lectures and search for the right answers, which will help them in the advanced coding lectures from next sections.

Please take this quiz, it will take only few minutes and it will be very helpful going forward.

Test your Networking Essentials knowledge
11 questions
Creating a TCP/IP socket server
9 Lectures 33:20

In this lecture we are going to setup a project in Visual Studio which will be used throughout the course. Contrary to most tutorials, you will notice that I am using a Forms application. It will give you a feel that you are building something "real" and it will also make things easier overall.

Important namespaces for socket and network programming are given below:



Preview 04:07

System.Net.TCPListener is used to listen for incoming connection requests. We will initialize the TCPListener in an onClick event of a button, it will need a valid IP address and port which will be passed in through the form fields.

This lecture will also show you how to implement some sanity checks to make sure that correct values are supplied. We will use the method IPAddress.TryParse to ensure correctness of IP address, and int.TryParse for port number.

Preparing a TCPListener

We will call the asynchronous method beginAcceptTcpClient of TcpListener class. A method from our code will be passed as a parameter, when a new client will get connected to your server process this specific delegate method from your code will be called by the system.

This video will show you how to define the callback. You will see an example of asynchronous programming, which will let your program to run smoothly in non-blocking fashion while the system is still listening for incoming connection requests over TCP/IP socket.

Listening for incoming connection requests

- Important: Always remember to turn Windows Firewall off, or allow your applications through it by adding an exception to the firewall settings. Otherwise the Windows firewall will most probably blog your socket program from sending/receiving data over the network.

This video shows you that you have already got the seeds of a network application in place.

You will run the server code written so far, you will run telnet utility as a client and try to get it connected with your server process.

Preview 02:47

Note: The description is for reference purpose only, you may read it after watching the video.

The two classes given below are very important in our course:

  1. System.Net.Sockets.TcpListener
  2. System.Net.Sockets.TcpClient

We have seen that the server role is wrapped the TcpListener class, and we have used to to accept incoming connection requests.

Similarly, the client role is wrapped in TcpClient class.

These classes encapsulate the sockets, end points, and streams which are needed to communicate over the network.

These classes make our lives easier by hiding a lot of mundane details.

We will use an object of TcpClient to receive data from a peer. An object of TcpClient will be returned by a EndAcceptTcpClient method call inside the onCompleteAcceptTCPClient method.

It is a good practice to use try/catch whenever you can, a lot of trouble and client side embarassment can be saved by a simple try/catch block.

Getting hold of a TCPClient object

Note: The description is for reference purpose, you may read it later on to refresh the things learned in the lecture.

In this video we will put together the plumbing needed to receive data from a socket client.

We will use an async method call BeginRead to read data from the client data stream. We will also define a callback delegate method which will be invoked by the system when some data is received on a client stream.

All data received over the network is in byte form, we will store the received data in a byte array.

The number of bytes received are returned by the EndRead method call.

If the number of bytes received is zero, it means that the other end of the connection is closed.

The method call Encoding.ASCII.Getstring method to convert the received bytes into string which could be printed.

Once the read operation is completed, we clear the buffer and start another read operation so that the client could send more data.

We will run the code in next video and demonstrate that our socket code is working.

Reading data from a TCPClient network stream

This video demonstrates an example of reading data from a TCP/IP socket client. Telnet utility is used for this purpose.

Telnet is a character based interface and as soon as a charater is typed it is sent over to our server application.

In case you have more than once PCs, you can try to run the server application we have created so far on one PC and the telnet utility on another PC.

If we will close the telnet utility first, our server program will show a messagebox to inform the client as well.

Preview 02:00

Note: The description is for reference purpose, you may read it later on to refresh the things learned in the lecture.

The network write or send operation is considerably simple to socket read or receive operation.

We will add a text box and a button which will be used to send data.

Please note that the data is sent in a byte form.

We don't try to send data if the supplied string is null or empty and we use string.IsNullOrEmpty method for this purpose.

We also check if mTCPClient is not equal to null and if the underlying socket represented by TcpClient.Client is connected or not by reading TcpClient.Client.Connected boolean property.

The text is converted into bytes using static method of class Encoding.ASCII which is Encoding.ASCII.GetBytes.

A simple call back method onCompleteWriteToClientSteam method is also defined which only calls the method EndWrite. A try/catch safety net is prsent even in this method.

Writing data to a TCPClient network stream

We will build and run the code which we have written so far.

This video will show you how to exchange data between your server and Telnet client utility.

Your TCP/IP socket server is now capable of handing incoming connection requests and send/receive data to/from a client.

The client and the server can exchange data in this fashion as long as they want and an underlying network is available.

Our server performs the key networking tasks, these are:

  1. Listen for incoming connection requests
  2. Send data to a client
  3. Receive data from a client
Preview 02:38

This quiz is intended to enforce the concepts and tools students have learned during section 2.

Creating a TCP/IP Server
11 questions
Creating a TCP/IP socket client
6 Lectures 17:36

Note: The description is for reference purpose, you may read after watching the video.

We will create a form almost identical to the form used in section 2. In this lecture we will add various controls like TextBox, Button, Label to the form.

I will also rename the objects for ease of use and a cleaner code.

Naming controls and objects properly is a very good approach and it makes your code self explanatory in many cases. The approach is certainly not an alternate to adding proper comments to your code, it is a complimentary technique which makes your code even more easily understandable.

Preview 01:34

We will add the namespaces which provide network and socket capabilities to our code. These .Net namespaces are:



An object of TcpClient will be defined as well. We will also add an event handler for btnConnect click event. After this we will add a callback/delegate method that will be called upon end of a connection attempt.

The method TcpClient.BeginConnect allows us to connect with a server process. It will be called in btnConnect_Click event handler method.

Connect to a TCP/IP socket server

The text from a TextBox named "tbPayload" will be sent to a server when the send button will be clicked on our WinForm.

Please note that we must be connected to a server process in order to send data.

An array of byte will be used to hold the bytes equivalent of the string supplied through the Textbox.

ASCII text conversion to byte form is done using GetBytes static method of Encoding.ASCII class.

We will use following method call to send or write the data


A very simple callback delegate method will be written to handle completion of write method as well.

Send data to a TCP/IP socket server

In this video we will dmonstrate the TCP/IP socket client functionality of the code we have written so far in section 3.

Disable Windows Firewall or add an exception so that TcpServer01 and TcpClient01 can use network to transmit and receive data.

We will need the TCPServer01 software that we created in section 1. You can download the exe from the "Downloadable Resources" section of this video or run the project through code written in section 2.

First of all we will run TcpServer and click " supply the IPv4 Address of our PC and port number, we will click "Start Listening" button after these steps.

Once the server is running and listening for incoming connections, we will run our TCPClient project. The IP address and port number of server will be supplied to the client and "Connect" button will be clicked.

Client will be able to send any amount of data it wants to the sever.

The server will not be able to send data back to the client since the client has not implement the TCP/IP network socket stream read operation until so far.

Preview 02:08

In this video I will show you how to implement TCP/IP network socket stream read operation in order to receive data from a TCP/IP client over the network.

The technique to read data on client side is exactly the same as reading data on the server side. Remember we implemented data receive operation in section 2 of this course.

We will define an array of byte and call it mRx as a member variable of our TCPClient01 form class.

The logic of reading data will be added to onCompleteConnect callback method.

The method TcpClient.GetStream().BeginRead will be used to initiate data read operation.

Some commonly used sanity checks and a try/catch safety net is going to be present as usual.

Once a read operation is finished we will start another read operation right away so that the server can keep sending more data to the client.

After going through this lecture we have completed a TCP/IP sockets based server and a client.

The duo can exchange data over the computer network as long as the two want.

We didn't use bare-bone sockets in this course. Instead, I showed you how to use .Net Framework utility classes TcpClient and TcpListener. Using these helper classes makes a developer's life easier and development much faster.

Receiving data from a server

This is a very exciting demo since we are going to see the stuff that we have built so far in action.

First of all let us run the TCPServer01.exe, it is available in downloads section of this lecture.

Once TCPServer is running and listening for incoming connections we will run TCPClient and connect it to our TCP/IP Server.

Data exchange between the TCP/IP network socket client and server will be shown and explained in this video.

Preview 01:55
Common Network Programming Topics
6 Lectures 25:23

A loopback address is a special IP address, is most commonly used loopback address.

This video provides an easy explanation of loopback address.

IPv4 addresses from to are reserved as loop-back addresses. It is an implementation detail guarded by various International Engineering Task Force (IETF) standards.

A set of loop back addresses is defined for IPv6 as well but that is not relevant to our course.

A loopback address is used to ease communication and data exchange between a client and a server process if both are running on the same computer.

By using loopback address the client/server don't need to know the exact IP address of their peer and they can communicate seamlessly through the loop-back address.

There is one tricky scenario worth mentioning.

If the client and server are running on same machine but the server is listening for incoming connections on the IPv4 address of the computer instead of the loopback address, the client will not be able to get connected to the server using the loopback address.

The server must be listening on the loopback address if it wants the client to allow to connect with it using loopback address.

Please note that a server running on loopback address is not accessible to other clients on the network.

This video shows you how to use the loopback address to send and receive data between a client and a server.

Preview 03:16

This video lecture shows code for finding an operational IPV4 address of your computer. Please note that this might not be the best method to do the job, someone might have a better technique but that is not the point. We are just trying to understand computer network programming concepts in a quick and practically feasible way in this course.

Finding IPv4 Address using Code

On the server side, it is possible to handle more than one clients at once. Web servers like Apache and Microsoft IIS do it all the time. Traditionally we would start a separate thread manually for this purpose. In our case, we are using asynchronous sockets which give us an advantage. We don't need to start and manage threads manually to handle socket communication or to listen for new requests. That part is handled by the .Net framework on our behalf.

In real world scenarios, you will need to be careful with managing concurrency around shared data though(using semaphores, lock, and monitors).

Handling more than one clients connection requests on server

The most asked for video is finally out. I hope you'll find it useful.

You are going to learn how to maintain a list of currently connected clients and how to send them data and how to receive data.

I have recently create a course title "Windows Service Programming in C# .Net for Coders & Students". I believe it will be useful for most of you. A discount coupon link is given below, grab it before the coupons run out:

Bonus: Maintaining a list of clients to handle multiple clients on your server

In this exciting demo, you are going to see the server capability to handle multiple clients at once. You will also see how to handle client disconnections. Our server is now capable of sending data back to specific client node.

The URL of my new course "Windows Service Programming in C# .Net for coders & students":

Preview 04:51

Listen for incoming connections on "Any" IP Address
1 page
About the Instructor
4.4 Average rating
225 Reviews
10,582 Students
2 Courses
Sr. Software Engineer

I am A person with many talents and interests, ranging from gardening to programming and poetry to cooking.

I've been writing computer software code(C++, C# .Net) since 2006, creating awesome Android apps since 2012. I've been working as a freelance software developer since Nov. 2012.

I've extensively worked on network communications, computer telephony integration(CTI) using Microsoft TAPI 2.x and 3.x, and LDAP server programming(Active Directory). Android app development has been my focus as a freelancer, I've created and published several apps for clients and a couple for myself on Google Play store.

I've been sharing my knowledge through my blogs, and I am confident its time share more things that I've experienced with the world in a comprehensive video format.

Report Abuse