Reputation: 3
I have a specific packet of data I want to receive from a server. I am programming a program that will receive data from a server, my program can be used from any PC - being a client.
Contents of packet sent by server:
1) Packet ID - Byte
2) Player ID - SByte
3) Message - String
The packet ID is 0x0d.
I have looked at TCPListener examples but I'm really unsure how I can customise it to handle this packet specifically. If it helps at all, My client would have already connected to the server before it could receive any of these packets like this:
ConnectionSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IEP = new IPEndPoint(IPAddress.Any, port);
ConnectionSocket.Connect(serverAddress, port);
I would know what the server's port and IP is, if that should help with the TCPListener?
I hope somebody can help.
Thanks.
Upvotes: 0
Views: 1646
Reputation: 22301
If you are looking to mess around with the Identification field, you are not using standard TCP. To the application, a TCP connection is presented as a stream. You do not have access to the base packets. Furthermore, in a standard TCP connection, the Identification field is a random 16 bit field. 0x0d doesn't fill this width, and will most likely not be constant. I can imagine several other scenarios where you would need this sort of scenario, however.
First, if you wish to use a packet based protocol, try UDP. The UdpClient class (http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx) will allow you to receive a single datagram, which you can then manipulate.
Secondly, if you are, in fact, using TCP, and for some reason do need to get the packet with Identification 0x000d, you would need to sit much lower on the stack. Winsock exposes this through RAW sockets (http://msdn.microsoft.com/en-us/library/windows/desktop/ms740548(v=vs.85).aspx), but it seems as though that is disallowed for XP and beyond.
Thirdly, if you are looking for an application defined packet, with a field called PacketID, there will be a packet protocol superimposed upon TCP. In all likelyhood, there will be a StartOfPacket byte that has a constant value, and an EndOfPacket byte. There are, however, a thousand other ways the stream could be packetized, and you will have to check with the server implementation to determine the proper way to parse the stream. In any event, using either TcpListener, TcpClient or Socket, you will need to loop on reading on the stream into a buffer. After each read, scan the buffer for a valid packet, and process it. At that point, you can check the contents of the PacketID field based off of the offset into the packet.
Upvotes: 1