I have a server and a client app, my server listens on port 10015 and a client which sends commands to that port. Currently both are running on the same machine, but in the future the goal will be to have running on different machines. I have this part working right now.
What I need to do next is have my server send commands to my client as well. So I thought I could just re-use my code from my server in my client to listen on a port.
But I am not sure that is the best way to do it. Suggestions?
When I first tried this, I ran my client app first, and it sent the command to itself. My server app failed to bind to the port (I assume you can only have one process listening on a given port? )
Question 1)
How can the server send commands to the client? Do I have to create make the server–>client communication on port 10015 and client—>server on a different port like 10016?
Question 2)
When I send a command from client–>server with send(), what’s the best way to receive an ACK to that specific command? If I dont need to send any data back is there a way to just get an ACK automatically when the packet is received by the server?
I am currently doing this for every command I want to send :
create socket()
conenct() to socket
send() packet
then call recv() to receive any data
then shutdown() connection
and closesocket() at end
not sure if there’s a better way to do it? I am expecting to send anywhere between 1-10 commands per sec when my app is busy.
thanks, I am new to this networking applications, so any help is greatly appreciated.
Edit after reading some comments:
I am using TCP protocol. When I said ACK, I meant I just wanted some acknowledgement from the other app that the command had been received and processed without errors
Typically the way this is done is as follows:
On the server:
If you are using UDP, you can simply copy the sockaddr structure that was set during recvfrom() into the sockaddr structure that you’re passing to sendto().
If you are using TCP, the socket file descriptor returned by accept() can be used in send() to send response traffic to the client.
My favorite C Sockets reference is the free and available online Beej’s Guide to Network Programming.