Click here to Skip to main content
11,927,711 members (34,616 online)
Rate this:
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);
           btnListen.Enabled = false;
           txtIp.Enabled = false;

ChatServer is a class
class ChatServer
public void StartListening()
            IPAddress ipaLocal = ipAddress;
            tlsClient = new TcpListener(1986);
 ServRunning = true;
            thrListener = new Thread(KeepListening);
 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
DaveAuld 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 4-Dec-11 5:55am
i think this may be possible by obtaining all lan network ip address
Richard MacCutchan 4-Dec-11 7:46am
Consider how you would call someone on the telephone without specifying their phone number!
mohanrajkiller 4-Dec-11 12:32pm
dial all the numbers and check :P

1 solution

Rate this: bad
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[^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 4 Dec 2011
Copyright © CodeProject, 1999-2015
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