Design tcp client and server application to transfer file




















NET libraries and assemblies that can be used to create network-oriented applications in the. NET Framework. The System. Net namespace provides us with assemblies that we can use to work around with networking and protocols in. NET applications. There are many namespaces under this namespace and I would talk about only a few. Namespaces that are introduced or talked about in this article are: System.

Net Core assembly for networking in. Also will be used in our sample project. Mail This namespace holds the objects used to send emails, connect to SMTP servers, and other required operations.

Security Provides us with an object required to generate SSL based streams. Sockets This namespace holds the objects for TCP or UDP clients and listeners and clients for working around with network based on these protocols.

We will use the preceding discussed namespaces and create our applications for both server and client use. In both of these scenarios, we would use console projects and write the appropriate code to maintain the server and client applications.

Sample project For our sample, we will create a sample project that accepts the data from a client. The client sends the data in JSON format, sending the name, email address, and the message of the client to the server.

The server for this example sends an email to the person using his email address. The client asks for the name, email, and message of the user. You can share this client application with anyone on the network, the server would accept the data and respond as required.

Writing the server application Our server application must be hosted to accept new requests from the client. To host our application, we need to provide an address and port that our application would be listening to.

We can use a TcpListener object to create an instance of our server. As a parameter to the constructor, we will pass the IP address endpoint that would be used as the hosting address for our server. Clients need to know this address since they will connect to this server application of ours using this address.

Note that I am using the loopback address You can use other IP addresses or ports to configure your own servers and provide access to it for your clients in the network.

At this stage, our server would be up and running and would be ready to accept new requests from TCP clients. Note that networks work with bytes and we would be reading the data in the form of bytes. After accepting the bytes, the decoding is done to convert the data to an appropriate type, text, image, or audio. In this sample, I will show text only that is simple enough.

Next, we listen to a client. Until a client sends a request, we cannot proceed. To accept the data, we need to have a memory location to store the data sent by the client. Also, we need to store the string representation of the message also. Look below at how I did that.

Read buffer, 0, bytesize ; The preceding code makes our server able to accept a new request and read the data that the client has sent. Until now we have configured the application to be able to start, run, and listen to new requests coming from the server.

Our server is currently synchronous, it can only accept one request and would not accept another unless the connection is terminated. You can use asynchronous programming models to accept multiple requests from multiple clients. In synchronous mode, you cannot accept multiple requests. You need to work with one request, complete the request, and send a response or terminate the connection and then you can move to the next request or client.

Since I told you that bytes are only sent or received, other data types are just fancy items that we use. We need to now convert these bytes into descriptive data. Since we know our client will transmit string data, we can decode the data into string type. Also, note that our buffer size is very large so that we can accept any amount of data. You can shorten it if you want. This much byte data would always contain null characters where there was no character found.

Null characters would take much space on your console. I have written a function for that, to remove the null characters from the string. To remove null characters from the messages sent or received. Apart from this, I have another 2 functions that are used to handle various functions of our server.

One to generate and send the response, the other to send the email to the user as a response. Sending the email As for this example, we will use an email as a response to the user.

We will send an email to the user to notify him that we have received the data that he sent us. I won't explain how this module is being done, for more information on sending emails in the. NET Framework, please read this article of mine. Email, "Thank you for using the Web Service" , string. Name, p. You can alter this email address or you can change how the server behaves when the client communicates.

Sending the response Once everything has been done, it is now time to generate the response to be sent to the client. Similarly, you would send bytes to the client that the application can then convert back to the text or other data that it knows the server sent it.

We can only send a response to the client if he is still connected. Once the connection is lost, we cannot send a response to the client. Since we have the client connected, we can create a separate function to convert bytes of data and stream it down to the client. Write bytes, 0, bytes. Complete server program The complete server program in our console project is as follows: using System; using System. Generic; using System. Linq; using System. Text; using System. Tasks; using System. Sockets; using System.

Net; using System. Mail; using Newtonsoft. Start ; Console. Address, ep. AcceptTcpClient ; sender. Name ; sender. WriteLine e. Do not consider them the required fields. I will explain the person's class in the client application. TCP does Flow Control and requires three packets to set up a socket connection, before any user data can be sent. TCP handles reliability and congestion control.

It also does error checking and error recovery. Erroneous packets are retransmitted from the source to the destination. The entire process can be broken down into following steps: The entire process can be broken down into following steps: TCP Server —.

Skip to content. Change Language. Related Articles. Computer Network Fundamentals. Physical layer. Data Link layer. Network layer. Transport layer.

You can use that to send anything, like the contents of files Alternatively, you could send a message type before each message. Chris Kimpton Chris Kimpton 5, 6 6 gold badges 44 44 silver badges 72 72 bronze badges. Ideally you'd need some to use separate Sockets to handle commands.

The way it's done with FTP is that there's a command channel and a data channel. Thangarasu Thangarasu 1 1 1 bronze badge. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.



0コメント

  • 1000 / 1000