Jump to content

  • Log In with Google      Sign In   
  • Create Account


[C#] Invisible TCP packets?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 30 June 2012 - 06:47 AM

Wow, it's great to be back... last time I posted here was about 9 months ago I believe... Posted Image
It looks like I need the help of the GDNet community once more. Posted Image

So recently I started learning some basic network programming in games. I am currently creating a game where the same program can act as both a client and host a multiplayer session. I am only doing LAN communication atm using direct IP address input (the user has to enter the IP address of the host of the game in order to join).
I implemented basic TCP connection in .Net using the System.Net.Sockets.TcpListener/TcpClient classes, and managed to establish a connection between 2 computers in my house. However, when I try to write data to the NetworkStream returned by TcpClient.GetStream() method (specifically 44 bytes, if it makes a difference) I do not receive input on my other end.
I tried using the same computer as both the Host and the Client by launching two instances of my program, and using 3 different packet analyzers to try to track the packets, however I find that nothing shows, not even the handshake establishment... (is this because of loopback, perhaps?) I did not try to track packets between two different PCs.

Questions:
  • Can I track packets on a loopback address? If yes, what program does it?
  • Perhaps the amount of data I'm writiing is not large enough to send the packet? Can I overcome this and send exactly 44 bytes anyway?
  • From the code below, am I even doing it right?
Object declarations:
[source lang="csharp"]me = new IPEndPoint(MyIpAddress, port);tcpl = new TcpListener(me);tcpl.ExclusiveAddressUse = false;tcpc = new TcpClient();tcpc.ExclusiveAddressUse = false;players = new List<TcpClient>();[/source]
Connection Establishment:
[source lang="csharp"]// hosting is True if the user choses to host a gameif (hosting){ tcpl.Start(); // start listening on the port specified while (!tcpl.Pending()) System.Threading.Thread.Sleep(2); // just waiting, will be replaced later with something better while (tcpl.Pending()) players.Add(tcpl.AcceptTcpClient()); // accept a tcp connection request tcpl.Stop(); // stop listening}else // the user wants to join a hosted game{ tcpc.Connect(HostIpAddress, port); // ask for connection from host}[/source]
Later, data send & receive:
[source lang="csharp"]if (hosting){ byte[] data = new byte[44]; // here data gets initialized... foreach (TcpClient player in players) player.GetStream().Write(data, 0, data.Length);}else // read data{ NetworkStream stream = tcpc.GetStream(); byte[] data = new byte[44]; stream.Read(data, 0, 44); // working with data here...}[/source]

Sponsor:

#2 Xanather   Members   -  Reputation: 703

Like
1Likes
Like

Posted 30 June 2012 - 08:59 PM

Wow, it's great to be back... last time I posted here was about 9 months ago I believe... Posted Image
It looks like I need the help of the GDNet community once more. Posted Image

So recently I started learning some basic network programming in games. I am currently creating a game where the same program can act as both a client and host a multiplayer session. I am only doing LAN communication atm using direct IP address input (the user has to enter the IP address of the host of the game in order to join).
I implemented basic TCP connection in .Net using the System.Net.Sockets.TcpListener/TcpClient classes, and managed to establish a connection between 2 computers in my house. However, when I try to write data to the NetworkStream returned by TcpClient.GetStream() method (specifically 44 bytes, if it makes a difference) I do not receive input on my other end.
I tried using the same computer as both the Host and the Client by launching two instances of my program, and using 3 different packet analyzers to try to track the packets, however I find that nothing shows, not even the handshake establishment... (is this because of loopback, perhaps?) I did not try to track packets between two different PCs.

Questions:

  • Can I track packets on a loopback address? If yes, what program does it?
  • Perhaps the amount of data I'm writiing is not large enough to send the packet? Can I overcome this and send exactly 44 bytes anyway?
  • From the code below, am I even doing it right?
Object declarations:
[source lang="csharp"]me = new IPEndPoint(MyIpAddress, port);tcpl = new TcpListener(me);tcpl.ExclusiveAddressUse = false;tcpc = new TcpClient();tcpc.ExclusiveAddressUse = false;players = new List<TcpClient>();[/source]
Connection Establishment:
[source lang="csharp"]// hosting is True if the user choses to host a gameif (hosting){ tcpl.Start(); // start listening on the port specified while (!tcpl.Pending()) System.Threading.Thread.Sleep(2); // just waiting, will be replaced later with something better while (tcpl.Pending()) players.Add(tcpl.AcceptTcpClient()); // accept a tcp connection request tcpl.Stop(); // stop listening}else // the user wants to join a hosted game{ tcpc.Connect(HostIpAddress, port); // ask for connection from host}[/source]
Later, data send & receive:
[source lang="csharp"]if (hosting){ byte[] data = new byte[44]; // here data gets initialized... foreach (TcpClient player in players) player.GetStream().Write(data, 0, data.Length);}else // read data{ NetworkStream stream = tcpc.GetStream(); byte[] data = new byte[44]; stream.Read(data, 0, 44); // working with data here...}[/source]

For the answer to 3. Packets should indeed send automatically after some time, although to get it sending just about instantaneously change the TcpClient.NoDelay boolean to true. Tcp packets have large headers on them though so doing this can increase bandwidth consumption. When TcpClient.NoDelay is set to false (which is the default) it waits some time collect more data and send possibly several messages in one packet.

Edited by Xanather, 30 June 2012 - 08:59 PM.


#3 Xanather   Members   -  Reputation: 703

Like
1Likes
Like

Posted 30 June 2012 - 09:02 PM

Also instead of using .GetStream for every player just create a object of NetworkStream and use that (i.e. create a list of a class that has both TcpClient and NetworkStream members in it).

#4 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 01 July 2012 - 03:36 AM

change the TcpClient.NoDelay boolean to true

I will try that now, thanks for replying.

#5 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 01 July 2012 - 04:09 AM

Never mind the OP, I found my problem. I was accidentally ignoring the 4th byte of my data, causing everything to shift by one - causing my program to freeze. Stupid me Posted Image

Edited by wiz3kid, 01 July 2012 - 04:10 AM.


#6 ApochPiQ   Moderators   -  Reputation: 15210

Like
0Likes
Like

Posted 01 July 2012 - 02:40 PM

Just for the record, it isn't always easy to capture loopback traffic, especially on Windows, because of how it works. I believe there are some specialty drivers which make this available, though. A little bit of time on Google should turn up some tricks for capturing loopback traffic :-)

#7 hplus0603   Moderators   -  Reputation: 5187

Like
0Likes
Like

Posted 02 July 2012 - 10:42 AM

Yes, Windows loopback is very difficult to capture. Linux loopback is much easier -- "tcpdump -i any" does the trick.
On Windows, you can try:
- Try using the IP of the machine (192.168.1.2 or whatever ipconfig says) instead of localhost/127.0.0.1
- Try setting up port forwarding on your router, and connect to that (if the router supports hairpin NAT)
- Build a simple TCP tunneling/proxying program, and use that to capture data

enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS