Beispiel #1
0
 public void ThreadSend(object Message)
 {
     try
     {
         String MessageText = "";
         if (Message is string)
         {
             MessageText = Message as string;
         }
         else
         {
             throw new Exception("Нужен текст, а не хрен знает что!");
         }
         IPEndPoint EndPoint  = new IPEndPoint(IPAddress.Parse(IP.Text), 7000);
         Socket     Connector = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         Connector.Connect(EndPoint);
         Byte[] SendBytes = Encoding.Default.GetBytes(MessageText);
         Connector.Send(SendBytes);
         Connector.Close();
         ChatBox.BeginInvoke(AcceptDelegate, new object[] { "Send " + MessageText, ChatBox });
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Beispiel #2
0
        private void Network_ChatEventHandler(object sender, ChatEvent e)
        {
            ChatBox.BeginInvoke((MethodInvoker) delegate
            {
                int st = ChatBox.TextLength;
                ChatBox.SelectionFont      = BoldFont;
                ChatBox.SelectionBackColor = Color.DarkCyan;
                ChatBox.AppendText(e.message.OwnerName + ":");

                ChatBox.Select(st, ChatBox.TextLength - st);
                ChatBox.SelectionFont = StdFont;
                ChatBox.DeselectAll();

                st = ChatBox.TextLength;
                ChatBox.AppendText(e.message.Message + "\r\n");
                ChatBox.Select(st, ChatBox.TextLength - st);
                ChatBox.SelectionBackColor = Color.Black;
                ChatBox.DeselectAll();

                ChatBox.ScrollToCaret();

                AddMessage(e.message);
            });

            if (e.message.MessageID == IncrementID)
            {
                ChatBoxMessage.Invoke((MethodInvoker) delegate
                {
                    ChatBoxMessage.Text = "";
                    IncrementID++;
                    ChatBoxMessage.Focus();
                });
            }
        }
Beispiel #3
0
        public void Receiver()
        {
            TcpListener Listen = new TcpListener(7000);

            //TcpListener Listen=new TcpListener(IPAddress.Parse(IP.Text),7000);

            Listen.Start();


            while (true)
            {
                try
                {
                    ReceiveSocket = Listen.AcceptSocket();

                    byte[] Receive = new byte[256];
                    using (MemoryStream MessageR = new MemoryStream())
                    {
                        Int32 ReceivedBytes;
                        do
                        {
                            ReceivedBytes = ReceiveSocket.Receive(Receive, Receive.Length, 0);
                            MessageR.Write(Receive, 0, ReceivedBytes);
                        } while (ReceiveSocket.Available > 0);
                        ChatBox.BeginInvoke(AcceptDelegate,
                                            new object[]
                        {
                            "Received " + Encoding.Default.GetString(MessageR.ToArray())
                            ,
                            ChatBox
                        });
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }