Reading and Writing Data – C# – part 8 (Network I/O)

Handling Multiple Connections – Asynchronous Network Client

using System;
using System.IO;
using System.Net.Sockets;

namespace AsynchNetworkClient
    class AsynchNetworkClient
        public static readonly string Message = "Hello from Client !!";
        private readonly NetworkStream _streamToServer;
        public static readonly string ServerName = "localhost";

        public static int Main()
            AsynchNetworkClient client = new AsynchNetworkClient();
            return client.Run();

        public AsynchNetworkClient()
            Console.WriteLine("Connecting to {0}", ServerName);
            TcpClient tcpClient = new TcpClient(ServerName, 65000);
            _streamToServer = tcpClient.GetStream();

        private int Run()
            Console.WriteLine("Sending {0} to server.", Message);

            StreamWriter writer = new StreamWriter(_streamToServer);

            StreamReader reader = new StreamReader(_streamToServer);
            string response = reader.ReadLine();
            Console.WriteLine("Recived from server: {0}", response);
            return 0;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s