Click here to Skip to main content
15,888,098 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm writing TCP client server software using C#. I want client be connected with server automatically as soon as server started. To do this task, client may need to know whether the server is started or not. But how, is it possible?

My situation: There is one client and one server on the same LAN network. Each one knows IP address of other one. Here is code section to make connection between them:

Server side:

C#
// Starting the Server ...
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();

...

// Listening for client's connection in a thread ...
while (true)
{
    clientSocket = serverSocket.AcceptTcpClient();
    msg(" The client connected");
}

...


Client side:

C#
// Here, client makes connection to the server when user clicks a button
clientSocket.Connect("192.168.1.1", "8888");

...


Regards, Orgil.D
Posted
Updated 18-Mar-15 18:15pm
v4
Comments
PIEBALDconsult 18-Mar-15 12:02pm    
That's backward. Clients connect to servers.

No, this is not how it's done.

Think about it. How is the server going to know that there are clients to connect to? How is the server going to find them??

It's not.
 
Share this answer
 
The client has to setup the connection because most clients are behind a router (NAT.) It prevents incoming connections to the client from anyone outside of the NAT. Meaning servers are reachable over the internet and clients are (mostly) not. But polling every x minutes should't be that bad.

Good luck!
 
Share this answer
 
I don't think this makes a lot of sense. However, if you really want to do that, you can just keep the client trying to connect in a loop.
 
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