Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
hi,

I am working on simple Client Server Application, it is just like a chat Messenger.
I am using Client Server approach. My application works fine on LAN Local Area Network.but when it try to communicate to sever out side the LAN. Then there is no response to Client. while i know the server IP Adress (through an external means ) which uses a broad band connection and resides on a LAN.
i think i am unable to resolve the IP address, or proxy like problem occurs.
Can anybody Help me out ?
Regards !
Sm.Abdullah
 

 
my Code looks like this.
//
/*   Server Program    */
                 
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
 
public class serv {
    public static void Main() {
    try {
        IPAddress ipAd = IPAddress.Parse("172.21.5.99");
         // use local m/c IP address, and 
         // use the same in the client

/* Initializes the Listener */
        TcpListener myList=new TcpListener(ipAd,8001);
 
/* Start Listeneting at the specified port */        
        myList.Start();
        
        Console.WriteLine("The server is running at port 8001...");    
        Console.WriteLine("The local End point is  :" + 
                          myList.LocalEndpoint );
        Console.WriteLine("Waiting for a connection.....");
        
        Socket s=myList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
        
        byte[] b=new byte[100];
        int k=s.Receive(b);
        Console.WriteLine("Recieved...");
        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(b[i]));
 
        ASCIIEncoding asen=new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("\nSent Acknowledgement");
/* clean up */            
        s.Close();
        myList.Stop();
            
    }
    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }    
    }
    
}
 
---------------------------------------------------------------------------
 
/*       Client Program      */
 
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Sockets;
 

public class clnt {
 
    public static void Main() {
        
        try {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting.....");
            // here is local ip.. 
            // if i replace it with WAN IP it does not communicate.
            tcpclnt.Connect("172.21.5.99",8001);
            // use the ipaddress as in the server program
            
            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted : ");
            
            String str=Console.ReadLine();
            Stream stm = tcpclnt.GetStream();
                        
            ASCIIEncoding asen= new ASCIIEncoding();
            byte[] ba=asen.GetBytes(str);
            Console.WriteLine("Transmitting.....");
            
            stm.Write(ba,0,ba.Length);
            
            byte[] bb=new byte[100];
            int k=stm.Read(bb,0,100);
            
            for (int i=0;i<k;i++)
                Console.Write(Convert.ToChar(bb[i]));
            
            tcpclnt.Close();
        }
        
        catch (Exception e) {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
 
}
Posted 22-Sep-12 1:29am
Edited 22-Sep-12 3:01am
DaveAuld158.2K
v3
Comments
Wes Aday at 22-Sep-12 7:37am
   
You mean you want help to fix your code that none of the rest of us can see?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If i am not wrong you want to make a chat messenger type application that can work over internet just like gtalk, yahoo, or other IM messengers do ?
 
There are some points to remeber for that. Lets Define some term here so it would be better to understant what i actually want to say
 
A server: The one who serves the request. [Servers are not allows to initialize a communications they only accepts connections and give services to requests]
 
A Client: The one who connects to server to gain some service and initialize connection with server
 
when you do socket programming on LAN as you are doing here it is pretty fine , any server can be hosted on any LAN computer and any client can initialize connection and can start communications, because they are in same home (for ease to say) They all know each other very well, but if you want to communicate some other computer which is outside the network then there are some issues,
 
Lets name [Computer A] who is in a LAN, and [Computer B] in same LAN.
[Computer C] is on a public IP address,
[Computer D] is also a computer in a LAN and that LAN is accessing Internet via router who uses a public IP Adress.
 
so if the Computer A is a Server and Computer B is a client then there is no issue they can communicate
 
if Computer D is a Client and "Computer C" is a Server then there is also not an issue as Client initiates a connection and spouses to be trusted and router know where to who initiates connections so where to send responses.
 
if Computer D is a Server then there is a issue, because Computer D has the Local Ip Address assigned by the router like 192.168...... and this ip is not presented to outside the word so no client can find the server so there cannot establish a connection, and that's what happening in your case
 
The only solution to accomplish this task is only one that (@Christiaan Rokowaski) mentioned that you should go to your router setting where the server resides and forward the packets to the port you are connect with on the local Ip where server resides like forward traffic to port [Local_IP:PORT_Number]
 
but what if you don't have a physical access to router , mean you are developing a software to spread other non technical persons over the glob
 
so coming to that solution, well! sory to say but there is not a quite straight solution here, you have to search some terms and try out them, i have not tried any of them by myself so unfortunately i cannot refer some cod
there are some tricks that can be applied to achieved the solution
You can try ARP resolving, {not much sure about that)
AS your server resides behind the NAT you can study about it and one technical that is applied to do this is called hole punching
which is i think not easy to implement the only successful implementation of this is Skype
 
One easiest trick is (the one that most of messengers do) resides a server on a global IP and let all the clients connect to that server and do a mid man peer to peer communication
 
Quote:
some router allow to define dynamic port forwarding through UPNP, but it has to be enabled. That would be my recommended way if both sides are behind a NAT.

there are some useful links abut UPNP
Dot net port mapper
managed UpnP
 

finally this link is best explaining
 
Most Chat messengers use a server with a well known public IP to help clients communicate with each other. Each client communicates with the server and the server sends every message to the appropriate client. This is perhaps the only way to do it, because otherwise:
 
Every client should have a public IP in order for other clients to reach him. Each client should also act both as client and a server.
It will be very difficult to discover clients as you would need to know their IPs

 

a question posted similar to your problem
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Looking at your code I see nothing wrong, so it must be networking issue.
 
If this is all correct, make sure you can ping the server before connecting.
 
If you can ping it, then the server is probably behind a NAT or firewall. If so you will need to make the port (8001) accept connections, and connect it to the server application.
 
If you can't ping it, double check the ip address. If you have a home connection most of the time the ISP is free to change that ip address whenever they please, however that depends on ISP and you subscription, and that might be related to the problem.
 
Hope this helped, if not, please post more details on the setup.
  Permalink  
Comments
Sm.Abdullah at 23-Sep-12 8:25am
   
HI , actually my Server Resides on a LAN , Mean to say a lot of computer connected through a single modem to internet so it means all of the computer having same IP address on the internet (WAN) but they have unique local IP address like 192.168.... on LAN , when i connect through client i use MODEM IP Adress through which server is connected. in my situation a lot of computers on the LAN. i think that why connection is failed to establish. am i right ? can you tell what should i do in this scenario. ..
Christiaan Rakowski at 23-Sep-12 8:33am
   
In this case you need to log into your router and set it to forward traffic on port 8001 to the local server. It depends on the specific router brand what the option is called, I have seen the menus being called: Port Forwarding, (Special) Applications, DMZ, (Advanced) Firewall. Check here for your specific router: http://portforward.com/english/routers/port_forwarding/routerindex.htm

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 400
1 Shai Vashdi 334
2 Sergey Alexandrovich Kryukov 305
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 8,979
1 OriginalGriff 5,280
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 24 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid