Exemplo n.º 1
0
        public override void RunSocket()
        {
            try {
                SocketApp.Bind(IpEndPoint);
                SocketApp.Listen(10);
                Console.WriteLine("Server started. Connected..");

                while (true)
                {
                    Socket        handler = SocketApp.Accept();
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;
                    byte[]        data    = new byte[256];

                    do
                    {
                        bytes = handler.Receive(data);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    } while (handler.Available > 0);

                    Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + builder);

                    string messageResponse = "Meessage send.";
                    data = Encoding.Unicode.GetBytes(messageResponse);
                    handler.Send(data);
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 2
0
        public override void RunSocket()
        {
            try {
                SocketApp.Bind(IpEndPoint);
                Console.WriteLine("Server started. Connected..");

                while (true)
                {
                    StringBuilder builder  = new StringBuilder();
                    int           bytes    = 0;
                    byte[]        data     = new byte[256];
                    EndPoint      remoteIp = new IPEndPoint(IPAddress.Any, 0);

                    do
                    {
                        bytes = SocketApp.ReceiveFrom(data, ref remoteIp);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    } while (SocketApp.Available > 0);

                    IPEndPoint remoteFullIp = remoteIp as IPEndPoint;
                    Console.WriteLine("{0}:{1} - {2}", remoteFullIp.Address.ToString(),
                                      remoteFullIp.Port, builder.ToString());
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            finally {
                Close();
            }
        }