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(); //DisplayText(">> Accept connection from client"); 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); SendMessageAll(channel + "Joined ", "", 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) { //Trace.WriteLine(string.Format("InitSocket - SocketException : {0}", se.Message)); break; } catch (Exception ex) { //Trace.WriteLine(string.Format("InitSocket - Exception : {0}", ex.Message)); break; } } TcpClient.Close(); TcpListner.Stop(); } catch { MessageBox.Show("Local IP Address 또는 Port 번호가 올바르지 않습니다."); } }
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(); } //IPAddress serverIPAddress = IPAddress.Parse(tbxLocalIpAddress.Text.Trim()); //IPEndPoint serverEndPoint = new IPEndPoint(serverIPAddress, Int32.Parse(tbxPort.Text)); _tcpListner = new TcpListener(IPAddress.Parse(tbxLocalIpAddress.Text.Trim()), Int32.Parse(tbxPort.Text)); _tcpClient = default(TcpClient); //_tcpListner = new TcpListener(IPAddress.Any, Int32.Parse(tbxPort.Text)); _tcpListner.Start(); DisplayText(">> server Started"); /* * _connectSocket = serverEvent.SetupSocket(); * _connectSocket.Bind(serverEndPoint); * _connectSocket.Listen(1000); * * ThreadStart threadDelegate = new ThreadStart(ReceiveDataThread); * Thread playThread = new Thread(threadDelegate); * playThread.Start(); */ while (true) { try { counter++; _tcpClient = _tcpListner.AcceptTcpClient(); DisplayText(">> Accept connection from client"); //ClientIP = _tcpClient.RemoteEndPoint.ToString(); 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); SendMessageAll(channel + "Joined ", "", false); handleClient h_client = new handleClient(); h_client.OnReceived += new handleClient.MessageDisplayHandler(OnReceived); h_client.OnDisconnected += new handleClient.DisconnectedHandler(h_client_OnDisconnected); h_client.startClient(_tcpClient, clientList); } catch (SocketException se) { //Trace.WriteLine(string.Format("InitSocket - SocketException : {0}", se.Message)); break; } catch (Exception ex) { //Trace.WriteLine(string.Format("InitSocket - Exception : {0}", ex.Message)); break; } } _tcpClient.Close(); _tcpListner.Stop(); } catch { MessageBox.Show("Local IP Address 또는 Port 번호가 올바르지 않습니다."); //isConnect = false; } }