|
why are you closing the socket in send_recv function? and why are you trying to listen and accept again?! Once you close the socket, you can no longer receive/send on it. you have to make a new connection to be able to send/receive anything, so keep your socket alive as long as you need it. On the same note, get rid of the connect code in the client send_recv function.
Some general things to follow for a server are:
- listen only once for each server you are providing. Dont close the socket only to reopen it again to listen.
- accept each connection and deal with it. keep the socket alive as long as you need it. Do not listen/accept on the returned socket from accept.
If you are having only one server that does a particular thing, you only need one listen call and one accept call in a loop.
Use of select makes sending and receiving easier by not requiring you to wait for the send/recv calls to return. read up on select function.
If you want to serve multiple clients using the same server simultaneously, you will have to use either threads or select function.
|