0

Any suggestions on testing a TCP client listener. What I'm looking for is actual network stats like "payload size", "time-out values", "actual payload bytes received from server".

  public static void tcpConnect()  { 
                int port = 12345;


                IPEndPoint ipep = new IPEndPoint(
                        IPAddress.Parse("xx.xx.xx.xx"), port);
                Socket server = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);
                IPGlobalProperties _ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
                TcpConnectionInformation[] _tcpConnInfoArray = _ipGlobalProperties.GetActiveTcpConnections();

               // bool isAvaiable = true;

                try
                {
                    server.Connect(ipep);                   
                }   
                catch (SocketException e)
                {
                    Console.WriteLine("Unable to connect to server");
                    Console.WriteLine(e.ToString());
                    return;
                }
                NetworkStream ns = new NetworkStream(server);
                if (ns.CanRead)
                {
                    foreach (TcpConnectionInformation tcpConnInfo in _tcpConnInfoArray)
                    {
                        if (tcpConnInfo.LocalEndPoint.Port == port)
                        {
                            //isAvaiable = false;
                            Console.WriteLine("Error: Can't Read from Port");
                            break;
                        }

                        Console.Write("Local endpoint: {0} ", tcpConnInfo.LocalEndPoint.Address);
                        Console.Write("Remote endpoint: {0} ", tcpConnInfo.RemoteEndPoint.Address);
                        Console.Write("{0}", tcpConnInfo.State);
                    }
                }
                else 
                {
                    Console.WriteLine("Error: Can't Read from Socket");
                    ns.Close();
                    return;
                }
                while (true)
                {

                   string input = Console.ReadLine();
                    if (input == "exit")
                        break;
                    if (ns.CanWrite)
                    {
                        Console.WriteLine("NS Can write");
                        ns.Flush();

                    }
                }
                Console.WriteLine("Disconnection from server");
                ns.Close();
                server.Shutdown(SocketShutdown.Both);
                server.Close();


              }       

          }     
AAH-Shoot
  • 627
  • 2
  • 14
  • 30
  • Is this for a one time test or something you want built into the client? – CrazyDart Oct 15 '10 at 15:06
  • Something that I would like to build into a client....because I'm having trouble determining if...i'm actually connected to the remote host. Not sure..how to verify that if I know the ip and port number with my client app. – AAH-Shoot Oct 15 '10 at 15:20
  • ...What i'm trying to do is read the "Payload data coming off that socket" and then write data to that socket. – AAH-Shoot Oct 15 '10 at 18:30

1 Answers1

1

So if I read correctly the real aim is to monitor the connection to see if it is still alive? We all know TCP is a state-full protocol. This means the "socket" must remain open. So you client will have a socket which is open. The TcpClient is just a nice wrapper that give you the ability to read and write to the socket. From the MS API page we can see there is an Active property. As the documentation reads, this is not updated if the remote disconnects... this is too bad and perhaps the problem you have. However they give the advise that if you want to watch the socket you will see if the remote disconnects. You can do this using the Connected property.

I know that not going to be the level of control you want, who wants to just see the TCP Client?? So get right at the Socket!!! once you have the socket you can have full control and viability over the connection. This should make you happy!!

Community
  • 1
  • 1
CrazyDart
  • 3,803
  • 2
  • 20
  • 29
  • Thanks for the information...this helps a whole lot. What I'm trying to do is read and write to that socket once I have established a good connection. – AAH-Shoot Oct 15 '10 at 18:23