1

I am attempting to interop with a custom hardware device.

It requires the first TCP packet to contain a data payload

I tried to achieve this using TcpClient in System.Net.Sockets

   TcpClient tcpclnt = new TcpClient();
   tcpclnt.Connect("192.168.1.11", 1500);
   Stream stm = tcpclnt.GetStream();
   byte[] ba = messagestr.StringToByteArray();                   
   stm.Write(ba, 0, ba.Length);

However wireshark shows that this code sends a number of TCP/IP packets to establish a connection and the data packet (containing messagestr) is the 4-5th packet.

How can I make C# send the data in the FIRST TCP packet?

GreyCloud
  • 3,030
  • 5
  • 32
  • 47
  • If you don't want TCP to establish a connection, are you sure that your requirement is to create a TCP socket? Don't you need a raw socket? – CodeCaster Oct 02 '15 at 13:30
  • wiresharking the *correct* packets I am trying to imitate shows them to be well formed TCP packets (though just 1 sent / 1 received) – GreyCloud Oct 02 '15 at 13:32
  • 1
    If that really is the protocol that the hardware device implements, I suspect the creators to have been high on something. How does the SYN/ACK/sequence flow work then? Anyway check [TCP/IP Raw Sockets](https://msdn.microsoft.com/en-us/library/windows/desktop/ms740548(v=vs.85).aspx), [How do you get raw TCP packet in C#?](http://stackoverflow.com/questions/11034870/how-do-you-get-raw-tcp-packet-in-c), [Send TCP packet in C#](http://stackoverflow.com/questions/4399473/send-tcp-packet-in-c-sharp). – CodeCaster Oct 02 '15 at 13:33
  • "I suspect the creators to have been high on something" yes I fully agree(!) this is a RS232 protocol embedded into TCP packets :( – GreyCloud Oct 02 '15 at 13:37
  • there is an ACK packet sent in response to the command (which is the first packet) – GreyCloud Oct 02 '15 at 13:39
  • 1
    If you want to send hand-crafted TCP packets you need to use the WinPcap library. There are Pcap wrappers for C# like SharpPcap available. – Robert Oct 02 '15 at 14:00
  • Thank you, use of the winpcap driver was the key solution for us – GreyCloud Oct 09 '15 at 15:16

1 Answers1

3

Use UDP to send only one packed without establishing a session. You are literally asking TCP not to be TCP.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • wiresharking the correct packets I am trying to imitate shows them to be well formed TCP packets (though just 1 sent / 1 received) ... should I therefore be trying to create a TCP packet using the UDP protocol? – GreyCloud Oct 02 '15 at 13:34