@@ -39,6 +39,7 @@ enum Tcp
3939
4040 const int SIZE1K = 1024 ; //bytes
4141 const int TIMEOUT = 2000 ; //ms
42+ const int MAX_CLIENTS = 5 ;
4243
4344 static volatile bool serverStartButtonFlag = false ;
4445 static volatile bool clientStartButtonFlag = false ;
@@ -57,6 +58,12 @@ enum Tcp
5758 public MainWindow ( )
5859 {
5960 InitializeComponent ( ) ;
61+
62+ ip = Dns . GetHostEntry ( Dns . GetHostName ( ) ) . AddressList [ 2 ] . ToString ( ) ;
63+ Application . Current . Dispatcher . Invoke ( DispatcherPriority . Background , new Action ( ( ) =>
64+ {
65+ serverIP . Text = ip ;
66+ } ) ) ;
6067 }
6168
6269
@@ -66,8 +73,9 @@ void TcpServerLoop()
6673 {
6774 bool closeFlag = false ;
6875
69- IPAddress localAddr = IPAddress . Parse ( ip ) ;
70- TcpListener server = new TcpListener ( localAddr , port ) ;
76+ IPAddress ipAddress = Dns . GetHostEntry ( Dns . GetHostName ( ) ) . AddressList [ 0 ] ;
77+ IPEndPoint ipLocalEndPoint = new IPEndPoint ( ipAddress , port ) ;
78+ TcpListener server = new TcpListener ( ipLocalEndPoint ) ;
7179 TcpClient client = null ;
7280
7381 byte [ ] bytesToSend = new byte [ SIZE1K ] ;
@@ -83,7 +91,17 @@ void TcpServerLoop()
8391 sendingString = "" ;
8492 receivingString = "" ;
8593
86- server . Start ( ) ;
94+ try
95+ {
96+ server . Start ( ) ;
97+ }
98+ catch ( SocketException ex )
99+ {
100+ string tempString = "Socket Exception Error-code: " ;
101+ tempString = tempString + ex . ToString ( ) ;
102+ MessageBoxResult errorMessageBox = MessageBox . Show ( tempString , "Error" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
103+ }
104+ //server.Start();
87105 client = server . AcceptTcpClient ( ) ;
88106 client . ReceiveTimeout = TIMEOUT ;
89107 client . SendTimeout = TIMEOUT ;
@@ -152,8 +170,10 @@ void TcpServerLoop()
152170 if ( client . Connected )
153171 {
154172 client . Close ( ) ;
173+ client = null ;
155174 }
156175 server . Stop ( ) ;
176+ server = null ;
157177 }
158178 catch { }
159179 serverState = Tcp . CONNECT ;
@@ -253,6 +273,7 @@ void TcpClientLoop()
253273 if ( client . Connected )
254274 {
255275 client . Close ( ) ;
276+ client = null ;
256277 }
257278 }
258279 catch { }
@@ -355,7 +376,6 @@ private void serverStartButton_Click(object sender, RoutedEventArgs e)
355376 serverTextboxBig . Text = "" ;
356377
357378 serverPort . IsEnabled = false ;
358- serverIP . IsEnabled = false ;
359379 clientTab . IsEnabled = false ;
360380
361381 sendingString = "" ;
@@ -385,7 +405,6 @@ private void serverStartButton_Click(object sender, RoutedEventArgs e)
385405
386406 clientTab . IsEnabled = true ;
387407 serverPort . IsEnabled = true ;
388- serverIP . IsEnabled = true ;
389408 }
390409 }
391410
0 commit comments