TcpListener not accepting clients
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: TcpListener not accepting clients

Hybrid View

  1. #1
    Join Date
    Dec 2011
    Posts
    10

    TcpListener not accepting clients

    I'll keep it short and simple. Take a look at my source.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    
    namespace EDOLOGGER_SERVER
    {
        public partial class Form1 : Form
        {
            TcpListener server;
            int port = 8888;
            IPAddress IP = IPAddress.Parse("192.168.1.114");
            string n = Environment.NewLine;
            public void start()
            {
                try
                {
                    textBox1.Text += "in start()" + n;
                    server = new TcpListener(IP, port);
                    server.Start();
                }
                catch (System.Exception e)
                {
                    textBox1.Text += "EXCEPTION: " + e + n;
                }
            }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                textBox1.Text += "Before start()" + n;
                start();
                textBox1.Text += "After start()" + n;
                while (server.Pending() == true)
                {
                    try
                    {
                        textBox1.Text += "Listening for request on " + IP + ":" + port;
                        TcpClient client = server.AcceptTcpClient();
                        textBox1.Text += "Connection accepted" + n;
    
                    }
                    catch (System.Exception ex)
                    {
                        textBox1.Text += ex + n;
                    }
                }
            }
        }
    }
    The following code never runs (under "while (server.Pending == true)"):
    "Listening for request on " + IP + ":" + port;
    "Connection accepted" + n;
    My best guess is that I'm using server.Pending incorrectly, but don't know how to correct that. Thanks in advance

  2. #2
    Join Date
    Dec 2011
    Posts
    10

    Re: TcpListener not accepting clients

    I moved some code around, created buttons for starting the server and and listening for connections, and it works now. Can someone explain why? lol. thanks in advance. New source:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Sockets;
    using System.Net;
    
    namespace EDOLOGGER_SERVER
    {
        public partial class Form1 : Form
        {
            TcpListener server;
            int port = 8888;
            IPAddress IP = IPAddress.Parse("192.168.1.114");
            string n = Environment.NewLine;
            public void start()
            {
                try
                {
                    textBox1.Text += "in start()" + n;
                    server = new TcpListener(IP, port);
                    server.Start();
                    button1.Enabled = false;
                    button2.Enabled = true;
                    button3.Enabled = true;
                }
                catch (System.Exception e)
                {
                    textBox1.Text += "EXCEPTION: " + e + n;
                }
            }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Text += server.ToString() + n;
                textBox1.Invalidate();
                textBox1.Refresh();
                while (server.Pending() == true)
                {
                    try
                    {
                        textBox1.Text += "Listening for request on " + IP + ":" + port;
                        TcpClient client = server.AcceptTcpClient();
                        textBox1.Text += "Connection accepted" + n;
    
                    }
                    catch (System.Exception ex)
                    {
                        textBox1.Text += ex + n;
                    }
                }
                textBox1.Text += "Out of while" + n;
                button2.Enabled = false;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Text += "Before start()" + n;
                start();
                textBox1.Text += "After start()" + n;
            }
        }
    }

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: TcpListener not accepting clients

    As per documentation: http://msdn.microsoft.com/en-us/libr...r.pending.aspx

    That property only returns 'true' if someone has connected to the socket but 'AcceptTcpClient' has not been called. In your case you can either do an asynchronous 'BeginAcceptTcpClient' or a blocking wait by just calling AcceptTcpClient and waiting for someone to connect. You should not have a while loop checking this property unless you keep rechecking even if this returns false (which is what you want).
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center