Click here to Skip to main content
15,439,117 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to connect to a remote IP using Socket Programming.
The Programme successfully connects to a machine where there is a Real IP (e.g IP like ) from any IP.

However, when there are more than one computer(computer having LAN IP like, etc.) connected to a router (router having IP Like,
the socket can't connect the IP IP Under the router ) from the IP

Error come from ConnectionRemoteEndPoint() function:

Error: A request to send or receive data was disallowed because the socket is not connected and ( when sending on datagram socket using sendto call)and no address was supplied.
Client Code : (IP > )
private void ConnectRemoteEndPoint()
            TcpClient tcp_Server=new TcpClient();
            IPAddress ipAddr = IPAddress.Parse(
                tcpServer.Connect(ipAddr, 2000);
            catch (Exception ex)

Server Code :(IP >
private void AcceptRemoteEndPoint()
      TcpListener listener_tcp;
      TcpClient tcp_Remote;
      listener_tcp = new TcpListener(IPAddress.Parse(, 2000);
            while (true)
                tcp_Remote= listener_tcp.AcceptTcpClient();

I am new in network programming..:confused:
How to solve the problem???

Updated 1-Feb-11 2:11am
JF2015 1-Feb-11 8:11am    
Added pre tags. Please use proper code formatting when posting questions to improve readability.

You won't be able to connect to a 192.168.* address from the outside world. You'll actually need to enable port forwarding on the router and map the port to the desired IP, meaning also that it likely won't be dynamic (you won't be able to connect to any old 192.168 address without first updating the mapping in the router).

192.168 addresses are not routable outside of the private network. See this article[^].

Share this answer
Sergey Alexandrovich Kryukov 1-Feb-11 13:01pm     CRLF
I guess you reported just first problem you spotted (my 5 anyway, "you don't have to eat whole egg to tell it's bad"). I spotted some other (please see). --SA
Sergey Alexandrovich Kryukov 1-Feb-11 15:17pm     CRLF
Also, pay attention: duplicate question, see my updated answer. This is bad :-) --SA
Set aside you address, where is you TcpListener.AcceptSocket or TcpListener.AcceptTcpClient? This is now how TcpListener works :). Also, normally, you would need accepting sockets one thread and exchange with client(s) via reading/writing from/to network stream on the other thread.

By the way, when I look at what's supposed to be a client part:

TcpClient tcp_Server = new TcpClient();

in particular, at your naming (a client called tcp_Server(!)), something tells me that you're in deep confusion :).

Now please tell me why this Question is nearly identical to the other one:
Problem to connect remote computer using socket[^]?!
You should not do that.

Share this answer
Bobby-CA 2-Feb-11 1:52am     CRLF
This is a very difficult question, actually original poster was unable to describe it properly. I am also facing the same situation. Here I am describing the problem in simple terms. Imagine 2 users connected to Yahoo messenger or GTalk etc. Both the users are under the same LAN (i.e., same router), hence their Internet IP's are same but LAN IP's are different. The question of Router configuration etc. will not arise at all. Because the persons under the same LAN are communicating with each other, without even bothering what their IP is and they are also communicating with another person in Montreal (i.e., out of the LAN). The Socket Server is always running in the background, but the communication is entirely P2P. The Problem: If a message sent from A in Montreal to B in Japan (C is also in Japan and in the same LAN as B). Now, the message from A destined for B will also be broadcast to C. Because the sockets are binded to an IP and Port. Since B & C have the same Internet IP, the sender message cannot resolve to the actual end point (i.e., the actual recipient of the message). I hope this makes more sense. Also, the confusion mentioned about "TcpClient tcp_Server" should actually be "TcpClient tcp_Client". SO, whatever, the problem still remains the same.
Sergey Alexandrovich Kryukov 2-Feb-11 4:02am     CRLF
Well, thank you. It makes a lot more sense. OP's report is nothing close to that (how do you even know this is the same problem?). Let me think about it later -- really want to sleep. Did you post your own question, separately? Drop me a comment with the link if you do... --SA
Bobby-CA 2-Feb-11 7:06am    
Ok, i have created a new post for this and here is the link
Sergey Alexandrovich Kryukov 2-Feb-11 13:20pm     CRLF
Thank you, I already read it. The problem looks very difficult or even not solvable in the bounds of that design. Only a radical change in design could reach the practical requirements, don't you think so? Could you think about it and add a set of functional requirements instead? In that case, maybe a workaround will bring a solution? --SA

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