Question

I have a code in c# which reads the data from tcp ipaddress and port number but my problem is this that i dont have any testing plateform with ipaddress and port number to test the application.My concern is this that is there any way by which i can test my code in local environmnet..

Here is my code..

 public class Listener
{

    private TcpListener tcpListener;
    private Thread listenThread;
    // Set the TcpListener on port 8081.
    Int32 port = 8081;
    IPAddress localAddr = IPAddress.Parse("192.168.1.3");
    Byte[] bytes = new Byte[256];

    public void ListenForClients()
    {

        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication 
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }
    public void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                // System.Windows.MessageBox.Show("socket");
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                // System.Windows.MessageBox.Show("disc");
                break;
            }

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();

            String textdata = encoder.GetString(message, 0, bytesRead);
            System.IO.File.AppendAllText(@"D:\ipdata.txt", textdata);


        }

        tcpClient.Close();
    }
}

Please help me to solve this .. Thanks in advance..

Was it helpful?

Solution

To test the TCP listening application, you obviously need to write TCP transferring one. You can write it and:

  • Run them simultaneously

OR

  • Run listener on your OS and transmitter in virtual machine. I made so and has written a post on my blog on Russian (sources are also available for download).

OTHER TIPS

I think you can configure the listener to 127.0.0.1 and test logic via localhost connetcion if you have transmitting part of system

Use RESTClient chrome/firefox AddOn (http://restclient.net/) to send data to IP and Port in Intranet environment, I use this

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top