Table of Contents
- Preface
- Serverside Code
- Clientside Code
- Local Computer Example (Successfull)
- İnternet Network Example (Unsuccessfull)
- My Possibility considerations
Preface
I take these codes from MSDN’s class library examples. So it must work correctly. Yes, I’m partially right. When running on my computer it succeed. But over internet network, it failes. I’m taking ConnectionRefused SocketErrorCode each time.
I’ll give source code below, initially, you must know my modified tests.
For Server Code:
- I used IPAddress.IPv6Any instead of IPAddress.Any.
- Also I tried without server.AllowNatTraversal(true)
But it failed
Serverside Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace ServerTest
{
class Program
{
static void Main(string[] args)
{
TcpListener server = null;
try
{
int port = 13000;
server = new TcpListener(IPAddress.Any, port);
server.AllowNatTraversal(true);
server.Start();
byte[] bytes = new byte[256];
string data = null;
while (true)
{
Console.WriteLine("Waiting for a connection...");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Connected: {0}", client.Client.LocalEndPoint.ToString());
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
data = data.ToUpper();
byte[] msg = Encoding.ASCII.GetBytes(data);
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
client.Close();
}
}
catch (SocketException e)
{
Console.WriteLine("Socket exception message: {0}", e.Message);
Console.WriteLine("Socket Error Code: {0}", e.SocketErrorCode.ToString());
}
finally
{
server.Stop();
}
Console.WriteLine("\nHit enter to continue...");
Console.ReadKey();
}
}
}
Clientside Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace ClientTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Started...");
Console.WriteLine("İP Address:");
string hostName = Console.ReadLine();
if(hostName.Length == 0)
{
hostName = Dns.GetHostName();
}
Connect(hostName, "hello");
}
static void Connect(string server, string message)
{
try
{
int port = 13000;
TcpClient client = new TcpClient(server, port);
byte[] data = Encoding.ASCII.GetBytes(message);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", message);
data = new byte[256];
string responseData = string.Empty;
int bytes = stream.Read(data, 0, data.Length);
responseData = Encoding.ASCII.GetString(data, 0, bytes);
Console.WriteLine("Received: {0}", responseData);
stream.Close();
client.Close();
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e.Message);
}
catch (SocketException e)
{
Console.WriteLine("Socket Error Code: {0}", e.SocketErrorCode.ToString());
}
Console.WriteLine("Hit enter to continue...");
Console.ReadKey();
}
}
}
Local Computer Example (Successfull)
I tried it only on my computer. First I opened ServerTest program then I ran ClientText program.
Clientside Output
Started…
İP Address:Sent hello
Received HELLO
Hit enter to continue
Serverside Output
Waiting for a connection…
Connected: 192.168.1.3:13000
Received: hello
Sent: HELLO
Waiting for a connection
İnternet Network Example (Unsuccessfull)
I tried with my friend whose ip address is “88.226.0.218”. He learnt his ip address from whatismyipaddress.com I ran ClientTest program and he ran ServerTest program. Outputs are below:
Clientside Output
Started…
İP Address:
88.226.0.218
SocketErrorCode: ConnectionRefused
Hit enter to continue
Serverside Output
Waiting for a connection…
And nothing. ServerTest program only waits.
My Possibility Considerations
- It can be using only İPv6 when I used İPAddress.İPv6Any
- My modem firewall can be preventing NatTraversal. If so, how can I enable modem’s NatTraversal programatically?
- AllowNatTraversal method of TcpListener class, have come with .Net4, doesn’t work properly. (I don’t believe, but for the sake of possibility)
Any help would be appreciated. This is very much important for me. I have been delaying my project almost for a month due to not succeeding this failure.
One thing to try is a test between two computers on the same network. If that works, you can turn your attention away from your computer being the problem.
In any case, the culprit is very likely firewalls, as Christopher said. I would actually check your friend’s firewall before your own, as some (most?) firewalls don’t hinder outgoing connections.
In addition, if your friend is behind a router, you will need to have him forward port 13000 to his computer’s local address (most likely 192.168.1.x) on port 13000.
More information (and guides!) on firewalls and port forwarding can be found here: http://portforward.com/help/start_here.htm