Select Tcp Client : TCP Client « Network « C# / C Sharp






Select Tcp Client

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class SelectTcpClient
{
   public static void Main()
   {
      Socket sock = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
      byte[] data = new byte[1024];
      string stringData;
      int recv;

      sock.Connect(iep);
      Console.WriteLine("Connected to server");
      recv = sock.Receive(data);
      stringData = Encoding.ASCII.GetString(data, 0, recv);
      Console.WriteLine("Received: {0}", stringData);


      while(true)
      {
         stringData = Console.ReadLine();
         if (stringData == "exit")
            break;
         data = Encoding.ASCII.GetBytes(stringData);
         sock.Send(data, data.Length, SocketFlags.None);
         data = new byte[1024];
         recv = sock.Receive(data);
         stringData = Encoding.ASCII.GetString(data, 0, recv);
         Console.WriteLine("Received: {0}", stringData);
      }
      sock.Close();
   }
}

           
       








Related examples in the same category

1.Uses a TcpClient to handle HTTP
2.Network Stream Tcp Client
3.Simple Tcp Client
4.Var Tcp Client
5.Bad Tcp Client
6.Fixed Tcp Client
7.Stream Tcp Client
8.Employee Client
9.Network Order Client
10.Tcp Client Sample
11.Async Tcp ClientAsync Tcp Client
12.Picky Tcp Client