0

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

Attachments code.jpg 222.27 KB
2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by jsnowden2008
0

What are you trying to do? Looking at this code I would say this is a bad idea:

localMachineInfo = Dns.Resolve(string.Empty);
localEndPoint = new IPEndPoint(localMachineInfo.AddressList[0], _port);

You have multiple local addresses on any given machine. Typically you have a LAN ip address of 192.168.x or 10.x.x.x and you also have a loopback address of 127.0.0.1 and with Vista and later you also have IPv6 addresses like 3ffe:401d:2042::. So blindly picking a local address to listen on is almost always a bad idea. There was a thread recently on a TCP client-server application for sending files:
http://www.daniweb.com/forums/thread228973.html

I would suggest binding to all interfaces on the machine with your desired port:

private TcpListener listener;
...
listener = new TcpListener(IPAddress.Any, DEFAULT_SERVER_PORT);

Take a look at the thread I mentioned and see if that helps you any. A functional application was also attached to the thread.

0

What are you trying to do? Looking at this code I would say this is a bad idea:

localMachineInfo = Dns.Resolve(string.Empty);
localEndPoint = new IPEndPoint(localMachineInfo.AddressList[0], _port);

You have multiple local addresses on any given machine. Typically you have a LAN ip address of 192.168.x or 10.x.x.x and you also have a loopback address of 127.0.0.1 and with Vista and later you also have IPv6 addresses like 3ffe:401d:2042::. So blindly picking a local address to listen on is almost always a bad idea. There was a thread recently on a TCP client-server application for sending files:
http://www.daniweb.com/forums/thread228973.html

I would suggest binding to all interfaces on the machine with your desired port:

private TcpListener listener;
...
listener = new TcpListener(IPAddress.Any, DEFAULT_SERVER_PORT);

Take a look at the thread I mentioned and see if that helps you any. A functional application was also attached to the thread.

Hi,

basically im writing an AddIn for windows media center. I want to be able to control media center remotely from my mobile phone. The socket listener is opened by the media center Addin and then waits for connections over a wifi connection from my mobile.

I'll check out that thread, cheers for the advice :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.