How can I listen for new connection as well as lisstening for ongoing communication with TCPIP? I have the following code which properly listen for new connections, sends and receive data. However, if the client application sends new data from an existing connection I don't see anything.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.Net.Sockets;
using System.Threading;

using System.IO;
using System.Xml;
using System.Xml.Linq;
//using XMLGenerator;
using XmlTools;
using System.Collections.Generic;


// Command Message Ping
namespace TestCase2
{
    class Program
    {
        private static StreamWriter sw;
        private static bool performPing = true;

        static void Main(string[] args)
        {

            try
            {
                int myClock;
                sw = new StreamWriter("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\Logs\\TestCase2_log_" + DateTime.Now.Millisecond.ToString() + ".xml", true);

                IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
                IPHost.AddressList[0].ToString();
                IPAddress localAddress = IPAddress.Parse(IPHost.AddressList[0].ToString());

                //IPAddress localAddress = IPAddress.Parse("127.0.0.1");

                // Define the kind of socket we want: Internet, Stream, TCP
                Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Define the address we want to claim: the first IP address found earlier, at port 2200
                IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 2200);

                // Bind the socket to the end point
                listenSocket.Bind(ipEndpoint);

                // Start listening, only allow 10 connection to queue at the same time
                listenSocket.Listen(10);
                listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket);
                //Console.WriteLine("Server is waiting on socket {0}", listenSocket.LocalEndPoint);


                myClock = 0;
                while (true)
                {
                    // Write a message and sleep for 2 seconds
                    //Console.WriteLine("Busy Waiting - Clock: " + myClock.ToString() + " seconds");
                    Thread.Sleep(2000);
                    myClock += 2;
                    if (myClock == 200)
                    {
                        sw.Close();
                        Environment.Exit(1);
                    }
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Caught Exception: {0}", e.ToString());
            }
        }


        public static void ReceiveCallback(IAsyncResult AsyncCall)
        {
            
           
            try
            {
                Socket listener = (Socket)AsyncCall.AsyncState;
                Socket client = listener.EndAccept(AsyncCall);

                //if (performPing)
                //{

                   
                    
                    // Sending Command Message Ping
                    Byte[] message = XmlConverter.ConvertStringToByteArray("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\CommandMessage_Ping_Request.xml");
                    sw.WriteLine("Sending Ping Command to " + client.RemoteEndPoint.ToString() + "on " + DateTime.Now.Millisecond.ToString());
                    sw.WriteLine(System.Text.Encoding.UTF8.GetString(message));
                    Console.WriteLine("Sending Ping Command to {0}", client.RemoteEndPoint);
                    //Console.WriteLine(System.Text.Encoding.UTF8.GetString(message));
                    client.Send(message);
                    performPing = false;

                    do
                    {
                        // Receiving
                        byte[] myBuffer = new byte[100000];
                        client.Receive(myBuffer);
                        BinaryWriter binWriter = new BinaryWriter(File.Open("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\XmlReceived.xml", FileMode.Append));
                        binWriter.Write(myBuffer);
                        binWriter.Close();
                        sw.WriteLine("Received Connection from " + client.RemoteEndPoint + "on " + DateTime.Now.Millisecond.ToString());
                        sw.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer));
                        Console.WriteLine("Waiting for ping/n");
                        Console.WriteLine("Received Connection from {0}", client.RemoteEndPoint);
                        Console.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer));
                        Thread.Sleep(50);
                       

                    } while (true);
                //}
             
                //else
                //{

                //    // Receiving response
                //    byte[] myBuffer2 = new byte[100000];
                //    client.Receive(myBuffer2);
                //    //Console.WriteLine("Receiving response from {0}", client.RemoteEndPoint);
                //    sw.WriteLine("Received response from " + client.RemoteEndPoint + "on " + DateTime.Now.Millisecond.ToString());
                //    sw.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer2));

                //    BinaryWriter binWriter = new BinaryWriter(File.Open("C:\\Users\\mathieu\\Documents\\Visual Studio 2010\\Projects\\MasterTester\\TestCase2\\XmlFiles\\XmlReceived.xml", FileMode.Append));
                //    binWriter.Write(myBuffer2.Length);
                //    binWriter.Write(myBuffer2);
                //    binWriter.Close();
                //    Console.WriteLine("Received response from {0}", client.RemoteEndPoint);
                //    //Console.WriteLine(System.Text.Encoding.UTF8.GetString(myBuffer2));
                //}

                //// At the end of the connection, we need to tell the OS that we can receive another call

                //listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener);
                    listener.Close();

                            
               
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }



        }


    }
}

You need to handle incoming connections on one thread, then handle each connection on an individual thread. This tutorial is a really good starting point for a TCP client-server model.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.