Sending a packet in C# via TCP?

17,404

This is the example from https://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

static void Connect(String server, String message) 
{
  try 
  {
    // Create a TcpClient.
    // Note, for this client to work you need to have a TcpServer 
    // connected to the same address as specified by the server, port
    // combination.
    Int32 port = 13000;
    TcpClient client = new TcpClient(server, port);

    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);         

    // Get a client stream for reading and writing.
   //  Stream stream = client.GetStream();

    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length);

    Console.WriteLine("Sent: {0}", message);         

    // Receive the TcpServer.response.

    // Buffer to store the response bytes.
    data = new Byte[256];

    // String to store the response ASCII representation.
    String responseData = String.Empty;

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);
    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    Console.WriteLine("Received: {0}", responseData);         

    // Close everything.
    stream.Close();         
    client.Close();         
  } 
  catch (ArgumentNullException e) 
  {
    Console.WriteLine("ArgumentNullException: {0}", e);
  } 
  catch (SocketException e) 
  {
    Console.WriteLine("SocketException: {0}", e);
  }

  Console.WriteLine("\n Press Enter to continue...");
  Console.Read();
}
Share:
17,404
xozijow
Author by

xozijow

Updated on July 13, 2022

Comments

  • xozijow
    xozijow almost 2 years

    I'll try and explain my question below, I have tried googling for an answer but firstly, I don't really know what I should be googling and I haven't found anything that makes sense to me, I was wondering if someone could explain it? Many Thanks.

    Hello. I am trying to send a simple network packet using TCP, I have done it using UDP pretty easily as its really easy with UDP, I was wondering if anyone could help me do the equivalent in TCP? I tried using a TcpClient, but it doesn't have a Send method the same as UDP?

    public void OnUdp()
    {
        var client = new UdpClient(Host, Port);
        client.Send(rubbish, rubbish.Length);
    }
    
    • spodger
      spodger over 6 years
      Try reading the TcpClient documentation. It has an example. msdn.microsoft.com/en-us/library/…
    • Umair M
      Umair M over 6 years
      Have a read on this one too
    • jdweng
      jdweng over 6 years
      See the msdn examples. The example uses TCP with the socket class. You can replace the Socket Class with any Class that inherits the socket like TCPListener and TCPClient : docs.microsoft.com/en-us/dotnet/framework/network-programmin‌​g/…
    • spender
      spender over 6 years
      TCP provides a connection oriented communication over an underlying packet oriented datagram network. The concept of "sending" a TCP "packet" is miguided.
    • Tom Blodget
      Tom Blodget over 6 years
      Agreeing with @spender, see this comparison. With TCP, you send a stream of bytes. The receiver gets none or some at a time and has to buffer until it can process an actionable sequence of bytes. The trivial case, when each byte is individually actionable, is rare.
  • jdweng
    jdweng over 6 years
    This is a very bad example. It closes the connection after one message. For beginners it will cause a lot of grief.
  • Tom Blodget
    Tom Blodget over 6 years
    It also assumes that the message has only characters that are also in the ASCII character set, without any parameter validation. That makes it too easy to silently corrupt the data.