I seem to be able to get a connection between the server and client but when the server tries to print out the buffer it prints and empty buffer just saying " the client says " and no string following that
I must be doing something wrong with the strings perhaps
also I now decided to send my message which is just hello in a loop so it will constantly print hello on the server, but on the server sometimes hello wil be incomplete, is there anyway to only print hello when the whole sring has been transmitted?
The Client
The server is expecting a single message. So the client shouldn't have a while loop. It should:
connect
send
close
Don't send 2000 bytes. Send strlen(buffer), which is the 5 bytes of "hello". There's no need to send the trailing null as recv will return the number of bytes received.
The Server
The code is mostly ok. But you're not checking return codes.
listen() should have a buffer size, on a Windows sample, 5 is a reasonable value.
accept() returns a socket connection to the client. So call recv(), then close() on it.
There's more stuff I can say, but the code ought to work with the changes. If it doesn't, the return codes will tell you what's failed, where and why.
also I now decided to send my message which is just hello in a loop so it will constantly print hello on the server, but on the server sometimes hello wil be incomplete, is there anyway to only print hello when the whole sring has been transmitted?
Both send() and recv() can be partially successful.
You can try to send "hello", but send() might return say 2, indicating that only "he" was sent, and that you need to try again to send "llo".
Similarly, at the receiver, you might only end up with recv() returning say 3, where you have "hel" in the buffer, and you need to call recv() again to fetch "lo".
Your only guarantee with a stream connection is that the bytes you send will arrive in the order you send them. There is NO additional synchronisation between the two ends, like for example assuming a 1:1 correspondence between send and recv calls, that buffers always match, or that buffers have specific terminators.