Home » How - To / Tutorial » Programming » Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB.net

Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB.net

Creating-Simple-TCP-IP-Server-And-Client Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB.net

A lot of networked applications nowadays choose TCP/IP because there is no risk of data being damaged while traveling across the Internet. Unlike using UDP to transfer file, TCP/IP said to be connection oriented; which, both client and server after a setup phase treat some IP packets as being sent along a virtual route, enabling for data that is too large to fit into a single IP packet to be sent and for retransmission to occur when packets are lost. This sample software will allow you to deliver any file from one computer to another. Again, it is client/server based, so you will need either two computers or to run both the client and server on the same device.

Creating a simple TCP/IP client In C# / VB.net

Create a new project as usual, and build a form as shown image below.

TCP-IP-Client-Application Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB.net

TCP IP Client Application

Name the Send button btnSend , the Browse button btnBrowse , the File textbox tbFilename , and the Server textbox tbServer . Also add an Open File Dialog control called openFileDialog . Click on the Browse button and put the following code: This code opens the default file open dialog box. If the user does not select a file, openFileDialog.Filename will return an empty string. Click on the Send button and put the following code:

C#

VB.net

The above code opens the default file open dialog box. If the user doesn’t select a file, openFileDialog.Filename will return an empty string. Click on the Send button and add the following code:

C#

VB.net

The above code reads in a file and sends it over a network connection. To read in a file, a stream for this file is created by passing the filename to the OpenRead method. This stream is read into the file buffer array. An alternate means of reading this file would be to pass the file stream as a parameter to the constructor of a StreamReader, then to call the ReadToEnd method, although this approach would only be useful for text-only files.

It then opens a TCP/IP connection with the server on port 8080, as specified in tbServer.Text. The TcpClient constructor is blocking, in that code execution will not continue until a connection is established. If a connection cannot be created, a SocketException will be thrown: “No connection could be made because the target machine actively refused it.” As usual, the following assemblies are included:

C#

 

VB.net

List below the significant methods and properties for TcpClient.

  • Constructor – Initializes a new instance of the TcpClient class. It may be used thus: new TcpClient(string,Int).
  • NoDelay – When set to true, it increases efficiency if your software application only transmits small amounts of data in bursts. Returns Bool.
  • ReceiveBufferSize – Gets or sets the size of the receive buffer. Returns Int.
  • SendBufferSize – Gets or sets the size of the send buffer. Returns Int.
  • SendTimeout – Gets or sets the amount of time a TcpClient will wait to receive confirmation after you initiate a send. Returns Int.
  • Close() – Closes the TCP connection.
  • Connect() – Connects the client to a remote TCP host using the specified host name and port number. It may be invoked thus: Connect(string,Int).
  • GetStream() – Returns the stream used to send and receive data. Returns NetworkStream.

 

Creating a simple TCP/IP server In C# / VB.net

Open a new project as before, and design a user interface as depicted in image below.

TCP-IP-Server-Application Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB.net

TCP IP Server Application

The label should be named lblStatus, and the list box, lbConnections. Like the UDP server, the TCP server is multithreaded. In such a case, three threads are used: the main thread maintains the user interface, a second thread listens for connections, and a third thread handles the connections. One socket is needed for each connection and will remain loaded in memory until the connection is closed.

These sockets need to be stored in an ArrayList rather than a standard array because it is impossible to predict how many connections will be received. To begin with, declare a global ArrayList variable:

C#

VB.net

Because any client wishing to connect to this server would need to know its IP address, it is helpful to display this on-screen. This is a cosmetic feature, but it may come in handy in other applications. To be able to retrieve the local IP address, we call the static method Dns.GetHostByName. This returns an IPHostEntry object, which is a collection of IP addresses, to provide multihomed computers, which many are. Element zero in this array is normally the external IP address for the computer.

The Form1_Load method displays the local IP address on the form and starts the thread that will wait for incoming connections. If the listenerThread method were to be called immediately, the application would become unresponsive and seem to hang, while the socket waited on inbound connections. This effect is avoided by performing the listenerThread method in a separate thread of execution, which can block without adversely affecting the user interface.

C#

VB.net

The listenerThread method’s function is to wait indefinitely for TCP connections on port 8080 and then to redelegate the work of handling these requests to the handlerThread method. This function also reports the source of the connections. This time, the explanation for redelegating work to a thread is not to maintain the responsiveness of the user interface, but rather to ensure that the software will continue to listen for new connections while it is handling a previous client.

The new thread will be needed to have access to the socket that is dealing with the current client. Otherwise, there would be no means of returning data. This thread will stop on the call to AcceptSocket. Execution will not continue until an incoming connection has been detected; when it has, a new socket is created and dedicated to handling this particular client. As soon as this socket has established a connection, the socket is placed on top of the nSockets array list to await pickup by the handler thread. It may seem strange that the socket is not passed directly to the thread. This is because it is not valid to specify parameters when defining the starting point of a thread, for example, making an erroneous statement such as

Therefore, another means of passing parameters to threads is required. In this example, a public array list of sockets is used, where the top-most entry is used by the newest thread, and so forth. Another popular technique for passing parameters to threads is to encapsulate the thread’s methods in a different class, with public variables acting as parameters. When a new instance of this class is created, it can be passed to the ThreadStart constructor.

Once the socket has been added to the array list, the handler thread is invoked, and this thread keeps to listen for incoming connections.

C#

VB.net

The rest of the work is carried out in the handlerThread method. This function finds the last used socket and then retrieves the stream from this socket. An array is allocated to the same size as the stream, and once the stream is completely obtained, its information are copied into this array. As soon as the connection closes, the data is written to file at c:\my documents\SubmittedFile.txt. It is important to have the lock() keyword around the lines of code associated with file access; otherwise, if two concurrent connections try to access the same file, the application will crash. The contents of the file are then shown in the list box on-screen.

The socket is then set to null to remove it from memory. If this point were omitted, the array list would quickly fill with sockets that had lost connection with their clients. Note that the constructor for TcpListener that takes only a single int for a port number is now obsolete. To prevent the compiler complaining about this line of code, simply call the constructor thus:

C#

VB.net

Like before, put the namespace references to the head of the code:

C#

VB.net

To test the application, run the server application, and take note of the IP address displayed. Then, run the client application. Type the IP address into the box provided. Click on browse to select a file. Press send to transfer the file. A file will soon appear on the server at c:\my documents\SubmittedFile.txt, which is an exact copy of the file that was located on the client.

To further illustrate this concept, you can use a telnet program to write text to c:\SubmittedFile.txt remotely. On Windows 95, 98, or ME machines, click Start→Run, then type Telnet. Click Connect→Remote System. Type the server IP address into the host name textbox, and type 8080 into the port textbox. Press Connect. Type some text into the window, and when finished, press Connect, Disconnect. A file will soon appear on the server at c:\my documents\ SubmittedFile.txt. On Windows NT, 2000, and XP machines, click Start→Run, then type Telnet. Type Open 127.0.0.1 8080. Replace 127.0.0.1 with the IP address of your server, if you have two computers.

Type some text into the window, and when finished, close the window. A file will soon show up on the server at c:\SubmittedFile.txt. Ways have already been developed to send files through the Internet. Anybody who has ever written a Web site would be familiar with programs such as cuteFTP and smartFT, albeit with a much more adjustable interface. It is rarely a good idea to try to recreate the wheel and develop a new way to send data through the Internet. The global standardization of protocols has made the Internet what it is today.

12 thoughts on “Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB.net

  1. Faith yintii says:

    Hi am new at this and i’ve got just one question
    From what you have stated, can i safely assume Multiple Clients can connect to the Server “almost” at once??

    1. James Howard says:

      well, if you want to use multiple clients you have to make a new thread for each client which run asynchronously

  2. Pushkar says:

    Hi, I am new at this and I want to transfer a string instead of a text file will you please guide me on how to transfer string instead of file.

  3. Hans G. von Sychowski says:

    Hi,
    I took your tutorial and reconfigured my single-client/server code to multi-client. Seems to work in some ways.
    As I’m still trying to educate myself in case of Client/server approach, most of the examples and tutorials you find in the web are highly complicated for my eyes still. Your example is one of the simple ones, which is not a negative measure – simplicity is in most cases the base for a very good solution.
    As I still have not fully understood the approach, let me ask you a question:
    I want to set up my server in a way that multiple clients connect to the server and then the server gives the command to a specific client to transfer data to the server. the server then gives the command to the client to stop data transfer. Then the server start communication with another client in the same way.
    When I have several clients connected to the server – I think it is not necessary to close the connection to a client I had communication. I think (and hope!) it is only necessary to close the communication stream but stay connected. To switch now to a different client (which should be connected and in the list beforehand)is it only necessary to change the index of the array which holds all the clients (nSockets)?
    If you could give me a hint in which direction to go would be highly appreciated.
    Best Regards
    Hans

  4. Dessotti says:

    Hello,

    First of all, thanks for the article, it helped me a lot. But I have a little problem e I think I must be missing something. but there is an invalid operation exception saying that the control lbconnections was accessed from a thread other than the thread it was created on. Any ideas? I’m using Visual Studio 2015 Express and took the full code to test but there is this problem.

    Thank you

    1. Dessotti says:

      Found it: add this if there is no concurrent threads using the same control: Control.CheckForIllegalCrossThreadCalls = False
      This line disable this check. For a simple form like this I think it is acceptable.

  5. Pinal says:

    Hello,

    Am i Using Your Code but Not Work,And Generate Error Something Like : Cross-thread operation not valid: Control ‘lbConnections’ accessed from a thread other than the thread it was created on.

    what is reason this error and how to solve them?

    1. James says:

      You’re using different thread for lbconnections. You should call the thread before accessing it from other thread. Here’s a way to make thread-safe calls https://msdn.microsoft.com/en-us/library/ms171728.aspx or just add
      Control.CheckForIllegalCrossThreadCalls = false;
      inside listener thread

  6. Rahman says:

    I need to send File name Also so How can i do it?

  7. aldwyn says:

    is this article still active?!?! cause im interested on this program
    pls reply cause i have lots of errors on server side thanks

  8. David Beckman says:

    Thanks for one’s marvelous posting! I truly enjoyed reading it, you may be a great author. I will be sure to bookmark your blog and will come back from now on. I want to encourage one to continue your great job, have a nice afternoon!

  9. brolland says:

    I really enjoyed reading this and following your steps. It helped me tremendously thank you.
    Side note: People who are getting cross thread error you need to put this in your code > Control.CheckForIllegalCrossThreadCalls = false; <

Leave a Reply to Dessotti Cancel reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website