Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

malpass

Simple Socket Question

This topic is 5425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, im trying my first network app, its a simple chat one, now I have a home network going through a hub (the 192.168.0.x IP) and I try my app on there and it works fine, and if i try my other IP, the actual internet, not HUB, it works on my home network, but then if i try to use it over the internet with someone else it doesnt connect. Why is this? I connect like this:
// Client

// -------

sWorker = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sWorker.Connect(new IPEndPoint(ipAddress,iPort));

// ... And Server 

private bool ConnectServer()
{
	try
	{
		sListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
				
		sListener.Bind(new IPEndPoint(ipAddress,iPort));
		sListener.Listen(4);

		sListener.BeginAccept(cbOnClientConnect,null);

		aBuffer = new byte[100];
		return true;
	}
	catch (SocketException se){MessageBox.Show(se.Message);return false;}
}

private void OnClientConnect(IAsyncResult ay)
{
	try
	{
		// Recieve Connected Socket From Client

		sWorker = sListener.EndAccept(ay);

		// And Keep Listening For More Clients

		sListener.BeginAccept(cbOnClientConnect,null);
	}
	catch (ObjectDisposedException o){MessageBox.Show(o.ToString());}
	catch (SocketException o){MessageBox.Show(o.Message);}
}

Then once thats done the rest works cuz when I try it on my network there fine, its just the connection thats the prob. The other thing is, if I be the client and let the other person be the server I can connect to them ??? I get problems like this with MSN when trying to send files, but not when simply trying to talk. Is this a common problem and how can I fix it? [edited by - malpass on August 16, 2003 1:13:18 PM]

Share this post


Link to post
Share on other sites
Advertisement
Change the firewall setting on your router (hub). It''s blocking incoming connections on whatever port you''re using; you have to open it up to allow incoming connections. How you do this depends on the type of router you have.

You can connect to someone else because the firewall is stateful - it allows outgoing connections to open sockets, but not incoming (this can be both good and bad). Your local network doesn''t go through the firewall, so it works fine.

Also, MSN Messenger does not play nice with NAT firewalls. It pretty much will not send files, or allow voice chat. I''m not sure if Trillian can, I tried it and didn''t like it, and GAIM doesn''t support file transfer either AFAIK.



"The sun is the same in a relative way,
but you''re older"
--Pink Floyd

Share this post


Link to post
Share on other sites
So I cant even talk if I have a firewall on?!?!

And also, If I am the server and I disconnect...:


if (sWorker!=null)
{
if (sWorker.Connected){sWorker.Shutdown(SocketShutdown.Both);sWorker.Close();}
sWorker = null;
}
if (sListener!=null)
{
sListener.Close();
sListener = null;
}


and then reconnect, every so often it will tell me it cannot access a disposed Socket object, linking back to EndAccept of my code:


private bool ConnectServer()
{
try
{
sListener = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

sListener.Bind(new IPEndPoint(ipAddress,iPort));
sListener.Listen(4);

if (cbOnClientConnect==null)
cbOnClientConnect = new AsyncCallback(OnClientConnect);

sListener.BeginAccept(cbOnClientConnect,null);

aBuffer = new byte[100];

return true;
}
catch (SocketException se){MessageBox.Show(se.Message);return false;}
}


private void OnClientConnect(IAsyncResult ay)
{
try
{
// Recieve Connected Socket From Client

sWorker = sListener.EndAccept(ay);

// Wait For Data From That Socket

WaitForData(sWorker);
}
catch (ObjectDisposedException o){MessageBox.Show(o.Message);}
catch (SocketException o){MessageBox.Show(o.Message);}
}


anyone know why this is?

And finally, if I disconnect either side using the first snippet of code the other side constantly and rapidly recieves an infinite list of blank messages, anyone know how to fix this?

Share this post


Link to post
Share on other sites
quote:
Original post by malpass
So I cant even talk if I have a firewall on?!?!




That is the purpose of a firewall. More specifically, cable modem/dsl "routers" are NATs, network-address-transation servers. Running servers behind a NAT requires you to tell the NAT which machine the service is running on, and to forward request to it. A good NAT will support this, but not all of them do.

From the outside world, you connect to the cable modem/dsl router IP address (then it forwards to the internal server). If you embedded IP addresses in your data and use them, your application most likely will not work across NATs (NATs have special programming in them to handle FTP correctly due to this).

Share this post


Link to post
Share on other sites
So anyone know how to get past the firewall? I mean I can talk on MSN, just not send files, is that because all ppl connect to a MS server, and the server does the work, but for files one on the 2 ppl needs to be a server or it would have to up it to the MS server then dl it to the client, IE 2x the dl.

Also anyone know how to close the connection correctly because if I do it like I showed above the other side gets a constant blank message.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You should whange the firewall rules to allow the specific kind of traffic you want to do that it won''t allow you to do otherwise. How this is done depends on what kind of firewall it is. If all else fails, you can get past your firewall by removing it from the network.

If it isn''t actually your firewall, try talking to the firewall admin. If that won''t work, you''re out of luck. You can''t programmatically pass through a firewall.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!