[RESOLVED] Need help with UDP server code
I'm having a few problems with this code
1. first, when I click start server and stop it, it gives no exception even though the ports in use (I have it in use to make it throw an error when binding )
2. When I click Start server the 2nd time, I get the following error:
Code:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
Here is my code:
Code:
public partial class Form1 : Form
{
private int server_port;
public Thread serverThread;
bool isRunning;
/*
* add_log()
* handle logging
*/
delegate void add_log_d(string text);
private void add_log(string text)
{
if (this.txtLog.InvokeRequired)
{
this.txtLog.Invoke(new add_log_d(this.add_log), new object[] { text });
}
else
{
this.add_log(text);
}
}
/*
* StartRecvFrom()
* - Start listening for data and handle it.
*/
public void StartRecvFrom()
{
IPHostEntry localhost;
try
{
// bind to localhost
Socket sckUdp = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
try
{
localhost = Dns.GetHostByName( Dns.GetHostName() );
}
catch( Exception e )
{
add_log(">> Error - " + e.ToString() + "\n");
return;
}
IPEndPoint localIP = new IPEndPoint(localhost.AddressList[0], server_port );
sckUdp.Bind( localIP );
// begin listening for data
while( true )
{
byte[] received = new byte[32768];
IPEndPoint tmpIP = new IPEndPoint( localhost.AddressList[0], server_port );
EndPoint remoteIP = (tmpIP);
int bytes_received = sckUdp.ReceiveFrom( received, ref remoteIP );
string data = System.Text.Encoding.ASCII.GetString( received );
add_log(">> Received packet from " + remoteIP.ToString() + ":\n" + data + "\n\n");
}
}
catch( SocketException se )
{
add_log(">> Socket Exception occurred - " + se.ToString() + "\n");
}
}
/*
* StartUDPServer
* - Start the server thread.
*/
public void StartUDPServer()
{
try
{
serverThread = new Thread(new ThreadStart(StartRecvFrom));
serverThread.IsBackground = true;
serverThread.Start();
isRunning = true;
}
catch (Exception e)
{
add_log(">> Error starting server - " + e.ToString() + "\n");
serverThread.Abort();
isRunning = false;
}
}
/*
* StopUDPServer()
* - Stop the UDP server thread.
*/
public void StopUDPServer()
{
try
{
serverThread.Abort();
isRunning = false;
}
catch (Exception e)
{
add_log(">> Error stopping server - " + e.ToString() + "\n");
}
}
private void btnStartStop_Click(object sender, EventArgs e)
{
if (btnStartStop.Text == "Start Server")
{
btnStartStop.Text = "Stop Server";
StartUDPServer();
}
else if ( btnStartStop.Text == "Stop Server" )
{
btnStartStop.Text = "Start Server";
StopUDPServer();
}
}
private void Form1_Load(object sender, EventArgs e)
{
btnStartStop.Text = "Start Server";
isRunning = false;
server_port = 21000;
}
public Form1()
{
InitializeComponent();
}
}
What am I doing wrong? It's probably something stupid.
Re: Need help with UDP server code
You could try this instead.
Code:
private void add_log(string text)
{
Invoke(new MethodInvoker(delegate { txtLog.Items.Add(text); }));
}
However, you're going to still get another exception. You're already bound to a socket and trying to bind again, but that code should take care of the overflow.
Re: Need help with UDP server code
Okay, got it working now thanks to that code. Also fixed the other exception.
Marking thread resolved. Thanks for the help.