Click here to Skip to main content
15,884,859 members
Please Sign up or sign in to vote.
5.00/5 (1 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.
C#
//
/*   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
Updated 22-Sep-12 3:01am
v3
Comments
[no name] 22-Sep-12 7:37am    
You mean you want help to fix your code that none of the rest of us can see?
Abdollah Mohammady 11-Aug-19 3:04am    
Hello Sm.Abdullah and others

(apologize for weak english)

I have a problem like your question.
differences is:
1- my sumsong mobile as modem
2- my client app is an android application
all of other parts is like you.

Project description:
I created c# wpf project for server side and android studio app for client side. in wpf I coded very like you also in android studio. main point is I connected my phone as modem to connect the internet. also use the phone for debugging android studio app. when I test the communication between my pc and phone established successfully . it connects very good on android virtual device manager too . but when my friend install client app on his phone , connection is not established and my ip (192.168.xxx.xxx) is not accessible for client. I tried by other public ip founded from whatismyip.com but I don't socceeded

If you solve your problem please help me I do it

special thanks for you and other friends

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.
 
Share this answer
 
Comments
Sm.Abdullah 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 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
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
 
Share this answer
 

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