Windows Develop Bookmark and Share   
 index > Windows Forms General > Help with TCP Client
 

Help with TCP Client

I need to write a TCP consol application that does the following:

-Authenticates with TCP server
-Gets authentication message
-Sends a report
-waits for response.... if it succeeded
---Sends mulitple supplementary data streams
-etc.

Everything I've read with TCp/IP is that I'll need to have a method running in a background thread waiting for this response... however seeing that after I send it, I know i'll be getting one... could I not dO:

-Send message
-while(.... true for 30 seconds...)
{
//wait for response.
}
if reponse = good
send supplementary
while(...wait for 30 seconds...)
{

}

etc.

Would that work? Or is there a potential hazard there?

killer ninja coding monkeys really exist!
rternier  Tuesday, September 15, 2009 3:33 AM

Hi rternier,

Based on my understanding, we do not need to use a loop to wait. When we call a Read method of a NetworkStream, the method would not return until the server handles its request. The code snippet below shows my ideas:

    byte[] data = new byte[1024];
    string input, responseData;
    TcpClient server;

    //Connect to server and get network stream.
    try
    {
        server = new TcpClient("127.0.0.1", 9050);
    }
    catch (SocketException)
    {
        Console.WriteLine("Cannot connect to server");
        return;
    }
    NetworkStream networkStream = server.GetStream();
    //Set report content to the input variable.
    input = GetReportContent();
    //Send the report content.
    networkStream.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);

    //Get the response.
    int recv = networkStream.Read(data, 0, data.Length);
    responseData = Encoding.ASCII.GetString(data, 0, recv);
    if (responseData == "good")
    {
        //If response is good, send the supplementary.
        List<string> supplementary = GetSupplementary();
        foreach (string s in supplementary)
        {
            networkStream.Write(Encoding.ASCII.GetBytes(s), 0, s.Length);
            networkStream.Flush();
        }
    }

    //Close the connection.
    networkStream.Close();
    server.Close();

If you want to wait some time before some operations, you can call the Sleep method of the Thread to wait.

This is a sample of a tcp client:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx.
This is a project about TCP/IP Chat Application Using C#:
http://www.codeproject.com/KB/IP/TCPIPChat.aspx.

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 16, 2009 8:43 AM

Hi rternier,

Based on my understanding, we do not need to use a loop to wait. When we call a Read method of a NetworkStream, the method would not return until the server handles its request. The code snippet below shows my ideas:

    byte[] data = new byte[1024];
    string input, responseData;
    TcpClient server;

    //Connect to server and get network stream.
    try
    {
        server = new TcpClient("127.0.0.1", 9050);
    }
    catch (SocketException)
    {
        Console.WriteLine("Cannot connect to server");
        return;
    }
    NetworkStream networkStream = server.GetStream();
    //Set report content to the input variable.
    input = GetReportContent();
    //Send the report content.
    networkStream.Write(Encoding.ASCII.GetBytes(input), 0, input.Length);

    //Get the response.
    int recv = networkStream.Read(data, 0, data.Length);
    responseData = Encoding.ASCII.GetString(data, 0, recv);
    if (responseData == "good")
    {
        //If response is good, send the supplementary.
        List<string> supplementary = GetSupplementary();
        foreach (string s in supplementary)
        {
            networkStream.Write(Encoding.ASCII.GetBytes(s), 0, s.Length);
            networkStream.Flush();
        }
    }

    //Close the connection.
    networkStream.Close();
    server.Close();

If you want to wait some time before some operations, you can call the Sleep method of the Thread to wait.

This is a sample of a tcp client:
http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx.
This is a project about TCP/IP Chat Application Using C#:
http://www.codeproject.com/KB/IP/TCPIPChat.aspx.

Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, September 16, 2009 8:43 AM

You can use google to search for other answers

Custom Search

More Threads

• how to print next value horizontally in crystal rpt
• Ways of integration for 2 Windows Forms executables ( .Net 2.0 )
• Resolution Issues
• VS 2005 and Word 2007
• Delay for Enter Key C#
• Dimming a class based on a choice
• bottom text in a TextBox
• small, simple typeconverter not working...
• My program run fine on windows XP and didn't run on windows 2000
• DataGridViewButtonColumn -> ButtonColor