Currently Empty: $0.00
- Description
- Curriculum
- FAQ
- Reviews
Network and socket programming tutorial in C# .Net using TCP
Get proficient in computer network socket programming using TCP/IP streaming sockets and become a better professional programmer. This course will start you with TCP IP network programming with C# dotnet socket library and Dotnet fast.
Each video in this course covers an essential concept of client-server socket programming & network communication. The ready-to-use C# code examples are supplied in the Visual Studio solution form to download. After every few lectures, a demo will show you the practical implementation of the concepts described earlier.
By the end of this course, you will be able to create C# (Sharp) .Net software capable of sending and receiving data over TCP/IP sockets on a peer-to-peer basis with async and await keywords. You will learn not only socket programming but async/await keywords as well. The course will make you a better programmer.
TCP/IP is a protocol suite that provides reliable, end-to-end communication over the Internet. It is not a layer in the OSI model but spans multiple layers, including the network layer (layer 3) and transport layer (layer 4). The TCP/IP protocol suite is designed to work on top of the underlying network infrastructure, allowing different network technologies to communicate with each other. While it doesn’t fit neatly into the OSI model, TCP/IP is widely used and is critical in modern networking.
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 an understanding of how computer networks work on a software level(either in C#, Java, or C++).
You will learn valuable techniques in real-life scenarios commonly faced by programmers.
Many students of distributed application programming university courses have taken this course in the past and posted positive comments in reviews. They could quickly complete their assignments on their own after watching this course. This is the best socket programming course on Udemy.
Section 1 is free; it contains useful information anybody can benefit from, whether they’re familiar with C#.Net.
Minimum upfront theory
Many courses tend to pile up theory ahead of the actual code. This course is going to take a minimum theory-first approach.
You will learn the essentials of network programming and start writing C# code in under 15 minutes.
Use of Windows Forms and class library
TCP IP socket programming in C Sharp on Windows is the focus of this course. For example, this course will use a class library project in C# to show client/server applications in WinForms. This is an approach different from many other courses that use command-line projects. My teaching methodology makes the course much less boring, non-classroom-like, practically advantageous, and suitable for professionals.
Short, sweet, to the point
The entire course is designed with busy professionals in mind, and the videos were created to make your online learning experience fruitful and easy. It is project-based training.
Asynchronous programming with async/await keywords, a modern real-world solution
async/await keywords were introduced in C# 5.0. In traditional socket programming scenarios, (multi) threading creates a responsive server or client. I have bypassed that route and shown you how to use asynchronous sockets directly. I first explain to you what async/await keywords are. Then I show how to use these for non-blocking network I/O. This part of the puzzle is the key to high-traffic enterprise applications.
Reinforced learning
Each section contains a quiz at the end, which is very helpful to ensure that you review and retain essential bits of information imparted in a relevant course section.
Join an active community.
Become a part of the programmer community who has already taken this course. Your fellow students will answer your questions, and the course instructor as well. A wonderful place to start learning!
Learn something new
Sockets are considered an advanced topic, a danger zone in programming parlance. However, knowing it means you take your trade seriously.
Object Oriented Programming
This course teaches you how to create a distributed application using the principles of OOP. You will bridge the gap between the back-end C#.Net class library and the front-end WinForms application by implementing the Publisher/Subscriber model based on EventHandler classes. This course also shows you what event handlers are and how to create your event.
Bonus
You will also learn how to resolve a hostname to an IP Address using System.Net.DNS class. You’ll also implement various sanity checks using tryparse and try/catch.
You don’t need to learn C to work on this course.
There are a few key things to remember regarding TCP/IP socket programming in C#. First and foremost, it’s important to understand the basics of how sockets work and how they can be used to establish network connections between applications. In C#, you can use the Socket class to perform socket programming tasks and interact with network sockets. This class provides a range of methods and properties that allow you to create, connect, send, and receive data from sockets. With a solid understanding of the Socket class and its functionality, you can create robust and reliable network applications that communicate seamlessly with other applications over the internet.
TCP/IP, or Transmission Control Protocol/Internet Protocol, is the primary protocol used for communication on the Internet. Here are some pros and cons of using TCP/IP:
Pros of TCP/IP Sockets:
– TCP/IP is a widely adopted protocol, meaning that it is compatible with many different devices and networks.
– It is a reliable protocol that ensures data is transmitted accurately and in the correct order.
– TCP/IP can handle large amounts of data, making it suitable for use with high-bandwidth applications like video streaming.
Cons of TCP/IP Sockets:
– TCP/IP can be slow, particularly when compared to other protocols like UDP.
– The protocol is not particularly secure, meaning that data transmitted using TCP/IP is susceptible to interception and tampering.
– TCP/IP is relatively complex, meaning that it can be difficult to implement and maintain.
This course is related to Socket Java, Python socket, and UDP.
Socket Programming in C# For Beginners
-
1Introduction to TCP/IP socket programming in C# .Net using Visual Studio
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.
-
2Downloading Source Code
-
3The Host In Computer Network - Networking Essentials
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.
-
4IP Address In Computer Network - Networking Essentials
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.
-
5Port Numbers In Computer Network Socket Programming- 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.
-
6Client/Server Model as used in TCP/IP Stream Sockets - 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
- Windows Firewall blocked either the client process or the server process
- The IP address supplied a server IP address to the client is incorrect.
- The port number supplied to the client process as server port number is wrong
- The server process was not started
- 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.
-
7Enable the Telnet client utility - Networking Essentials
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.
-
8Test your Networking Essentials knowledge
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.
TCP/IP Server Asynchronous Socket Programming With async & await Keywords in C#
-
9Server Side TCP/IP Socket Programming C# .Net Project Setup in Visual Studio
The goal of this video is to get started with the server side of a TCP/IP client server arrangement.
We will create a program which would accept an incoming connection on the IP address of the PC on which it is running.
We are going to use port number 23000.
I will show you the steps needed to call the Accept method.
Please be informed, that this part of the course does not demonstrate production grade code.
The purpose of this section is to familiarize you with how sockets work in principal.
The production grade code will be shown in later sections of this course.
This object will provide us a means to receive data from and send data to the client PC which just got connected.
Let’s head over to Visual Studio
You can use an older version of VS.
I am just showing off my cool new software
We are going to create a C# console application to achieve the goal described earlier.
I’m going to create a new Windows Console type application and name it “SocketsServerStarter”.
First thing we need to do is add the namespaces related to sockets in the program, which are
System.Net;
System.Net.Sockets;
Inside the main method, I’ll create an object of Socket class.
Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
The first parameter means that it’s an IPV4 socket. The other two parameters are self describing.
Next thing, I will create an object of IPAddress.
I’ll assign it the value IPAddress.Any
This means that our socket will be listening for incoming connections on any available IP address on this PC. That can be the loopback IP address 127.0.0.1 or the current IP address shown by the IP config command.
Please don’t get confused by this statement, it will be clarified in next video.
Next, let’s define an IPEndPoint, which is a combination of an IP Address and a port number.
IPEndPoint ipep = new IPEndPoint(addr, 23000);
We passed it the IP address we define above and the port number 23000.
After this, we will bind the socket which we created earlier to the IP End Point which we just created.
This way, our socket will know what IP address and port will it use for its operation.
After this we will call the listen method.
listenerSocket.Listen(10);
The parameter tells the system how many clients can wait for a connection at anytime while the system is busy.
In order to make something happen, we need one final step.
That is to call the Accept method on our socket.
listenerSocket.Accept();
Accept is a blocking operation. It is a synchronous operation.
- Our program will not move forward until this operation is finished.
-
10Demo - Accept Incoming Connections on Socket in C# .Net Using Telnet Client
In this video we run the server side socket program which we wrote last time. We use Telnet Client utility to connect with a server and put a debugger break point to see the effect of our code.
-
11Receive Data on a TCP/IP Socket in C# .Net
-
12Using Encoding.ASCII.GetString to Convert Bytes To String for socket transfer
In this video I show you how to convert a byte array into an ASCII string.
-
13Socket Programming: Send Data on a TCP/IP Socket in C# .Net
Sending data back to a client is fairly simple
We need the the client socket to do it.
Please note that we will send data back in byte format.
We can simply echo the stuff sent to us by the client.
In that case, we will be able to reuse the byte array buff along with the local variable client.
The will be client.Send(buff);
And let’s put this part of our code in a while loop.
Right above numberOfReceivedBytes I will add a while(true) and an opening bracket.
And right after client.send I will add the closing bracket
Let us clean the byte array buffer so that I can receive fresh data every time.
Array.clear(0, buff.length);
numbereOfReceivedBytes = 0;
One last thing, there has to be some way to end this infinite loop here. Right?
So, right after client.send(buff) I am going to add an if statement.
if(receivedText == “x”) {break;}
And we’re done here.
Let’s take a look at the change we made again.
In the next video I’ll demonstrate it. And after that I will show you how to deal with the firewall.
And how we can send or receive data to and from another device, like my Android smart phone.
Do watch the upcoming videos, they’ll be really juicy!
-
14Socket Programming Example Demo - Send and Receive Data on TCP/IP Sockets in C#
"Nothing is more fun than watching your own code run." Of course this is the height of my poetic skills.
In this video we will run our server side script and see how it is going to rock and roll by by echoing data back to the client.
-
15Demo - Communicate between C# desktop & Android app, configure Windows Firewall
Windows Firewall is a security component of Windows Operating System.
If it is turned on, it allows only a specific set of application software to use network for communication.
In case of our socket application, if it tries to communicate with an app running on another device over the network, Windows Firewall is going to block it right there. Because it is turned on at my PC.
The firewall works based on user defined rules.
We can tell it using rules what applications to block and what to allow
Let us open Windows Firewall and add rules to allow our sockets application to communicate.
Open Control Panel
Search for “Firewall”
Click the Windows Firewall link
Click “Advanced Settings” link
Click “Inbound rules”
Click “New rule”
Select “Program” radio button and click “Next”
Click “Browse” button
Go to the path of our project debug folder and select “SocketServerStarter”
Click “Next” and select “Allow the connection”
Click “Next”, don’t change anything and click “Next” again
Supply a name here and some description too. Click “Finish”
Now let’s click the “Outbound” item in tree and add a rule here too.
It is possible to send data to this machine from a telnet terminal running on any other machine on the same network.
For example, we can use another PC for this purpose or my Android smartphone.
Let me show you my phone screen.
I have installed this free app on my phone.
Its name is “Telnet Client”. And no this is not a sponsored advertisement!
Here’s their Google Play store listing
I am going to open this app
On my laptop, I will run our sockets application
It will show an “Allow Access” dialog first time. Click the button “Allow Access”.
And I will click the button “Connect”
In here, I need to supply the IP address of my laptop.
The IP address keeps changing because it is assigned by the DHCP in my home router.
Let’s find it quickly.
So the IP address is 192.168.10.5 and in port number I will put 23000
Over here you can see I can simply send anything I like.
This telnet client is different from Windows telnet client in that we can send more than one bytes of data here.
Let’s have a conversation here.
Looks cool
Now I’m going to go to our server window and type Ctrl C to close the application.
One last thing,
The name of my PC is shows NWING in the IP configurations here.
Instead of looking for the IP address again and again, I can use this piece of information here.
For example. Let’s run the telnet client app on Android again and try connecting to NWING on port 23000.
- Here you see, it works like a charm!!!
-
16Introduction to Client Side TCP/IP Socket Programming in C# .Net
Working with sockets on the client side is fairly similar to to the stuff we have seen in the server side example.
Let me tell you some of those before we start cranking out code. [change slide]
One major difference is that the client sockets don’t do the bind/listen/accept method calls.
Instead, the client sockets perform a connect method call.
In case of TCP/IP, the client socket must know the IP address and port number on which it is supposed to connect.
In case of UDP sockets, there are some other possibilities, but that’s not the subject of this course.
If the client and server are running on the same machine, we can use the loopback IP address 127.0.0.1 to make our lives easier.
I’ve done socket programming on Android too, that means I can teach that too if people are interesting.
I’d be curious to know if you would like to see an Android example too
Anyways, I digress. I’m Sorry :)
Let’s open Visual Studio and create a new console application.
I’ll call our application SocketClientStarter
Inside the code, the first thing I’ll do is add namespaces needed for socket programming
Using System.net
Using System.net.sockets
Inside the main method, I’ll define a socket client variable
Socket client =
New socket(
I’ll use the constructor with three parameters
AddressFamily.InterNetwork is for IPV4
SocketType.Stream is for streaming socket
ProtocolType.Tcp is for TCP sockets
We’re going to need the IP Address of our server, let’s define it here and assign it a null for now.
Ipaddrserver = null;
On the next line, let’s define a try/catch block to handle failure scenarios.
catch(Exception excp)
{
Console.WriteLine(excp.ToString());
}
Inside the try block I’ll define a new string and call it strServerIPInput and assign it the value input by user from console.
Let’s give our users a clue about what they’re supposed to do here one line above.
Console.WriteLine("*** Welcome to Socket Client Starter Example by Naeem Akram Malik ***");
Console.WriteLine("Please Type a Valid Server IP Address and Press Enter: ");
Also define a string to take port number input from user.
string strPortInput = Console.ReadLine();
And some help for users
Console.WriteLine("Please Type a Valid Server Port Number(Integer Only, max 65535) and Press Enter: ");
And an int to store the port number int after parsing it.
Now let’s parse the string given by the user into an IP Address.
IPAddress.TryParse(strServerIPInput, out ipaddrserver);
Add an if condition here in case of bad user input.
if(!IPAddress.TryParse(strServerIPInput.Trim(), out ipaddrserver))
{
Console.WriteLine("Invalid server IP supplied.");
return;
}
We’ll do the same for the port number too.
if(!int.TryParse(strPortInput.Trim(), out nPortInput))
{
Console.WriteLine("Invalid port number supplied, return.");
}
The tryparse functions will return false if they fail to parse the supplied strings according to their type specifications.
We also know that a valid port number has to bee greather than zero and smaller than 65535. So, let’s add an if condition to handle that too.
if(nPortInput <= 0 || nPortInput > 65535)
{
Console.WriteLine("Port number must be between 0 and 65535.");
return;
}
Let’s show the stuff on screen too
System.Console.WriteLine(string.Format("IPAddress: {0} - Port: {1}", ipaddrserver.ToString(), nPortInput));
Next, we’ll call the client socket connect method.
client.connect(
We can supply it a new IPEnd point, or use a constructor which takes IP Address and Port directly.
Let’s use the second one:
And supply IP Address & Port
client.Connect(ipaddrserver, nPortInput);
Console.Writeline(“Connected to server.”);
Add a console.ReadKey() on next line.
Please note that this is a blocking call and it will fail after a timeout period if the server is not running.
Let’s try to run it right away without running the server first.
I’ll press control F5
Input IP Address . 127.0.0.1
Input port number 23000
It fails after a brief pause
An exception is thrown on line number 47, that’s where we try to connect with server.
So now let’s go to our server project and start the server first.
So here you see,we’re connected to the server.
No data will be sent/received right now.
We’ll need to add some send/receive logic to our client program to do that.
Now let’s close the server and go.
- You are going to learn just that in the next video, do watch the next video.
-
17Client Side Socket Programming VS Project Setup, using methods Connect, TryParse
Writing some safe code to input server IP and port. Show use of IPAddress.TryParse and int.TryParse.
In the end we perform an Connect method call and see how it works with the Server which we have written so far in this course.
Also show you the poser of Ctrl + . to import missing namespaces into the solution.
-
18Socket.Send() & Socket.Receive() Calls On Client Side TCP/IP Socket in C#.Net
How to perform send and receive operations on client side sockets.
-
19Demo - Run TCP/IP Client & Server network programs, improve Server
Run the final version of code produced in this section and see how it goes. Make minor changes in the client and server to prevent the applications from crashing.
-
20How to cleanup a console application for properly closing the TCP/IP Socket
How to handle closing of a TCP/IP client socket in a console application upon application closing. Using the ConsoleEventDelegate of kernel32.dll through interop.
-
21Section Summary
Summary of the things which we learned in this section.
TCP/IP Sockets On Server Side
§How to setup a server socket(Bind and Listen)
§The IPAddress.Any constant
§Accept a connection on the server
§Using telnet client utility
§How to Receive and Send data
§Encoding.ASCII to convert byte[] to string
§Clearing the byte array
§Setup Windows Firewall allow
connecting to my phone
TCP/IP Sockets On Client Side
§Why we need to know server IP & port
§Parsing an IP Address safely
§How to connect to a server
§How to Receive and Send data
§Encoding.ASCII to convert string to byte array
§How to shutdown/close/dispose a socket
§Using try/catch to prevent crashes
Did do it wrong?
§I’m going too fast?
§Did I miss something, just let me know
§Feel free to ask questions in Q&A section
§Source code in lecture downloads area
§I would love to add more videos here
-
22Introduction to socket programming in C# .Net
A short quiz to reinforce the learning about socket programming in C# .Net.
Async Client Side Socket Programming
-
23Section Intro: Asynchronous socket programming with async await in C# .Net
We are going to get into some exciting production grade stuff in this section.
First of all I will show you how to create non-blocking socket applications using async and await keywords. We’ll start on the server side.
The async/await keywords and relevant methods were introduced in .Net Framework 4.5. So you need a version of visual studio which supports it.
Secondly, we will move on from raw sockets and start using helper classes.
For example, instead of using a socket object to accept incoming connections, I am going to show you how to use a TCPListener.
And instead of using a Socket class object to represent the client, I am going to use TCPClient class object.
I am also going to show you how to use StreamReader and StreamWriter with network streams.
Yet another wonderful thing is that we’ll create a class library now onwards in this course.
This approach will make the end result of taking this course much more useful my for students.
-
24Example: Disadvantage of synchronous I/O in C# .Net
-
25Accepting a TCPClient Connection Asynchronously with await keyword in C#
Let us create a new WinForms Project.
I’ll pick Visual C# => Windows Classic Desktop => Windows Forms App(.Net Framework)
I’ll call it, UdemyAsyncSocketServer
Let’s add a new class library to house the logic of our sockets stuff
Right click the solution in solution explorer and select Add => New Project.
I’ll select Visual C# => Windows Classic Desktop => Class Library(.Net Framework)
I’ll name the library “LahoreSocketAsync”
Lahore is the wonderful city where I live
Click the Class1.cs file and press F2 to rename it to “LahoreSocket.cs”
Click “Yes” when VS asks you if you want to rename the auto generated class too.
First of all, we need to supply an IP address and a port number when a server needs to listen for incoming connections.
Let’s define those in our class.
IPAddress mIP;
int mPort;
Click IPAddress and press Ctrl+.
Click “Using System.Net”
Next, let’s define our first method
I’ll call it
public void StartListeningForIncomingConnection(IPAddress ipaddr = null, int port = 23000)
This method will allow the caller to specify an IP Address and a port.
Default values of these parameters are in place for convenience.
Now I need you to pay attention, just in case you’re doing something else alongside watching the videos.
I do it all the time.
We need to add the keyword async before the return type of our method.
This is mandatory if we want to make an async call inside the body of this method.
We are going to use an asynchronous method of a TCP/IP helper class inside this method.
Inside the method, let’s add some code to see if parameters supplied are ok or not.
if(ipaddr == null)
{
ipaddr = IPAddress.Any;
}
if(port <= 0)
{
port = 23000;
}
mIP = ipaddr;
mPort = port;
Let’s print the final values to the debug output
System.Diagnostics.Debug.WriteLine(string.Format("IP Address: {0} - Port: {1}", mIP.ToString(), mPort));
Now let’s introduce a variable of type TCPListener.
TCPListener is a .Net framework helper class which makes TCP/IP programming on the server side easier.
mTCPListener = new TcpListener(mIP, mPort);
The system is showing us some squiggles, let’s fix them.
Click TcpLiseneter constructor and press Ctrl+.
Select appropriate socket namespace to add.
Click mTCPListener and press Ctrl+.
Click “Generate field in LahoreServer” option
Now let’s call the start method of our TCPListener.
mListener.Start();
Next, we’ll make the async call
mListener.AcceptTcpClientAsync();
If we hover the mouse on the method, we’ll be able to see more details about it.
The usage section below says we need to put the async keyword behind the method call.
If we take a look at the definition of our method, it also shows a green squigly complaining that we need to await this method.
Let’s introduce the await keyword here, right before the accept call.
Now put the return value in a variable too
var returnedByAccept =
Let’s write some information to the debug console too
I’ll put a breakpoint after the acceptTcpClientAsync as well.
We are going to use the value returned by this method in an upcoming video.
The important point you need to understand is that when the .Net Framework will see the await keyword inside a method which has got async in its definition, it will generate some code behind the scene to make sure things happen in an async manner.
Once the asyc operation is finished, your code will resume execution beyond this point.
Now I’ll go back to my Forms project and add a reference of LahoreSocketAsync.
I’ll go to the Form design view and add a button, I will name it btnStartServer
Double click the button
Go to the top and add a using statement to bring in LahoreSocketAsync
Inside the form class, I’ll create a reference of LahoreServer
LahoreServer mServer;
We’ll instantiate this member variable in form constructor
mServer = new LahoreServer();
Now let’s go back to button click method and start accepting incoming connection request
mServer.StartListeningForIncomingConnection();
Let’s run the code in next video and see the affects of the work we’ve done so far.
-
26Demo: Accept TCPClient Asynchronously
Demonstration usage of AcceptTcpClientAsync method call to listen for incoming connection requests for non blocking I/O without spinning extra threads in your code.
-
27Continuously Accept Client Connections, Exception Handling in C# async Method
-
28Use System.Net.Socket.NetworkStream & StreamReader to read data from client
In order to perform async read operation in method TakeCareOfTCPClient, we need to make it async.
Let’s add the keyword async to the method definition
Inside the method, I’ll define a variable of type NetworkStream and assign it null for now.
NetworkStream stream = null;
I’ll also add a variable of StreamReader type too.
Every TCP/IP stream socket has got an I/O stream attached to it.
Just like the stream available in case of console I/O or file I/O.
We will use the StreamReader to read data from the network stream associated with the TcpClient passed into this method as a parameter.
After that let’s create a try catch block
try
{
}
catch(Exception excp)
{
System.Diagnostics.Debug.WriteLine(excp.ToString());
}
Inside the try block, I’ll assign the network stream object a value.
stream = client.GetStream();
I’ll assign the stream reader object a value based on the network stream
reader = new StreamReader(stream);
In order to read data sent from the client through stread reader, we’ll define an array of char
char[] buff = new char[64];
Now we’ll create a while loop
while (keeprunning)
{
}
Let’s write info on debug that we’re ready to read
Debug.WriteLine("*** Ready to read");
After this, we’ll call an async method on stream reader.
We will start with the await keyword
await
reader.ReadAsync(buff, 0, buff.Length);
If we hover the mouse on the call to ReadAsync, we can see that it returns a Task<int> and the usage tells us we can store return value in int.
Let’s define a variable and store the values here.
We’ll also print the number of received bytes to the screen
System.Diagnostics.Debug.WriteLine("Returned: " + nRet);
On the next line, we’ll see if the return value is zero.
When this method returns zero, it means the socket connection has been closed.
In this case, we’ll print a debug log and break the loop.
if (nRet == 0)
{
System.Diagnostics.Debug.WriteLine("Socket disconnected");
break;
}
In case you are wondering “wait, why the Debug.Writeline?”
Rest assured, we’ll take good care of these messages later in this course.
Next, we’ll convert the received character array into a string
string receivedText = new string(buff);
And print it to the debug console
System.Diagnostics.Debug.WriteLine("*** RECEIVED: " + receivedText);
Before we could start the next read operation, we also need to clear the byte array buffer which we use for receiving data.
Array.Clear(buff, 0, 64);
Now build the solution and make sure it compiles
That’s it, in the next video we’ll run our server application with the client application which we created in the last section.
-
29Demo: Read Data On Network Stream and Endless Accept async in C#.Net
Read data from a network stream in C# .Net using asynchronous StreamReader method calls.
-
30How to Handle Multiple Network Clients on Server in C# .Net
-
31Demo: Send Data to Multiple Network Clients from List at Once, SendAll Method
-
32How to Stop Listening for New Connections and Disconnect Client Sockets Properly
How to stop the server and disconnect the client sockets properly
In order to stop the server properly, we’ll need to do two things.
First, stop the TCPListener from accepting new incoming connections.
And second, disconnect all connected clients.
We’ll add a method StopServer to our library to do this.
In this video, I’m going to show you how to do these steps.
Let’s head over to the visual studio
I’ve opened the socket server project
First, I’ll add a button on the Form. I’ll name it btnStopServer and set text to Stop Server.(do form resize and button addition faster).
Double click the button
Inside event handler, I’ll use the object mServer to call a method which does not exist yet.
I’ll say mServer.StopServer()
Now I’ll press Ctrl+. to generate the method and press F12 to go inside of it.
First thing, Let’s add a try catch block.
Inside the try/catch set KeepRunning to false.
Next, if(mTcpListeners != null){}
mTcpListener.Stop();
By doing this, we’ve stopped the listener from accepting new connections.
But, we still have the socket connections open which mean we can still receive/send data.
Let’s go through the list of connected clients and close them one by one. We’ll also remove them from the list.
foreach(TcpClient c in mClients)
{
c.Close();
}
After the loop, we’ll clear the list
mClients.Clear();
We can call this method on the Form OnClosing event too.
I’ll go to the form designer view and open Properties pane.
Click “Events” thunderbolt
That’s it, we’re done. We’ll be able to start and stop our server any time we want after this implemenation.
Let’s run it in the next video.
-
33Demo: TcpListener.Stop and TcpClient.Close Calls to Stop Server
Demo: Stop TcpListener and Close client sockets
I will start our server program and click button “Accept Incoming Connections”
Launch a few clients by pressing Windows + R and supplying the telnet command.
You can see we’re sending data back & forth.
Now if I go ahead and click button Stop Server
You will see that the telnets will close(will have to click the telnet windows)
I can go back and start the server again by clicking the button “Accept Incoming Connections”
And launch a few telnets once again
If I type text on these telnet terminals, you’ll see it reaches across to the server.
And, if I try to send text to all clients, that too is going to work fine. (Click button Send All on server)
This means, that starting and stopping multiple times does not have a bad affect of our server.
Now let’s see what happens if I have stopped the server.
I’ll click the button “Stop Server” once again.
And try to connect a Telnet client to it.
You see, the telnet can’t find our server.
Why? Because we called the TcpListener.Stop method when we click the button “Stop Server”.
That’s the reason the server does not listen for new incoming connections.
Now you know how to stop the tcp listener from accepting new connections and how to do away with the tcp client objects too.
-
34Section Summary: TCP/IP Server Side Asynchronous Socket Programming in C# .Net
Section Summary: Asynchronous Socket Programming
Let’s quickly recap the useful things we learned in this section.
In this section, we got started using async and await keywords along with some special async socket I/O methods.
We learned how to accept incomi ng connections using socket helper class TcpListener.
We used the method AcceptTcpClientAsync() to accept connections in a non blocking way
After that, we saw how we can continuously listen for incoming connections and how to handle exceptions in the async method of our class library.
Without impeding the operations.
We also came to know about the TCPClient helper class, which makes client side socket programming easier.
Then we went on to use the ReadAsync method of the network I/O stream using an object of StreamReader.
Not to mention that this too is an asynchronous method, which doesn’t block our application.
Afterwards, we wrote the logic to maintain a list of clients connected to our server.
This list was used to send data to all connected clients using WriteAsync method of NetworkStream attached to the instance of the socket which is attached to our TcpClient.
In the end, I showed you how to stop the server properly by calling the Stop method on the TcpListener and how to disconnect a socket by calling Close method on a TcpClient.
I hope you would’ve found this section useful. Feel free to ask questions in Q&A section.
You can clone or fork the public repositories of this library on GitHub. It’s all yours to modify, improve, and play.
If you think this course has been useful, this course has added value to your life, please leave a positive rating and review.
Next section, we’ll create the client application using async calls.
Using Publisher Subscriber Model To Add Events for socket library in C# .Net
-
35Client side async socket Visual Studio C# project setup
Hi, in this video we’re going to setup a project to create the async client side software of a TCP/IP client server arrangement.
We’re going to add another class to our library LahoreSocketAsync for this purpose.
This work will be done in a console application, to take down two birds with one stone.
We’ll create a new C# console application solution and project and then add the existing library to it.
The server and client solutions refer to the same library, but I’ve kept the solutions separate intentionally.
This is going to make our life easier when we’ll be debugging.
Let’s get started(<do> create project here)
I’ll add a the existing socket library project to this solution
In the solution explorer, I’ll right click the solution and select Add => Existing Project
I’ll go to UdemyAsyncSocketServer folder and go to LahoreAsyncSocket folder
Add LahoreSocketAsync project
Right click LahoreSocketAsync project and select Add => New Item
Select “Class” And name it “LahoreSocketClient”
All right, we’re cool here. In the next video, we’ll some logic to connect with a server and start reading data.
-
36Client Socket Programming using async keyword in C#.Net
In this video, we’re going to call the async method TcpClient.ConnectAsync to connect with the server.
We will also add the member variables needed to represent the server IP Address, server port number, and the TcpClient object.
In order to make the class available publicly, we will add public access specifier to our client class name too.
I’ve opened the socket client library project.
First thing, I’ll add member variables to class LahoreSocketClientAsync to store server IP and port.
IPAddress mServerIPAddress;
Click red squigly, click light bulb, and click “Add System.Net;”
int mServerPort;
We’re going to use a TcpClient helper class to connect with the server. Let’s declare it at the class level.
TcpClient mClient;
Add relevant namespace using System.Net.Sockets;
I want to declare class constructor to set default values of these members here.
Ctor double tab
mClient = null;
mServerPort = -1;
mServerIPAddress = null;
Now Let’s create getters for these members.
The setters will be a little different. I want to return false if the user supplies an invalid IPAddress or Port values.
Let me define a new method here
Let’s create a similar method for the port number too.
public bool SetPortNumber(string _ServerPort)
Now comes the heart of this video, the async method ConnectToServer.
public async Task ConnectToServer()
{
}
This method is declared to be async because we’re going to make an async method call inside of it.
The return type Task is essentially equivalent to void.
The MSDN says we should use async Task instead of async void for async methods. Going into the details will be beyond the scope of this course.
Now let’s add the method
public async Task ConnectToServer()
{
}
If mClient is equals to null, we’ll assign it a new instance.
if(mClient == null)
{
mClient = new TcpClient();
}
We’ll use the constructor without parameters.
Let’s add a try/catch block for safety sake
And then, we
’ll call the method mClient.ConnectAsync
We’ll supply this method with the IPAddress of the server and the port number.
Await mClient.ConnectAsync(mServerIPAddress, mServerPort);
Console.WriteLine(string.Format("Connected to server IP/Port: {0} / {1}", mServerIPAddress, mServerPort));
This is it, we’re in good shape. We’ll add the logic for reading data from the socket network stream in the next video. We’ll also call these the newly added in our console application and have a demo afterwards. See you in the next video!
-
37Creating a console application and adding C# socket library reference
-
38Reading data from TcpClient socket network stream with ReadAsync in C# .Net
In this video, we’re going to write code to read data sent down from the server to our client.
2. The receive operation is going to be exactly the same like we saw in the server side video previously
3. I’ll go inside the LahoreSocketClient class dot c s file.
4. In the ConnectToServer method, I’ll go to the point after the method call
5. await mClient.ConnectAsync(mServerIPAddress, mServerPort);
6. We need to add three local variables here
i) A streamreader to read data off the network stream of our TcpClient object
ii) A char array to store the data sent by the server
iii) And an int to store the number of characters read in
7. Let’s declare the variables
9. You can see that we’ve called the constructor of StreamReader and passed it the result of mClient.GetStream() method.
10. This method is going to return the network stream associated to the instance of our TcpClient.
11. A network stream is attached to every socket and every TcpClient to perform I/O operations.
12. I’ll click the StreamReader and press Ctrl+. And click using System.IO; to bring in the relevant namespace.
13. Now add an infinite while loop which will end only when the connection is broken from the server.
17. Inside this loop, I’ll call the ReadAsync method of our stream reader.
18. await clientStreamReader.ReadAsync(buff, 0, buff.Length);
19. This method is going to return the data sent by the server in the first parameter, which is the char array buff.
20. We’ve told the method that it can start putting data in the output buffer starting from zero and ending at the length of buffer.
21. Let’s put the return value in the readByteCount variable.
23. If the read byte count is ever less than equal to zero, that means the connection with server has been broken.
24. We’ll put this fact in code
We will need to close the client before moving forward.
25. And now write the result to the console.
26. Console.WriteLine(string.Format("Received bytes: {0} - Message: {1}", readByteCount, new string(buff)));
27. Before we start reading data again, we need to clean the buffer.
28. Array.Clear(buff, 0, buff.Length);
29. This is it, we’re all set for receiving data too.
30. We’ll have a demo in the next video!
-
39Demo: Async Client side TCP/IP socket programming in C# .Net
-
40Writing data on the a client socket with StreamWriter in C# .Net
1. Sending data to the server is again, a fairly simple task.
Just like everything else in this course.
2. We will use an object of stream writer to write data on the socket connected to our instance of TcpClient.
3. Needless to say, we’ll do it in a non-blocking way and add an async method to our client class.
Let’s open the client project in visual studio.
4. We’ll head over to the main method in the console application.
5. Inside the do while loop, we can send the input data to the server if user input is not equal to <EXIT>
if(strInputUser.Trim() != "<EXIT>")
{
}
6. Inside the if condition Let’s call an arbitrary send method on the client object.
7. We’ll define this method later on.28-Read data on stream reader
8. client.SendToServer(strInputUser);
9. Now I’ll click the method call SendToServer and press Ctrl+.
10. And select Generate method
11. Next, I’ll press F12 and go to the method definition
12. The default implementation is throwing NotImplementedException, let’s remove it
13. We also need to add the keyword async to the method signature to make it non blocking
14. Let’s do it
15. And as you remember I told your earlier, we must never put async void in a method signature.
16. Instead, we should use async Task
17. Inside of this method we can simply check if the mClient is not null and if it’s connected.
if(mClient != null)
{
if(mClient.Connected)
{
}
}
18. I also don’t want to do anything when the user has supplied a null or empty parameter. Let’s put this in code too.
19. We’ll do it at the top section of the method.
20.
if (string.IsNullOrEmpty(strInputUser))
{
Console.WriteLine("Empty string supplied to send.");
return;
}
21. Now let’s create an instance of a stream writer to send data.
StreamWriter clientStreamWriter = new StreamWriter(mClient.GetStream());
clientStreamWriter.AutoFlush = true;
24. Auto flush is an important property.
25. This means that as soon as one write operation is finished, the data should be flushed over to the other end of stream.
26. And that is just it!
27. Let’s have a short demo in the next video.
-
41Demo: Write data on TcpClient network stream with StreamWriter in C#.Net
Demonstration, writing data on TcpClient network stream.
-
42Close Connection on TCPClient for socket programming in C# .Net
- Let us write a method to close and disconnect the TcpClient.
- We’ll first go to the program.cs file.
- Inside the while loop, Right after the if input is not equals to null, I’ll add an else if condition.
- Inside this if condition, I will call a method which we will define in a moment.
- I’ll click the Disconnect method and the bulb and generate the method.
- Then, I’ll press F12 to go to the method body.
- Remove the default code.
- And I’ll add some new logic in here.
- That’s all for this video, let’s have a demo in the next one.
- I’ll run the server first and start listening for incomings
- Then I will run our client program.
- I’ll supply it the IP address & port number
- Then I will type <EXIT> command.
- The client will close peacefully.
- I can see that I can start the client again and exchange data with the server.
- Closing the client does not cause any problem.
- We can see that the server logs contain no exceptions.
- Let’s run the client one more time.
- This time, I’ll close it by clicking cross button directly.
- You’ll see that the server is handling an exception in this case.
- So, always be a responsible citizen and close your sockets properly.
else if(strInputUser.Trim().Equals("<EXIT>"))
{
}
client.CloseAndDisconnect();
if(mClient != null)
{
if (mClient.Connected)
{
mClient.Close();
}
}
-
43Demo: Closing connection on TcpClient for socket programming in C# .Net
Demo of how to close a TCP/IP client side connection properly.
-
44Section Summary: Async client socket programming in C# .Net with async/await
§Setup a new solution for client side network programming
§Added a new class to the library
§TcpClient.ConnectAsync method call
§Read data with StreamReader.ReadAsync method call
§Write data with StreamWriter.WriteAsync method call
§Close a connection with TcpClient.Close method call
Socket Programming: Additional Helpful Topics in C# .Net
-
45Introduction to pub/sub model and events/delegates in C#.Net
- We will publish events that conform to .Net Framework Guidelines as specified on Microsoft’s website.
- You can see the page on screen.
- We will define events based on generic EventHandler delegate which is defined in .Net Framework.
- The website is shown on screen
- The event objects which we will define are going to hold one or more references to method names in the subscriber code.
- Delegate definition is quite simple.
- The first parameter will contain a reference to the object from which the event was raised
- The second parameter will contain an object of a class derived from EventArgs.
- We will create a special class for this purpose.
- The EventArgs derived object will contain event specific information.
- Let’s open the LahoreSocketAsync project in the UdemyAsyncSocketServer solution
- First of all, I will define a custom class derived from EventArgs
- Right click the “solution”, go to “Add”, Select “New Item”, and select “Code”, “Code File”Name it “CustomEventArgs.CS”.pri
- Now copy over the namespaces stuff.
- To add a new class, I’ll type class and press double tab.
- I’ll rename the class and make it derive from EventArgs
- I want this event arguments to contain the IP address of currently connected client
- So, I’ll add a new private string here.
- And a getter for this string.
- Let’s add a parameterized constructor to this class too.
- The first parameter will be a string representing the EndPoint of newly connected client.
- Now let’s go to the LahoreSocketServer class.
- In here, we’ll add a public member EventHandler of ClientConnectedEventArgsIn order to raise this event, we’ll define a new method too.
- The method is defined as protected virtual such that derived classes can override event invocation behavior
- Inside the OnRaise method, we will create a temporary copy of the event handler object to avoid race conditions.
- EventHandler<ClientConnectedEventArgs> handler = RaiseClientConnectedEvent;
- Then a sanity checks to make sure handler is not null.
- the call to handler
- handler(this, e);
- We are going to call the OnRaise method right after a new client has connected.
- For this purpose We’ll go to the method StartListeningForIncomingConnection
- Right after the debug line we can see a method TakeCareOfTCPClient
- We’ll raise event after this method call
- Let’s prepare the event arguments first of all, this object will contain important information about the event being raised.
- We’ll new it
- And then we’ll call the OnRaise method
- We could’ve done this in one line, but I prefer clarity on cleverness.
- Let’s head over to the subscriber side of the application and add some code to configure & consume this event.
- I’ll open the UdemyAsyncSocketServer.
- Right click the Form1.cs and select View Code.
- Let’s go to the bottom of the file and add a new event handler method here.
- Once we’re done, this method will be the voice of our library.
- The library will be able to invoke this method when a new client will connect.
- Utilization of the information will be totally up to the consumer form application.
- We need to define a method whose first parameter is an object and second parameter is ClientConnectedEventArgs.
- So that, it conforms to the event defined by the server library(?? Can we show some reference material here ??)
- Before we fill into the body of this method, let’s add a multi line text box to the form. I’ll name it txtConsole. <DO THIS QUICKLY>
- Back in the HandleClientConnected, I will add the information to the txtConsole.
- Now, the last and most important step is to hook the subscriber method up to the publisher method
- To do this, we’ll go to the point where we new up the server object.
- In case you’re not familiar with event handlers, we’ll use the plus equals to operator to assign the subscriber method to the publisher hook.
- mServer.RaiseClientConnectedEvent += HandleClientConnected;
- Instead of having a demo in a separate video as we usually do, let’s run the server right away and see how it goes.
- I’ll start the server and click “AcceptIncomingConnection” button.
- Then I’ll press Windows + R and supply telnet command several times
- If we take a look at the server form now, we can see that it is showing a message about client connected.
- I have an assignment for you
- The assignment is you have to create a similar event system when a client gets disconnected.
- You’ll create a class derived from eventargs,
- On the publisher side, a generic member to store the evet handler, a method to fire the event handler, and call it in the server appropriately on one or more places,
- On the subscriber side
- You’ll write add an event handler
- And plug it in to the publisher’s hook. See you in the next video!
-
46Adding a ClientConnected event to the server socket library in C# .Net
Adding a ClientConnected event to the publisher library and handling it in the subscriber side as well.
-
47Adding a TextReceived event on the server to socket library in C# .Net
Time to publish another event in our socket library. This time we'll add a TextReceived event on the server and display the information in a Forms text box.
-
48Publishing event TextReceived on client to socket library in C# .Net
How to publish a TextReceived event in client side classes.
-
49Additional Events C# Source Code
Additional Events
I’ve added following events to the class library along with usage code as well.
Client Library Events
public EventHandler<ConnectionDisconnectedEventArgs> RaiseServerDisconnected;
public EventHandler<ConnectionDisconnectedEventArgs> RaiseServerConnected;
Server Library Events
public EventHandler<ConnectionDisconnectedEventArgs> RaiseClientDisconnectedEvent;
Please download the zip file attached in the downloads section of this lecture and take a look, it will be utilized in the next section(in the making at present).
Deprecated Section: Creating a TCP/IP socket server
-
50Finding IP Address & Hostname of Your Windows PC from Command Prompt
Finding IP Address of Your PC
We will use command prompt to find the IP Address of your PC.
The graphical user interface is different in case of different versions of the Windows operating system, but the good old command prompt remains unchanged.
So, let’s launch command prompt.
Press Windows + R
Type cmd
Click OK
The command we’re going to use is ipconfig with option forward slash all.
I’ll type it on command prompt
Ipconfig space forward slash all
And press enter
The information related to ip configuration of your machine will be printed
Information related to the IP address to your machine while its running on the WiFi is grouped under “Wireless LAN adapter Wireless Network Connection”
The IP Address is printed in front of IPv4 Address
If your machine is connected to the network through an ethernet network cable, the information will be grouped under “Ethernet adapter Local Area Connection”.
Right now, my machine is not connected to an ethernet cable that’s why not much information is displayed here.
It is possible to dump the output of a command prompt command to a text file.
We can use the greater than sign operator for this purpose.
Let’ me show you, I’ll type the same IPConfig command again.
Add a greater than sign, a space and supply the text file name where we want to dump the output.
I’ll name the file ipc.txt
And press enter
Let’s open the file explorer in current directory to see the file.
I’ll type explorer space and a dot
Press enter
We can see the file ipc in the file explorer, let’s open it.
All IP configuration information of your PC is printed here.
The IP Address is printed under Wireless LAN adapter wireless network connection.
-
51Role of DHCP and DNS in a Computer Network
The DHCP and DNS play a key role in modern computer networks.
DHCP stands for Dynamic Host Control Protocol
DNS stands for Domain Name Server
Let me tell you more about the “dynamic duo” of computer netwoks
The Dynamic Host Control Protocol is implemented by a piece of software mostly built into devices like home routers.
The job of DHCP server is to assign an IP address to every device that joins a network and configured to work with DHCP.
Once an IP address is assigned to a host dynamically, the job of DNS starts.
The Domain Name Server keeps track of the IP addresses of all hosts on the network.
The DNS can be used to lookup IP addresses of any host on the network using the host name
The DNS is built into every home router, just like its friend DHCP
In the next video I’ll show you how to get the IP Address of a host by hostname programmatically using the .Net DNS class.
-
52Resolving Hostname to IP Address Using System.Net.DNS for socket programming C#
-
53Demo: Resolving Hostname with System.Net.DNS Class in C# .Net
Demo of how to find IP Address of a HOST by using System.Net.Dns class.
-
54How to use StreamReader.ReadLineAsync and StreamWriter.WriteLineAsync in C# .Net
-
55Bonus: UDP Socket Programming in C# .Net
Since you've come this far in TCP/IP Socket Programming, I'm pretty sure you'd love to learn more about the twin brother of TCP/IP sockets. Yes, I'm talking about UDP Sockets.
Deprecated Section: Creating a TCP/IP socket client
-
56Setting up the project in Visual Studio
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:
System.Net
System.Net.Sockets
-
57Preparing a TCPListener
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.
-
58Listening for incoming connection requests
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.
-
59Demo, 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.
-
60Getting hold of a TCPClient object
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:
- System.Net.Sockets.TcpListener
- 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.
-
61Reading data from a TCPClient network stream
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.
-
62Demo, reading data
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.
-
63Writing data to a TCPClient network stream
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.
-
64Demo, writing data
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:
- Listen for incoming connection requests
- Send data to a client
- Receive data from a client
-
65Creating a TCP/IP Server
This quiz is intended to enforce the concepts and tools students have learned during section 2.
Deprecated Section: Common Network Programming Topics
-
66Setting up the project for socket client in Visual Studio
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.
-
67Connect to a TCP/IP socket server in C# .Net
We will add the namespaces which provide network and socket capabilities to our code. These .Net namespaces are:
System.Net
System.Net.Sockets
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.
-
68Send data 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
TcpClient.GetStream().BeginWrite
A very simple callback delegate method will be written to handle completion of write method as well.
-
69Demo, connect to TCP/IP socket server and send data
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.
-
70Receiving data from a server
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.
-
71Demo, receiving data from 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.
How long do I have access to the course materials?
You can view and review the lecture materials indefinitely, like an on-demand channel.
Can I take my courses with me wherever I go?
Definitely! If you have an internet connection, courses on Udemy are available on any device at any time. If you don't have an internet connection, some instructors also let their students download course lectures. That's up to the instructor though, so make sure you get on their good side!
Stars 5
657
Stars 4
443
Stars 3
157
Stars 2
33
Stars 1
19