예제 #1
0
        private void ReceiveIo()
        {
            var receive = new TcpListener(System.Net.IPAddress.Any, tPortNumber);

            receive.Start();

            while (true)
            {
                TcpClient cl = new TcpClient();
                cl = receive.AcceptTcpClient();

                var handle = new clsChild(cl);
                handle.ReceivedMessage   += handle_ReceivedMessage;
                handle.LostConnectedPeer += handle_LostConnectedPeer;
                var tsk = new Thread(handle.task);
                tsk.Start();
                var       e       = new IPEventArgs();
                IPAddress ipa     = IPAddressConvert.GetRemoteIPAddress(cl);
                string    portnum = IPAddressConvert.GetRemotePortNumber(cl);

                e.IPAddress  = ipa;
                e.PortNumber = portnum;

                ConnectedPeerHandler(e);
            }
        }
예제 #2
0
            public void task()
            {
                IPAddress ipa     = IPAddressConvert.GetRemoteIPAddress(soc);
                string    portnum = IPAddressConvert.GetRemotePortNumber(soc);

                try
                {
                    NetworkStream s = soc.GetStream();

                    string str = "";
                    byte[] buf = new byte[4000 * 3000 * 3];
                    int    len = s.Read(buf, 0, buf.Length);
                    while (len > 0)
                    {
                        var tbuf = new byte[buf.Length - 1];
                        for (int i = 0; i < buf.Length - 1; i++)
                        {
                            tbuf[i] = buf[i + 1];
                        }
                        if (buf[0] == (byte)'i')//イメージ
                        {
                            var img = ByteArrayToImage(tbuf);
                            var e   = new MessageEventArgs();
                            e.Type       = MessageType.Image;
                            e.Image      = img;
                            e.Message    = null;
                            e.IPAddress  = ipa;
                            e.PortNumber = portnum;
                            ReceivedMessageHandler(e);
                        }
                        else if (buf[0] == (byte)'m')//メッセージ
                        {
                            str = Encoding.UTF8.GetString(tbuf, 0, len - 1);
                            var e = new MessageEventArgs();
                            e.Type      = MessageType.Message;
                            e.Image     = null;
                            e.Message   = str;
                            e.IPAddress = ipa;
                            ReceivedMessageHandler(e);
                        }
                        len = s.Read(buf, 0, buf.Length);
                    }
                    var e2 = new IPEventArgs();
                    e2.IPAddress = ipa;
                    LostConnectedPeerHandler(e2);
                }
                catch (Exception)
                {
                    var e2 = new IPEventArgs();
                    e2.IPAddress = ipa;
                    LostConnectedPeerHandler(e2);
                }
            }