Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# socket Network Client
Hey,
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())
            {
                try
                {
                    c.Connect(ip);
                    NetworkStream clientStream = c.GetStream();
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    byte[] buffer = encoder.GetBytes(data);
 
                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                    c.Close();
                }
                catch
                {
                    c.Close();
                    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,
eLe
Posted 2-Jun-13 11:09am
Comments
austinbox at 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: http://www.codeproject.com/Articles/13285/Using-UPnP-for-Programmatic-Port-Forwardings-and-N
iliya Mir Alavy at 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 at 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.
—SA
iliya Mir Alavy at 3-Jun-13 3:06am
   
Do you know any guide which can help me to do that?
Sergey Alexandrovich Kryukov at 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.
—SA
iliya Mir Alavy at 3-Jun-13 5:05am
   
Thanks bro :)

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 3-Jun-13 9:30am
   
Exactly, a 5.
—SA

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

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2014
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