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();


              }       

          }     
3
  • Is this for a one time test or something you want built into the client? Commented Oct 15, 2010 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. Commented Oct 15, 2010 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. Commented Oct 15, 2010 at 18:30

1 Answer 1

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!!

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.