Team LiB
Previous Section Next Section

Chapter 14: Networking and WWW Connections

Socket Connections

For networking, you need to create socket connections for clients and listen for socket connections. The following examples demonstrate creating and listening for socket connections.

Code Example: Creating a Socket Connection/Client
Start example
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Client.Chapter_14___Networking_and_WWW_Connections
{
          class CreatingSocketConnections
          {


                 [STAThread]
                 static void Main(string[] args)
                 {
                        TcpClient MyClient = new TcpClient();
                        MyClient.Connect("localhost", 10000);
                        NetworkStream MyNetStream = MyClient.GetStream();
                        if(MyNetStream.CanWrite && MyNetStream.CanRead)
                        {
                             //Does a simple write.
                              byte[] sendBytes =
                                Encoding.ASCII.GetBytes("Is anybody there");
                              MyNetStream.Write(sendBytes, 0, sendBytes.Length);
                             //Reads the NetworkStream into a byte buffer.
                              byte[] bytes = new byte[MyClient.ReceiveBufferSize];
                              MyNetStream.Read(bytes, 0,
                                (int) MyClient.ReceiveBufferSize);
                              //Returns the data received from the
                              //host to the console.
                              string returndata = Encoding.ASCII.GetString(bytes);
                               Console.WriteLine("This is what the host " +
                                  "returned to you: " + returndata);
                        }

                        else if (!MyNetStream.CanRead)
                        {
                               Console.WriteLine(
                                 "You cannot write data to this stream");
                               MyClient.Close();
                        }
                        else if (!MyNetStream.CanWrite)
                        {
                               Console.WriteLine(
                                 "You cannot read data from this stream");
                               MyClient.Close();
                        }
                 }
          }
}
End example
Code Example: Listening for Socket Connections
Start example

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

namespace Client.Chapter_14___Networking_and_WWW_Connections
{
          class ListeningForSockets
          {
                [STAThread]
                static void Main(string[] args)
                {
                       int PortNumber = 10000;
                       TcpListener MyListener = new TcpListener(PortNumber);
                       MyListener.Start();
                      //Console.WriteLine("Waiting For Connection " );
                       TcpClient MyClient = MyListener.AcceptTcpClient();
                       Console.WriteLine("Connection Accepted");
                       NetworkStream MyNetStream = MyClient.GetStream();
                       String Response = "Connection Has been accepted";
                       byte[] SendTheseBytes = Encoding.ASCII.GetBytes(Response);
                      MyNetStream.Write(SendTheseBytes, 0, SendTheseBytes.Length);
                       MyClient.Close();
                       MyListener.Stop();
                }
          }
}
End example

Team LiB
Previous Section Next Section