Network I/O - Part 4

Naimishforu
Posted by in C# category on for Beginner level | Points: 250 | Views : 4062 red flag
Rating: 5 out of 5  
 1 vote(s)

We will see how to work with Network I/O using C#.

Introduction


Moving information across a network uses essentially the same streaming processes, and the same classes, as moving information around in a file system.

But network data transfers also have to deal with the problem of establishing and maintaining communication between machines.

Network I/O is based on the use of sockets – data objects that represent the endpoints of communication streams.

Sockets are protocol independent and are used for remote procedure calls, as well as peer-to-peer or client-server communications.

A single computer can communicate with many other computers simultaneously by using different ports for each communications socket.

Ports are simply more specific addresses than network addresses. Particular applications can be assigned their own ports for more efficient communication.

Applications use a particular port to receive all communications across the network, but this can lead to delays if multiple clients want to use the same application.

You can use asynchronous I/O to reduce waiting time by making use of additional ports for an application. As soon as secure communications have been established on one port, you can switch the client over to another port, leaving the original address port free to accept new calls.

To read files across a network, you must create a TCPListener instance and specify a port on which it will listen.

This code creates an object called myTcpListener, listening on port 13.

TCPListener myTcpListener = new TCPListener(13);

You must then start the listener.

TCPListener myTcpListener = new TCPListener(13); 

myTcpListener.Start();

When a client requests a connection at port 13, you use the Accept method to create a socket.

You can start transferring information as soon as the socket is accepted.

Socket mySocketForClient = myTcpListener.Accept(); 

if (mySocketForClient.Connected){… }

To send information across the network connection, you must create an instance of the NetworkStream class, which will use the socket you've created.

Then you use a StreamWriter object to send information across that network stream.

NetworkStream myNetworkStream = new NetworkStream(mySocketForClient);

System.IO.StreamWriter myStreamWriter = new System.IO.StreamWriter(myNetworkStream);
Thanks and Have Fun!!!!!
Page copy protected against web site content infringement by Copyscape

About the Author

Naimishforu
Full Name: John Doe
Member Level: Bronze
Member Status: Member,MVP
Member Since: 1/22/2011 7:38:35 AM
Country: India



Login to vote for this post.

Comments or Responses

Posted by: Tripati_tutu on: 3/25/2011 | Points: 25
Simple one. Looking clean.
Posted by: Naimishforu on: 3/25/2011 | Points: 25
Thanks :)

Login to post response

Comment using Facebook(Author doesn't get notification)