Skip to content

Commit 3093fcc

Browse files
committed
changed the server IP address to read only
automatically recognizes the server IP address
1 parent ab46eba commit 3093fcc

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

TCP_Server_Client_Tester/MainWindow.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@
8080

8181
<Label Content="Port:" Grid.Column="0" Grid.Row="0" VerticalAlignment="Bottom"/>
8282
<TextBox x:Name="serverPort" Text="233" Grid.Column="0" Grid.Row="1" Margin="5,0,5,10"/>
83-
<Label Content="IP:" Grid.Column="0" Grid.Row="2" VerticalAlignment="Bottom"/>
84-
<TextBox x:Name="serverIP" Text="192.168.144.1" Grid.Column="0" Grid.Row="3" Margin="5,0,5,10"/>
83+
<Label Content="IP (read only):" Grid.Column="0" Grid.Row="2" VerticalAlignment="Bottom"/>
84+
<TextBox x:Name="serverIP" Text="0.0.0.0" Grid.Column="0" Grid.Row="3" Margin="5,0,5,10" IsEnabled="false"/>
8585
<Button x:Name="serverStartButton" Content="Listen" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="LightGreen" FontWeight="Bold" Click="serverStartButton_Click"/>
8686
</Grid>
8787
</Grid>
@@ -160,7 +160,7 @@
160160
<Label Content="Port:" Grid.Column="0" Grid.Row="0" VerticalAlignment="Bottom"/>
161161
<TextBox x:Name="clientPort" Text="233" Grid.Column="0" Grid.Row="1" Margin="5,0,5,10"/>
162162
<Label Content="IP:" Grid.Column="0" Grid.Row="2" VerticalAlignment="Bottom"/>
163-
<TextBox x:Name="clientIP" Text="192.168.144.1" Grid.Column="0" Grid.Row="3" Margin="5,0,5,10"/>
163+
<TextBox x:Name="clientIP" Text="0.0.0.0" Grid.Column="0" Grid.Row="3" Margin="5,0,5,10"/>
164164
<Button x:Name="clientStartButton" Content="Connect" Grid.Column="0" Grid.Row="4" Margin="5,5,5,5" Background="LightGreen" FontWeight="Bold" Click="clientStartButton_Click"/>
165165
</Grid>
166166
</Grid>

TCP_Server_Client_Tester/MainWindow.xaml.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)