Skip to content
Advertisement

TCP echo server doesn’t accept client

I’m trying to make an echo client server in C. The problem is, when I start the server and run the client code, in client shell it says “Connected” but in server shell it still says “Waiting for connection”. (Server doesn’t say “accept failed”. It says “Waiting for connection” forever). I think server is not accepting client and therefore if I write anything from client shell it doesn’t come back.

I would appreciate if I can get any help.

Here is server code:

JavaScript

Here is the client code:

JavaScript

Advertisement

Answer

You accept new clients in a infinite loop. So even if a client is detected the code will wait for an other client to connect.

If you want to make a single client server you can just remove the loop around the accept(). Example here

If you want to make a multiple client server you can thread your accept and store all your opened file descriptor. Example here

Advertisement