connections over internet

i have been trying to implement a server and a client little program that will be connecting with each other over the internet.

i written the server application to open a port on my computer and listen for tcp connections with tcplistener.
the client i used tcp client and gave it to a friend over the net to try and connect to the server on my computer.
i gave him my IP address, he putted that in, and the port as well, and tried to connect, but it the tcpclient crashed.
it said something along the line the server host refused, something along that line. and that it is not finding the server.

im on a switch, and as so as my friend. is there a special way to do the connections over the internet? and will users on switch need something else as well?

thx in advanced.

[798 byte] By [egyptian_bean] at [2007-12-17]
# 1
Hi,

if your program works on a local network take care of these things if you will try it over the internet:

1) be sure your router firewall has the ports open you use.
2) you muster enter the pc's ip not the router's, some routers have port forwarding which means that if a user connects to a certain port the port will be redirected to a certain pc.
3) to not need manually refersh the ips of both pcs I would recommend you to use a service like dyndns.
4)Last but not least: Check if your local firewalls do block any trafic or ports from your appliction.

rgerbig at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
ok, i got the dynDNS thing to work, and i tried it again.
this time i got "An existing connection was forcibly closed by the remote host" as an exception message from the client.

here is my code:

SERVER:

TcpListener listener = new TcpListener(12121);

listener.Start();

statusBar1.Text = "Listening to port 12121";

listener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), listener);
my CLIENT code is:

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPAddress[] ip = Dns.GetHostAddresses("ngen.game-server.cc");

client.Connect(ip[0], 12121);

=============================================

now that exception i got from the "client.Connect" line.

why is that so? and where windows is the firewall settings?

egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3

does anybody happen to know what is the following exception caused by:
"An existing connection was forcibly closed by the remote host" ?

can it be from my router?

egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

What are doing after the BeginAcceptTcpClient?
Are you calling the EndAccept from a call back?

Can you use the Sync Version AcceptTcpClient?
And hold on to the TcpClient? What *COULD* be happening is that
the connection is established and immediately disposed since you are not holding on to the
connection.

The exception you mentioned means that
"A connection was established but was prematurely closed - with a TCP REset"

What you can do is to use a network monitor to capture the trace and see
what is really going on. ..

DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5
well after the "BeginAcceptTcpClient" i was doing nothing.
here is my server code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ChatServer
{
public partial class Form1 : Form
{
TcpListener listener;
AsyncCallback cb;
public Form1()
{
InitializeComponent();
listener = new TcpListener(IPAddress.Parse("10.1.1.4"), 12121);
cb = new AsyncCallback(AcceptClient);
}
private void OpenPortBTN_Click(object sender, EventArgs e)
{
listener.Start();
statusBar1.Text = "Listening to port 12121";
listener.BeginAcceptTcpClient(new AsyncCallback(AcceptClient), listener);
}
private void ClosePortBTN_Click(object sender, EventArgs e)
{
listener.Stop();
statusBar1.Text = "Closed port 12121";
}
private void AcceptClient(IAsyncResult ar)
{
TcpClient s = listener.EndAcceptTcpClient(ar);
}
}
}


here is my client code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace ChatClient
{
public partial class Form1 : Form
{
TcpClient client;
public Form1()
{
InitializeComponent();
}
private void ConnectBTN_Click(object sender, EventArgs e)
{
IPAddress[] ip = Dns.GetHostAddresses("ngen.game-server.cc");
client = new TcpClient(ip[0].ToString(), 12121);
}
}
}


now what am i missing in here? Tongue Tied thx buddy
egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 6

Exactly that is the issue

TcpClient s = listener.EndAcceptTcpClient(ar);

After the callback, the TCPClient goes out of scope and the connection is closed.
You should have a member variable that gets updated and then communicate with the client.

In your case the connection is established but immediately terminated since the variable is going out of scope and eventually the dispose is called.

DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 7
i removed the 'TcpClient s' declaration, and declared it as a member of a class above. and just wrote 's = listener.EndAcceptTcpClient(ar);'. isnt that what u meant? but it still crashed.
egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 8
your code should work.... i guess it is the dns resolution that is causing the problem.. Try giving explicit IP address instead of using Dns.GetHostAddresses.. Dns entry might be outdated and that might be the reason for the problem.

couple of steps to debug if the pbm is present still

Do a netstat -a on the server.. see if the port in which you are listening is listed..
ping the server machine from the client.. if that succeeds Try doing a telnet ip port from the client.. if this succeeds your socket code also should succeed..

MalarChinnusamy at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 9
ok thats sounds good, ill try those as soon as i get home, thx buddy. Big Smile
egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 10
ok, i netstat -a at my server, and the port is opened. and i pinged to the server and its working.

but i realized a weird thing. i did Telnet on that address, ngen.game-xxx and i got a login for my router. now why is that?

so that address goes directly to my router, should it go to my pc (where server i at)? i think thats why its refusing and giving me that weird exception. how do i direct it to my pc where the server is at?

btw, my router is d-link DSL-500G.

thx

egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 11
I don't know why the router is responding with a login, you might need to talk to a
network admin.

The fact is that you can ping the machine itself is a good sign.
Now what you could probably do is to capture a netmon trace.
This will tell you whether a connection is being opened or not.

DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 12

Could it be that your router is using NAT and dynamically giving your machine its IP Address? Did you check the IP address on your machine and compare it to the ipaddress that DNS is giving you?

From a command prompt, run "ipconfig /all" on the server to get your addresses.

JonCole at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 13
i think the router is using NAT, cause i didnt assign local ips on my LAN network, i letted it automaticaly assign.
the ip address from ipconfig /all and the dns ip is the same, for the local ips.
the ip that im using for the net is from the dynDNS one, and its the same ip that i get from any site that gives u the ip.
does that make anything clear? Tongue Tied
egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 14
ok i have another question, in order to get this part working, do i have to have a gateway on my LAN at home where my server is placed at?
wat i have now is, a switch and 2 computers connected, to it, and a router also connected to it, there is no gateway.
can that be causing anything?
egyptian_bean at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified