Hi,
Im having a problem with some socket programming and im hoping someone here can help me.
basically im trying to bind a listener to a port. I have used some source code i obtained from the net, however the code was a few years old and contained the obsolete "Dns.Resolve(string.Empty);" method to get the IPHostEntry information.
Ive tried to replace this with "Dns.GetHostEntry(string.Empty);" however my code does not seem to be excuting past this point so im wondering if i have a logical error.
this is the original method
private void AcceptConnections()
{
IPHostEntry localMachineInfo;
IPEndPoint localEndPoint;
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
localMachineInfo = Dns.Resolve(string.Empty);
localEndPoint = new IPEndPoint(localMachineInfo.AddressList[0], _port);
// Bind the listener socket to a local port
listener.Bind(localEndPoint);
// Set the socket in a 'listener' mode
listener.Listen(10);
// Continue accepting and processing new connections indefinitely
while (true)
{
threadEvent.Reset();
// Begin listening for a new connection. When a new connection arrives,
// a new thread will invoke the BeginSocketConversation method to handle the socket conversation
listener.BeginAccept(new AsyncCallback( BeginSocketConversation), listener);
// Wait for a connection to arrive. When the connection arrives and is succesfully transferred
// to a new thread, the new thread will signal this thread to begin listening again
threadEvent.WaitOne();
}
}
catch (Exception)
{
}
}
this is my new altered method that does not seem to be executing
/// Listen for inbound TCP socket connections and spawn new threads to manage them
/// </summary>
private void AcceptConnections()
{
IPHostEntry localMachineInfo;
IPEndPoint localEndPoint;
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// print message to screen to verify code has reached this stage in execution
string temp3 = "Accept connections executing";
DialogTest(temp3);
try
{
localMachineInfo = Dns.GetHostEntry(string.Empty);
localEndPoint = new IPEndPoint(localMachineInfo.AddressList[0], _port);
// Bind the listener socket to a local port
listener.Bind(localEndPoint);
// Set the socket in a 'listener' mode
listener.Listen(10);
string temp4 = "socket listener bound";
DialogTest(temp4);
// Continue accepting and processing new connections indefinitely
while (true)
{
threadEvent.Reset();
// Begin listening for a new connection. When a new connection arrives,
// a new thread will invoke the BeginSocketConversation method to handle the socket conversation
listener.BeginAccept(new AsyncCallback(BeginSocketConversation), listener);
string temp5 = "Begin socket conversation executing";
DialogTest(temp5);
// Wait for a connection to arrive. When the connection arrives and is succesfully transferred
// to a new thread, the new thread will signal this thread to begin listening again
threadEvent.WaitOne();
}
}
catch (Exception)
{
}
}
basically the only difference is at line 8 in the original code, and line 17 in my new code, where i have tried to replace the Dns.Resolve with a Dns.GetHostEntry.plus i am using a method called DialogTest() to print messages to the screen so i can tell how far the code is executing.
i know the AcceptConnections() method is being called and starting to run because i recieve the
"Accept connections executing" message printed to screen, however the try statment does not seem to be executing properly.
does anyone know how i can properly replace this line of code
"localMachineInfo = Dns.Resolve(string.Empty);
with something that is logically correct and will work?
or can anyone see anything else they think might be preventing proper execution?
Many thanks