Author Topic: Packets  (Read 1635 times)

Hero

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • View Profile
    • http://rev7.net
Packets
« on: November 15, 2008, 08:38:45 AM »
I am curious how you go about sending and listening for packets packets in a .NET language. I sort-of understand it in VB (06). Can anyone show me how it's done?
Hero
AKA: HeroAssasin and Mike
- - - - -  - - -
Visit Clan R77
- - - - -  - - -
Please do not PM me with random questions. That is what I made these forums for.

Noob ~Vector

Pyro

  • Dumbass
  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Packets
« Reply #1 on: November 15, 2008, 01:22:41 PM »
It's incredibly simple.

Code: [Select]
byte[] data = Encoding.ASCII.GetBytes("ohai");
client.GetStream().Write(data, 0, data.Length);
Code: [Select]
NetworkStream ns = client.GetStream();
byte[] data = new byte[ns.Length];
ns.Read(data, 0, data.Length);
« Last Edit: November 15, 2008, 01:24:13 PM by Tony »

Hero

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • View Profile
    • http://rev7.net
Packets
« Reply #2 on: November 15, 2008, 02:20:25 PM »
client = ?

Encoding = System.Text.Encoding ?
Hero
AKA: HeroAssasin and Mike
- - - - -  - - -
Visit Clan R77
- - - - -  - - -
Please do not PM me with random questions. That is what I made these forums for.

Noob ~Vector

Pyro

  • Dumbass
  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Packets
« Reply #3 on: November 15, 2008, 04:29:53 PM »
client = System.Net.Sockets.TcpClient

Yes.

Hero

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • View Profile
    • http://rev7.net
Packets
« Reply #4 on: November 15, 2008, 05:14:33 PM »
Thanks.

Why do people use big Packet Buffer classes if it is that fast?
Hero
AKA: HeroAssasin and Mike
- - - - -  - - -
Visit Clan R77
- - - - -  - - -
Please do not PM me with random questions. That is what I made these forums for.

Noob ~Vector

Pyro

  • Dumbass
  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Packets
« Reply #5 on: November 16, 2008, 09:16:15 AM »
A packet buffer is basically a byte array with special functions making it easier to insert different types of data (strings, integers, etc) into it.

For example, the way you would put a string into a byte array would be:
Code: [Select]
byte[] buffer;
string a = "some string";
int t = buffer.Length;
Array.Resize(buffer, t + a.Length + 1);
Encoding.ASCII.GetBytes(a + "\n").CopyTo(buffer, t);
But with a packet buffer:
Code: [Select]
PacketBuf pak = new PacketBuf();
pak.InsertString("some string");

Hero

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • View Profile
    • http://rev7.net
Packets
« Reply #6 on: November 16, 2008, 09:43:01 AM »
How is TcpListener used?

So when you are receiving data you have to be listening to a port, right? and to do that you would want to do something like:

Code: [Select]
TcpClient client = new TcpClient("127.0.0.1", 1337)
NetworkStream stream = client.GetStream()

// Do I want to use a StreamReader/Writer ?
Hero
AKA: HeroAssasin and Mike
- - - - -  - - -
Visit Clan R77
- - - - -  - - -
Please do not PM me with random questions. That is what I made these forums for.

Noob ~Vector

Pyro

  • Dumbass
  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Packets
« Reply #7 on: November 16, 2008, 06:37:55 PM »
TcpListener is used for listening for new connections to your computer on a specified port.

It can be setup like this:
Code: [Select]
TcpListener listener = new TcpListener(IPAddress.Any, 1337);Whenever a new connection is pending on it, listener.Pending() will equal true. You can then accept the client (or socket) like this:
Code: [Select]
// Accept as TcpClient.
TcpClient newClient = listener.AcceptTcpClient();
or
Code: [Select]
// Accept as Socket.
Socket newSock = listener.AcceptSocket();

As for receiving data: if you want to receive from a remote host that you have a connection to through a TcpClient, then no, you don't; however, if you want to receive data from an unknown host (one you don't have a connection to) then yes, you need a TcpListener. (see above)

You can read from and write to a NetworkStream directly, you don't need a StreamWriter/Reader, simply stream.Read() and stream.Write().
« Last Edit: November 16, 2008, 06:38:51 PM by Tony »

Hero

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • View Profile
    • http://rev7.net
Packets
« Reply #8 on: November 16, 2008, 08:23:25 PM »
What is the point of connecting to something?

So this:
Code: [Select]
TcpListener listener = New TxpListener(IPAddress.Any, 1337);

// This on a timer
if (listener.Pending) {
  // Accept as TcpClient.
  TcpClient newClient = listener.AcceptTcpClient();
  NetworkStream newStream = newClient.GetStream();
  byte[] receivedData = new byte[newSteam.Length];
  newStream.Read(receivedData, 0, receivedData.Length);
  }

Would listen and read all packets received on the port 1337?
« Last Edit: November 16, 2008, 08:23:44 PM by Hero »
Hero
AKA: HeroAssasin and Mike
- - - - -  - - -
Visit Clan R77
- - - - -  - - -
Please do not PM me with random questions. That is what I made these forums for.

Noob ~Vector

Pyro

  • Dumbass
  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Packets
« Reply #9 on: November 16, 2008, 08:58:33 PM »
When you have a connection established to a remote host, there is a stream of data which you can send information to them, and likewise receive from them, on.

You only have to accept the TcpClient once, after that, you're free to send to and receive from it as you please. In most server apps that I've created, I keep a collection of all of the TcpClients, and add them to it as they come. (when you accept) Then, I usually assign a thread to each of them to listen for the actual data coming from them, receive it, and finally handle it.

To show the similarities between server and client, when the client does Connect(), the server's Pending() changes to true, and the server can then accept the connection in the form on a TcpClient or Socket. After that, any data sent from that client to the server will end up at it's network stream, and can be accessed from client.GetStream().Read().

Also, I rarely use timers in .Net. Threads ftw.

Hero

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • View Profile
    • http://rev7.net
Packets
« Reply #10 on: November 22, 2008, 08:36:42 PM »
How would I create a secure connection between two computers?
Hero
AKA: HeroAssasin and Mike
- - - - -  - - -
Visit Clan R77
- - - - -  - - -
Please do not PM me with random questions. That is what I made these forums for.

Noob ~Vector