Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
i was creating a desktop application (chat) and i want the client to connect without specifying the ipaddress
 
 private void btnListen_Click(object sender, EventArgs e)
        {
            IPAddress ipAddr = IPAddress.Parse(txtIp.Text);
            ChatServer mainServer = new ChatServer(ipAddr);
            ChatServer.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
            mainServer.StartListening();
            btnListen.Enabled = false;
            txtIp.Enabled = false;
            Name1.Start();
          
        }
 
ChatServer is a class
class ChatServer
{
public void StartListening()
        {
            IPAddress ipaLocal = ipAddress;
            tlsClient = new TcpListener(1986);
            tlsClient.Start();
 ServRunning = true;
            thrListener = new Thread(KeepListening);
            thrListener.Start();
        }
 private void KeepListening()
        {
            while (ServRunning == true)
            {
                tcpClient = tlsClient.AcceptTcpClient();
                Connection newConnection = new Connection(tcpClient);
            }
        }
 
 

in client program
i have to specify the ip address in order to connect with the server.
 

IS there a way to connect the client with server without specifying the ipddress
Posted 3-Dec-11 22:10pm
Comments
DaveAuld at 4-Dec-11 3:57am
   
What makes you think this can be done? have you seen something else that makes you think this is possible, just trying to understand your thinking that is all. If you have seen something else, let us know, you are maybe missing what is going on in the background of the other app.
mohanrajkiller at 4-Dec-11 5:55am
   
i think this may be possible by obtaining all lan network ip address
Richard MacCutchan at 4-Dec-11 7:46am
   
Consider how you would call someone on the telephone without specifying their phone number!
mohanrajkiller at 4-Dec-11 12:32pm
   
dial all the numbers and check :P

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Or you should use the IP, or you should use the DNS name of the server.
 
But if what you want is to connect to a game server in the local network, you may want to use IP broadcasting. I am not sure if TCP/IP allows this, but UDP allows it.
 
You may then want to use both, something like this:
Server starts to listen to broadcast messages.
As soon as it receives a client message (a broadcast message that says the client is there), it gets the IP of that client and sends another UPD message (so the client can check if it is the server message).
The client then connects to another port of such server. Note that the server message may include such port.
 
See the UdpClient.EnableBroadcast at http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.enablebroadcast.aspx[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 4 Dec 2011
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