I tried to program a simple networking test with a WAN server/client, but it failed. I know the problem is not the ports or the buffer to be sent, because I tried it using the localhost(127.0.0.1) and the string "hihi" was shown correctly by the server, but when I tried with my public IPv4(the ip you can get on any website googling for 'my ip'), that just don't works, and the server stays on the Waiting for connection... forever, and the client launchs an exception(not timeout) which says that the server didn't response. It's not a port forwarding problem, because then my localhost would not been worked neither. The code:
Server:
static void Main(string[] args)
{
TcpListener s = null;
try
{
s = new TcpListener(IPAddress.Any, 8888);
s.Start(1);
byte[] dataBytes = new byte[4];
string data;
while(true)
{
Console.WriteLine("Waiting for a connection...");
TcpClient c = s.AcceptTcpClient();
Console.WriteLine("Connected!");
c.ReceiveTimeout = 999999999;
NetworkStream ns = c.GetStream();
int i;
while((i = ns.Read(dataBytes, 0, dataBytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(dataBytes, 0, i);
Console.WriteLine("Received: {0}", data);
}
c.Close();
}
}
catch(SocketException e)
{
Console.WriteLine("An exception ocurred!");
}
finally
{
s.Stop();
}
Console.ReadLine();
}
Client:
static void Main(string[] args)
{
string msg = "hihi";
bool localhost = false;
TcpClient c = null;
try
{
if(!localhost)
c = new TcpClient("83.xx.xx.xx", 8888);
else
c = new TcpClient("127.0.0.1", 8888);
c.SendTimeout = 999999999;
byte[] data = Encoding.ASCII.GetBytes(msg);
NetworkStream ns = c.GetStream();
ns.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", msg);
ns.Close();
}
catch(SocketException e)
{
Console.WriteLine("An exception ocurred: {0}", e.ToString());
}
finally
{
c.Close();
}
Console.ReadLine();
}