Click here to Skip to main content
13,147,591 members (31,806 online)
Rate this:
Please Sign up or sign in to vote.
I am trying to write a client-server program for the first time. It works fine on LAN networks but I can't get it to work over internet.

I can easily send data to the server program with the following code:
        public static bool sendTo(IPEndPoint ip, String data)
            using (TcpClient c = new TcpClient())
                    NetworkStream clientStream = c.GetStream();
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    byte[] buffer = encoder.GetBytes(data);
                    clientStream.Write(buffer, 0, buffer.Length);
                    return false;
            return true;

Then server has to respond to the client using the same code but it fails giving the following error:

No connection could be made because the target machine actively refused it *IP*:*PORT*

I know the problem can be fixed with port-forwarding and changing the settings of router. But I'll probably try to make a software out of this soon and users should be able to use the software with minimal configurations. So... I was wondering if anyone can help me here...

Thanks in advance,
Posted 2-Jun-13 10:09am
austinbox 2-Jun-13 16:48pm
Well, outside your network people can't connect to your server without a port forwarding, or unless you own a http server. So I would suggest if you can't get a web server, either experimenting with automatic port forward or either a really nice port forwarding guide. There are articles on Upnp and other port forwarding guides like this one:
iliya Mir Alavy 2-Jun-13 17:10pm
I own a virtual private server and have my server program running on it. I also don't have any problem in sending data to it. It easily listens to the specified port, parses the received data packets and chooses the right response. The problem is when server wants to send the response to the client. Here's when I get that error. I thought server's IP automatically gets saved in NAT table after sending data... But it just doesn't seem to work that way for me...
Why would you need you server side to connect a client side at all? Usually, this is not needed. Client connect to some service, and then, if the service accepts it, they start sending and receiving data in certain order.
iliya Mir Alavy 3-Jun-13 3:06am
Do you know any guide which can help me to do that?
Look, I did not read any guides, just took the API reference and implemented it in different situations, so you should be able to do it, too. It just need proper attention while reading it.
iliya Mir Alavy 3-Jun-13 5:05am
Thanks bro :)

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You'll need to re-design your architecture. For the server, use some kind of a Web Service, e.g. WCF. Your client will then send requests to that service and receive an answer as some kind of return value.
That way, there is no need for the server to connect to the client.
Exactly, a 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170915.1 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100