1.7.2. fejezet, TCP Socket

TCP szerver

namespace TCPServerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = null; NetworkStream stream = null;
            try
            {
                IPAddress ipAddr = IPAddress.Parse("127.0.0.1");
                int portNum = 5000;
                IPEndPoint endPoint = new IPEndPoint(ipAddr, portNum);
                server = new TcpListener(endPoint);
                server.Start();
                Console.WriteLine("A szerver elindult!");
                TcpClient client = server.AcceptTcpClient();
                stream = client.GetStream();
                byte[] data = new byte[256];
                int length = stream.Read(data, 0, data.Length);
                Console.WriteLine("A kliens üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
 
                data = Encoding.UTF8.GetBytes("Hello kliens...");                
                stream.Write(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                stream.Close(); server.Stop();
            }
        }
    }
}

TCP klies

namespace TCPClientApp
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = null;
            NetworkStream stream = null;
            try
            {
                client = new TcpClient("127.0.0.1", 5000);
                byte[] data = new byte[128];
                stream = client.GetStream();
                byte[] data = data = Encoding.UTF8.GetBytes("Árvíztűrő tükörfúró gép");
                stream.Write(data, 0, data.Length);
                data = new byte[256];
                int length = stream.Read(data, 0, data.Length);
                Console.WriteLine("A szerver üzenete: {0}", Encoding.UTF8.GetString(data));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally {
                stream.Close();
                client.Close();
            }
            Console.ReadKey();
        }
    }
}

Socket szerver

namespace SocketServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket server = null;
            Socket client = null;
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000);
                server.Bind(endPoint); server.Listen(2);
                Console.WriteLine("Szerver elindult...");
                client = server.Accept();
                byte[] data = new byte[256];
                int length = client.Receive(data);
                Console.WriteLine("A kliens üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
                data = new byte[256];
                data = Encoding.UTF8.GetBytes("Hello kliens!");
                client.Send(data, data.Length, SocketFlags.None);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                client.Close();
                server.Close();
            }
            Console.ReadKey();
        }
    }
}

Socket kliens

namespace SocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket client = null;
            try
            {
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000); ;
                client.Connect(endPoint);
                byte[] data = new byte[256];
                data = Encoding.UTF8.GetBytes("Árvíztűrő tükörfúró gép");
                client.Send(data, data.Length, SocketFlags.None);
                data = new byte[256];
                int length = client.Receive(data);
                Console.WriteLine("A szerver üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
            }
            catch (Exception e) 
            {
                Console.WriteLine(e.Message); 
            }
            finally 
            {
                client.Close();
            }
            Console.ReadKey();
        }
    }
}

Blokk nélküli TCP kliens

namespace BlocklessTCPClient
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = null;
            NetworkStream stream = null;
            try
            {
                client = new TcpClient("127.0.0.1", 5000);
                stream = client.GetStream();
                byte[] data = Encoding.UTF8.GetBytes("Árvíztűrő tükörfúró gép");                        
                        stream.Write(data, 0, data.Length);
 
                bool complette = false;
                while (!complette)
                {
                    if (stream.DataAvailable)
                    {
                        data = new byte[256];
                        int length = stream.Read(data, 0, data.Length);
                        Console.WriteLine("A szerver üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
                        complette = true;
                    }
                    else
                    {
                        Console.WriteLine("A szerver még nem küldött választ!");
                        System.Threading.Thread.Sleep(200);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                stream.Close();
                client.Close();
            }
            Console.ReadKey();
        }
    }
}

Blokk nélküli TCP szerver

namespace BlocklessTCPServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = null;
            NetworkStream stream = null;
            IPAddress ipAddr = IPAddress.Parse("127.0.0.1");
            int portNum = 5000;
            IPEndPoint endPoint = new IPEndPoint(ipAddr, portNum);
            server = new TcpListener(endPoint);
            server.Start();
            Console.WriteLine("A szerver elindult!");
            try
            {
                bool repeatable = true;
                int count = 0;
                while (repeatable)
                {
                    if (server.Pending())
                    {
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Kliens kapcsolódott...");
                        stream = client.GetStream();
                        byte[] data = new byte[256];
                        int length = stream.Read(data, 0, data.Length);
                        Console.WriteLine("A kliens üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
                        data = Encoding.UTF8.GetBytes("Hello kliens!");
                        stream.Write(data, 0, data.Length);
                        if (count == 2)
                        {
                            repeatable = false;
                        }
                        count++;
                    }
                    else
                    {
                        Console.WriteLine("Várakozás...");
                        System.Threading.Thread.Sleep(500);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                stream.Close();
                server.Stop();
            }
            Console.ReadKey();
        }
    }
}

Blokk nélküli Socket kliens

namespace BlocklessSocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket client = null;
            try
            {
                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000); ;
                client.Connect(endPoint);
                byte[] data = new byte[256];
                data = Encoding.UTF8.GetBytes("Árvíztűrő tükörfúró gép");
                client.Send(data, data.Length, SocketFlags.None);
                while (true)
                {
                    if (client.Available > 0)
                    {                        
                        int length = client.Receive(data);
                        Console.WriteLine("A szerver üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Várunk a szerver válaszára...");
                        System.Threading.Thread.Sleep(200);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                client.Close();
            }
            Console.ReadKey();
 
        }
 
    }
}

Blokk nélküli Socket szerver

namespace BlocklessSocketServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket server = null;
            Socket client = null;
            try
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5000);
                server.Bind(endPoint); server.Listen(2);
                Console.WriteLine("Szerver elindult...");
 
                bool repeatable = true;
                int count = 0;
                while (repeatable)
                {
                    if (server.Poll(0, SelectMode.SelectRead))
                    {
                        client = server.Accept();
                        byte[] data = new byte[256];
                        int length = client.Receive(data);
                        Console.WriteLine("A kliens üzenete: {0}", Encoding.UTF8.GetString(data, 0, length));
                        data = new byte[256];
                        data = Encoding.UTF8.GetBytes("Hello kliens!");
                        client.Send(data, data.Length, SocketFlags.None);
                        if (count == 2)
                        {
                            repeatable = false;
                        }
                        count++;
                    }
                    else
                    {
                        Console.WriteLine("A szerver bejövõ kapcsolatra vár!");
                        System.Threading.Thread.Sleep(500);
                    }                    
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                client.Close();
                server.Close();
            }
            Console.ReadKey();
        }
    }
}
CsatolmányMéret
Csomag ikon TCPApps.zip45.4 KB