473,569 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C#: Client can't connect to external IP server

1 New Member
I've been slaving at this problem for over a week, and would appreciate some help from you kind folks.

Basic Problem: I have made a server program and a client program. When I try to use the client to connect to 127.0.0.1 or my LAN IP, the program connects and transfers information successfully. However, when I get somebody else to host the server, or when I try to connect to a server on my own machine using my WAN IP, I currently get a "Connection could not be made because the target machine actively refused it." exception. I'm not sure what I changed to get this exception I just mentioned instead, but I was previously getting a "Connection could not be made because the target machine did not respond." exception as well.

Here is the jist of the code causing the problem:

Information Initialization Code:

Expand|Select|Wrap|Line Numbers
  1.         internal Connection(int port)
  2.         {
  3.             DNSname = Dns.GetHostName();
  4.             IPAddress ip = Dns.GetHostEntry(DNSname).AddressList[0];
  5.             address = new IPEndPoint(ip, port);
  6.         }
  7.  
Server code:

Expand|Select|Wrap|Line Numbers
  1.             TcpListener Listener = new TcpListener(IPAddress.Any, address.Port);
  2.             Listener.ExclusiveAddressUse = false;
  3.             Listener.Start(10);
  4.             while (Active)
  5.             {
  6.                 if (Listener.Pending())
  7.                 {
  8.                     if (RemoteTcps.Count < CommonConstants.MAX_PLAYERS)
  9.                     {
  10.                         Thread ConnectThread = new Thread(delegate() { ConnectToClient(Listener); } );
  11.                         ConnectThread.Start();
  12.                     }
  13.                 }
  14.                 Thread.Sleep(100);
  15.             }
  16.             Listener.Stop();
  17.  
The listener never receives a request, so Listener.Pendin g() is never true (unless I specify my LAN/loopback address).

Client code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.         internal void StartAsClient(IPEndPoint serverIP)
  3.         {
  4.             Active = true;
  5.             Tcp = new TcpClient(this.address);
  6.             remoteaddress = serverIP;
  7.             if (ConnectToServer())
  8.             {
  9.                 Thread ClientThread = new Thread(delegate() { LiveClientConnection(); });
  10.                 ClientThread.Start();
  11.             }
  12.         }
  13.  
  14.         private bool ConnectToServer()
  15.         {
  16.             try
  17.             {
  18.                 Tcp.Connect(remoteaddress);
  19.                 return true;
  20.             }
  21.             catch (Exception exception)
  22.             {
  23.                 Debug.Error(2, "Connection " + address.Address.ToString() + ":" + address.Port + " to " + remoteaddress.Address + ":" + remoteaddress.Port + " failed.  " + exception.Message, this);
  24.                 return false;
  25.             }
  26.         }
  27.  
  28.  

The catch() above in the client code is what is telling me that Connect() failed because the server machine actively refused it.

I have made sure to turn off windows firewall completely for testing, and I have only been using forwarded ports on my router when I host. I have also tried putting the server on somebody else's computer and tried connecting to him through his WAN address, with no prevail, and he doesn't use a firewall, his router is forwarded, and he can successfully host other programs.

I used the MSDOS prompt to ping my WAN address and that was successful.

Someone with a similar problem in another forum was told to use TcpListener Listener = new TcpListener(Dns .GetHostEntry(D ns.GetHostName( )).AddressList[0], address.Port); instead, but that didn't work for me and I think IPAddress.Any is probably what should be used.

Thanks in advance for any help.
May 16 '07 #1
3 8198
Motoma
3,237 Recognized Expert Specialist
Can you connect to your server, from the client machine, using telnet or netcat?
May 17 '07 #2
radcaesar
759 Recognized Expert Contributor
Try after disable ur OS forewall.
May 17 '07 #3
Plater
7,872 Recognized Expert Expert
I currently get a "Connection could not be made because the target machine actively refused it." exception. I'm not sure what I changed to get this exception I just mentioned instead, but I was previously getting a "Connection could not be made because the target machine did not respond." exception as well.
'target machine actively refuses it' usually occurs when the request reaches the correct port on the machine and there is nothing listening on that port.
'target machine did not respond' is usually related to a firewall blocking the port and stoping a result message from making it back to you (stealth mode).

I would suggest checking up on yours (and others and network's) firewalls first.
Then try a tracert to your wan IP and LAN IP and the LocalHost(127.0 .0.1) IP. Then debug your server with breakpoints at the connection received sections and try to connect to it with like telnet or something.
May 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3832
by: Paul | last post by:
Hi, Is their a client side VBscript command that I can use to post the local IP address of the machine back to my web server. I want to obtain the client's side IP address of their machine. However because my IIS stands behind a firewall and uses reverse hosting techniques, the http headers contains the firewall's IP instead of the...
5
1516
by: John | last post by:
I have a simple script that runs a server where one client can connect. I would like to make it so that many clients can connect to one server on the same port. Where can I find how to do this? Thanks, --j
7
3352
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly communicate directly with the database. Do I need a separate db2 connect on each such machine. Please advice.
7
1537
by: rob | last post by:
I want to create an application that can be used both for a server-client model as well as a standalong application. The idea is to write a general server-client application. The standalong then just has both components on the same system. Now the question is if this is a good approach or if this is even doable. For instance do web services...
22
6243
by: Jordan S. | last post by:
SQL Server will be used as the back-end database to a non trivial client application. In question is the choice of client application: I need to be able to speak intelligently about when one client (MS Access vs ..NET Windows Forms) would be preferred over the other. While I have some good arguments on both sides, I would appreciate your...
6
1953
by: MaxPersson | last post by:
Hi guys! My first post here is ofcourse a little problem of mine! =) ive been looking pretty mutch everywhere, asking my proffesor and such, and we cant figure this out at all. I even analyzed the network trafic with Wireshark to see what was going on. No luck. So now im turning to you hardcore guys here! =) Well to the problem. Im doing a...
2
7269
by: goyalak | last post by:
Hi All, I am new to PHP and am facing some problems. I am trying to access the http://ops.espacenet.com/. Its a SOAP based service. I am trying to access it via PHP (PEAR) from Apache web server on my computer. I connect to the web via a proxy server. I have enclosed the code below and also the error given. Any help will be appreciated. ...
3
1594
by: Joh | last post by:
I'd like to test a server application and a client application from the same computer. Currently if I let the client try to connect to my IPv4 adress provided by my router in this case 192.168.1.2 or my "external" IP provided by my ISP the tcplistener in the Server class wont accept the connection. Can I do this in another way and make it...
0
1506
by: 85ssp | last post by:
I am creating a small server client program that is meant for up to 70 connections from 70 different computers on a network. Everything in the program functions correctly except when testing operations with a server crash, the clients go into a disconnected state and wait for the server to come back up. Once the server is up the clients...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.