IT rekvalifikace s garancí práce. Seniorní programátoři vydělávají až 160 000 Kč/měsíc a rekvalifikace je prvním krokem. Zjisti, jak na to!
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Tcp_communication

TCP komunikacia client - server

C# .NET

//---------------------------------------- kniznice
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Globalization;



//------------------------------------------ CLIENT
    class Program
    {
        private TcpClient tcp;
        private StreamWriter SwSender;
        private StreamReader SrReciever;
        private Thread thrMessage;
        private String strHostName;


        static void Main()
        {
            try
            {
                IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress adresa = ipEntry.AddressList[4];
                Console.WriteLine("Ip adresa je {0} a host name je {1}", adresa, Dns.GetHostName());
                TcpClient client = new TcpClient();
                Console.WriteLine("Connecting....");
                //client.Connect(IPAddress.Parse("47.94.1.100"), 6000);
                var result = client.BeginConnect(adresa, 6000, null, null);
                var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5));
                if (!success)
                {
                    throw new Exception("Failed to connect.");
                }

                client.EndConnect(result);

                Console.WriteLine("Connected\nexit  - close communication\n");
                while (success)
                {

                    Console.Write("Odoslane z {0}: ", Dns.GetHostName());
                    String text = Console.ReadLine();
                    while (text.Length > 49)
                    {
                        Console.Write("Max. dlzka retazca je 50 znakov!\nOdoslane: ");
                        text = Console.ReadLine();
                    }

                    Stream stm = client.GetStream();
                    ASCIIEncoding asen = new ASCIIEncoding();
                    byte[] ba = asen.GetBytes(text);
                    stm.Write(ba, 0, ba.Length);

                    if (text.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
                        return;

                    byte[] bb = new byte[50];
                    int k = stm.Read(bb, 0, bb.Length);
                    Console.Write("Prijate od {0}: ", Dns.GetHostName());
                    for (int i = 0; i < k; i++)
                        Console.Write(Convert.ToChar(bb[i]));
                    Console.WriteLine();
                    stm.Flush();
                }
                Console.ReadKey();
                client.Close();
            }

            catch (Exception e)
            {
                Console.WriteLine("Error " + e.StackTrace);
                Console.ReadKey();
            }


        }
    }
}

//--------------------------------------------------- SERVER
    class Program
    {
        private StreamWriter writer;
        private StreamReader reader;

        static void Main()
        {

            try
            {
                TcpListener tcpServer = new TcpListener(IPAddress.Any, 6000);
                TcpClient client;
                String exit = "exit";
                NetworkStream stream;

                Console.WriteLine("Waiting fo client....");
                tcpServer.Start();
                Byte[] bytes = new Byte[256];
                client = tcpServer.AcceptTcpClient();
                Console.Write("Connected");
                while (true)
                {
                    stream = client.GetStream();
                    byte[] buffer = new byte[50];
                    stream.Read(buffer, 0, buffer.Length);
                    double[] newValues = new double[buffer.Length];
                    char[] chars = new char[buffer.Length];

                    Console.Write("\nPrijate:  ");
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        Console.Write(Convert.ToChar(buffer[i]));
                        chars[i] = Convert.ToChar(buffer[i]);
                    }
                    string text = new string(chars);
                    if (text.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
                        return;

                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();
                }
                tcpServer.Stop();

            }
            catch (Exception e)
            {
                Console.WriteLine("Error " + e.StackTrace);
            }

        }

    }


Neformátovaný

Přidáno: 20.7.2016
Expirace: Neuvedeno

Avatar
Autor: marek.figura1
Aktivity