private void InitSocket()
        {
            try
            {
                tbxLocalIpAddress.Text =
                    Regex.Replace(tbxLocalIpAddress.Text, @"[^0-9].[^0-9].[^0-9].[^0-9]", "");
                tbxPort.Text = Regex.Replace(tbxPort.Text, @"[^0-9]", "");
                if (tbxLocalIpAddress.Text == "" || tbxPort.Text == "")
                {
                    throw new Exception();
                }
                TcpListner = new TcpListener(IPAddress.Parse(tbxLocalIpAddress.Text.Trim()), Int32.Parse(tbxPort.Text));
                TcpClient  = default(TcpClient);
                TcpListner.Start();
                DisplayText(">> server Started");
                while (true)
                {
                    try
                    {
                        counter++;//무슨 기능이 있는지 잘 모르겠음.
                        TcpClient = TcpListner.AcceptTcpClient();

                        this.Invoke(new DeligateGetClientIP(GetClientIP));

                        NetworkStream stream  = TcpClient.GetStream();
                        byte[]        buffer  = new byte[1024];
                        int           bytes   = stream.Read(buffer, 0, buffer.Length);
                        string        channel = Encoding.Unicode.GetString(buffer, 0, bytes);
                        channel = channel.Substring(0, channel.IndexOf("$"));

                        clientList.Add(TcpClient, channel);

                        ipPoint  = (IPEndPoint)_tcpClient.Client.RemoteEndPoint;
                        ClientIP = ipPoint.Address.ToString();
                        SendMessageAll(ClientIP + ":" + ipPoint.Port + "/", channel, false);

                        HandleClient h_client = new HandleClient();
                        h_client.OnReceived     += new HandleClient.MessageDisplayHandler(OnReceived);
                        h_client.OnDisconnected += new HandleClient.DisconnectedHandler(HandlerClientsOnDisconnected);
                        h_client.startClient(TcpClient, clientList);
                    }
                    catch (SocketException se)
                    {
                        MessageBox.Show(se.ToString());
                        throw new Exception();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        throw new Exception("");
                    }
                }
                TcpClient.Close();
                TcpListner.Stop();
            }
            catch
            {
                MessageBox.Show("Local IP Address 또는 Port 번호가 올바르지 않습니다.");
                this.Invoke(new DeligateButtonChange(ButtonStatusChange));
            }
        }
Beispiel #2
0
        private void InitSocket()
        {
            try
            {
                tbxLocalIpAddress.Text =
                    Regex.Replace(tbxLocalIpAddress.Text, @"[^0-9].[^0-9].[^0-9].[^0-9]", "");
                tbxPort.Text = Regex.Replace(tbxPort.Text, @"[^0-9]", "");
                if (tbxLocalIpAddress.Text == "" || tbxPort.Text == "")
                {
                    MessageBox.Show("Local IP Address 또는 Port 번호가 올바르지 않습니다.");
                    throw new Exception();
                }
                TcpListner = new TcpListener(IPAddress.Parse(tbxLocalIpAddress.Text.Trim()), Int32.Parse(tbxPort.Text));
                TcpClient  = default(TcpClient);
                TcpListner.Start();
                DisplayText(">> server Started");
                while (true)
                {
                    try
                    {
                        TcpClient = TcpListner.AcceptTcpClient();

                        NetworkStream stream = TcpClient.GetStream();

                        stream = TcpClient.GetStream();//acc.Receive(sizeBuf, 0, sizeBuf.Length, 0);

                        byte[] sizeBuf = new byte[TcpClient.ReceiveBufferSize];
                        stream.Read(sizeBuf, 0, (int)TcpClient.ReceiveBufferSize);
                        int size = BitConverter.ToInt32(sizeBuf, 0);

                        MemoryStream ms = new MemoryStream();

                        while (size > 0)
                        {
                            byte[] buffer;
                            if (size < TcpClient.ReceiveBufferSize) //acc.ReceiveBufferSize)
                            {
                                buffer = new byte[size];
                            }
                            else
                            {
                                buffer = new byte[TcpClient.ReceiveBufferSize];
                            }

                            int rec = stream.Read(buffer, 0, buffer.Length);

                            size -= rec;
                            ms.Write(buffer, 0, buffer.Length);
                        }
                        ms.Close();

                        byte[] data = ms.ToArray();

                        ms.Dispose();

                        string channel = Encoding.UTF8.GetString(data);


                        this.Invoke(new DeligateGetClientIP(GetClientIP), channel);

                        ClientList.Add(TcpClient, channel);

                        IpPoint  = (IPEndPoint)TcpClient.Client.RemoteEndPoint;
                        ClientIP = IpPoint.Address + ":" + IpPoint.Port;
                        ClientInfoList.Add(ClientIP.ToString());

                        SendMessageAll(ClientIP + "/ 님이 연결되었습니다.", channel, true);

                        HandleClient handle = new HandleClient();
                        handle.OnReceived     += new HandleClient.MessageDisplayHandler(OnReceived);
                        handle.OnDisconnected += new HandleClient.DisconnectedHandler(HandlerClientsOnDisconnected);
                        handle.StartClient(TcpClient, ClientList);
                    }
                    catch
                    {
                        throw new SocketException();
                    }
                }
            }
            catch (SocketException ex)
            {
                this.Invoke(new DeligateDisconnect(Disconnect));
                ServerEvent.ErrorLog("InitSocket", ex.Message);
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("InitSocket", ex.Message);
            }
            finally
            {
                this.Invoke(new DeligateButtonChange(ButtonStatusChange));
            }
        }
Beispiel #3
0
        private void InitSocket()
        {
            try
            {
                tbxLocalIpAddress.Text =
                    Regex.Replace(tbxLocalIpAddress.Text, @"[^0-9].[^0-9].[^0-9].[^0-9]", "");
                tbxPort.Text = Regex.Replace(tbxPort.Text, @"[^0-9]", "");
                if (tbxLocalIpAddress.Text == "" || tbxPort.Text == "")
                {
                    MessageBox.Show("Local IP Address 또는 Port 번호가 올바르지 않습니다.");
                    throw new Exception();
                }
                TcpListner = new TcpListener(IPAddress.Parse(tbxLocalIpAddress.Text.Trim()), Int32.Parse(tbxPort.Text));
                TcpClient  = default(TcpClient);
                TcpListner.Start();
                DisplayText(">> server Started");
                while (true)
                {
                    try
                    {
                        TcpClient = TcpListner.AcceptTcpClient();

                        NetworkStream stream  = TcpClient.GetStream();
                        byte[]        buffer  = new byte[1024];
                        int           bytes   = stream.Read(buffer, 0, buffer.Length);
                        string        channel = Encoding.Unicode.GetString(buffer, 0, bytes);
                        channel = channel.Split('$')[0];

                        this.Invoke(new DeligateGetClientIP(GetClientIP), channel);

                        ClientList.Add(TcpClient, channel);

                        IpPoint  = (IPEndPoint)TcpClient.Client.RemoteEndPoint;
                        ClientIP = IpPoint.Address + ":" + IpPoint.Port;
                        ClientInfoList.Add(ClientIP.ToString());
                        SendMessageAll(ClientIP + "/ 님이 연결되었습니다.", channel);

                        HandleClient handle = new HandleClient();
                        handle.OnReceived     += new HandleClient.MessageDisplayHandler(OnReceived);
                        handle.OnDisconnected += new HandleClient.DisconnectedHandler(HandlerClientsOnDisconnected);
                        handle.StartClient(TcpClient, ClientList);
                    }
                    catch
                    {
                        throw new SocketException();
                    }
                }
            }
            catch (SocketException ex)
            {
                this.Invoke(new DeligateDisconnect(Disconnect));
                serverEvent.ErrorLog("InitSocket", ex.Message);
            }
            catch (Exception ex)
            {
                serverEvent.ErrorLog("InitSocket", ex.Message);
            }
            finally
            {
                this.Invoke(new DeligateButtonChange(ButtonStatusChange));
            }
        }