Click here to Skip to main content
15,946,074 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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,
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...
Sergey Alexandrovich Kryukov 2-Jun-13 21:13pm    
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?
Sergey Alexandrovich Kryukov 3-Jun-13 3:18am    
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.

1 solution

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.
Share this answer
Sergey Alexandrovich Kryukov 3-Jun-13 9:30am    
Exactly, a 5.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900