Creating Simple TCP/IP Server And Client to Transfer Data Using C# / VB. 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. Create a new project as usual, and build a form as shown image below. Free VB and Visual Basic.NET icons for your programs and applications! Icons or Images can really enhance your Visual Basic 6.0 and Visual Basic.NET applications. A website with various source code and resources for Visual Basic and Visual Basic.Net. TCP IP Client Application. Name the Send button btn. Send , the Browse button btn. Browse , the File textbox tb. Filename , and the Server textbox tb. Server . Also add an Open File Dialog control called open. File. Dialog . Click on the Browse button and put the following code: This code opens the default file open dialog box. VBA tutorials: Visual Basic programming help and examples for Microsoft Access. Learn how to create simple VBA scripts to create dynamic Access databases. FTP Examples for VB.NET. FTP Download with Progress Event Callbacks; Active and Passive Modes in FTP; Append to Existing File on FTP Server; FTP/SSL (AUTH SSL, TLS). Defining basic terms Application An application is a collection of objects that work together to accomplish something useful. In VB the application is called a Project. If the user does not select a file, open. File. Dialog. Filename will return an empty string. Click on the Send button and put the following code: C#. Visual Basic 6 is still very alive and active. Many business have huge applications written in this great language. Wouldn't it be nice to have.Browse. If the user doesn’t select a file, open. File. Dialog. Filename will return an empty string. Click on the Send button and add the following code: C#. Send. To read in a file, a stream for this file is created by passing the filename to the Open. Read 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 Stream. Reader, then to call the Read. To. End method, although this approach would only be useful for text- only files. It then opens a TCP/IP connection with the server on port 8. Server. Text. The Tcp. Client constructor is blocking, in that code execution will not continue until a connection is established. If a connection cannot be created, a Socket. Exception will be thrown: “No connection could be made because the target machine actively refused it.” As usual, the following assemblies are included: C#. System. Threading. System. Net. using System. Net. Sockets. using System. Text. using System. IO; using. System. Threading; using. System. Net; using. System. Net. Sockets; using. System. Text; using. System. IO; VB. net. System. Threading. System. Net. imports System. Net. Sockets. imports System. Text. imports System. IOimports. System. Threadingimports. System. Netimports. System. Net. Socketsimports. System. Textimports. System. IOList below the significant methods and properties for Tcp. Client. Constructor – Initializes a new instance of the Tcp. Client class. It may be used thus: new Tcp. Client(string,Int). No. Delay – When set to true, it increases efficiency if your software application only transmits small amounts of data in bursts. Returns Bool. Receive. Buffer. Size – Gets or sets the size of the receive buffer. Returns Int. Send. Buffer. Size – Gets or sets the size of the send buffer. Returns Int. Send. Timeout – Gets or sets the amount of time a Tcp. Client 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). Get. Stream() – Returns the stream used to send and receive data. Returns Network. Stream. Creating a simple TCP/IP server In C# / VB. Open a new project as before, and design a user interface as depicted in image below. TCP IP Server Application. The label should be named lbl. Status, and the list box, lb. Connections. 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 Array. List rather than a standard array because it is impossible to predict how many connections will be received. To begin with, declare a global Array. List variable: C#. Form. 1 : System. Windows. Forms. Form. 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. Get. Host. By. Name. This returns an IPHost. Entry 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 Form. 1. If the listener. Thread 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 listener. Thread method in a separate thread of execution, which can block without adversely affecting the user interface. C#. private void Form. 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 Accept. Socket. 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 n. Sockets 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. New Thread. Start(Address. Of handler. Thread(Parameter))1. New. Thread. Start(Address. Of handler. Thread(Parameter))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 Thread. Start 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#. public void listener. Thread(). . 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\Submitted. File. 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 Tcp. Listener 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. Tcp. Listener(IPAddress. Any,8. 08. 0)1new. Tcp. Listener(IPAddress. Any,8. 08. 0)C#. public void handler. Thread(). . 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\Submitted. File. 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: \Submitted. File. txt remotely. On Windows 9. 5, 9. ME machines, click Start. Type the server IP address into the host name textbox, and type 8. Type some text into the window, and when finished, press Connect, Disconnect. A file will soon appear on the server at c: \my documents\ Submitted. File. txt. On Windows NT, 2. XP machines, click Start. Type Open 1. 27. 0. Replace 1. 27. 0. 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: \Submitted. File. 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 cute. FTP and smart. FT, 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |