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(); byte[] sizeBuf = new byte[TcpClient.ReceiveBufferSize]; stream.Read(sizeBuf, 0, (int)TcpClient.ReceiveBufferSize); int size = BitConverter.ToInt32(sizeBuf, 0); MemoryStream memoryStream = new MemoryStream(); while (size > 0) { byte[] buffer; if (size < TcpClient.ReceiveBufferSize) { buffer = new byte[size]; } else { buffer = new byte[TcpClient.ReceiveBufferSize]; } int rec = stream.Read(buffer, 0, buffer.Length); size -= rec; memoryStream.Write(buffer, 0, buffer.Length); } memoryStream.Close(); byte[] data = memoryStream.ToArray(); memoryStream.Dispose(); string channel = Encoding.UTF8.GetString(data); this.Invoke(new DeligateGetClientIP(GetClientIP), channel); ClientList.Add(TcpClient, channel); SendMessageAll(IpPoint.Address + ":" + IpPoint.Port + " 님이 연결되었습니다.", channel, true); HandleClient handle = new HandleClient(); handle.OnReceived += new HandleClient.MessageDisplayHandler(OnReceived); handle.OnDisconnected += new HandleClient.DisconnectedHandler(OnDisconnected); 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)); } }