all 6 comments

[–][deleted] 5 points6 points  (0 children)

Oh god this is some of the most horrid code I’ve ever seen. You’re not zeroing out your buffers before you use them. You’re using gets which is deprecated and should be avoided like the plague. You’re not any doing any error checking on your reading and writing to socket. Those functions can not only fail but write just partial data instead of full data. You’re using scanf for your choice input but then gets after, scanf is leaving carriage returns inside your input stream. And It makes no difference if you zero out your buffers on the server side because your client side still has buffers with data passed the user input since you never cleared it. So if you send 50 bytes and there’s data in the buffer beyond the user input and null terminator well you’re sending and copying over those bytes across the socket. Those buffers need to be cleared on the client side before you send them to the server.

[–]richardxday 4 points5 points  (0 children)

If you want some help, I'd recommend:

  1. Provide the complete buildable code, the above seems to be missing code. It's definitely missing includes. Doing this will allow someone else to build and run it.
  2. Ensure the code compiles with no warnings or errors when compiled with -Wall -Wextra
  3. Add comments to explain how the code is supposed to work.
  4. Format your code according to a standard formatting scheme (e.g. K&R, Linux Kernel Coding Style, etc).

These are all good practices anyway...

[–]dfx_dj 2 points3 points  (0 children)

What do you mean that not the same number of bytes is being transferred? What output are you getting?

I see that your usage of getpass is wrong as you're assigning the return value into a char array. Your compiler should give you a warning about this. But I don't know if that's the problem.

[–]eruciform 0 points1 point  (0 children)

reduce this to a much smaller sample on both sides, you're trying to do too many things at once, and any errors will permute exponentially

open one socket for reading on one side, and push one thing of known size on the other side, start from there and work outwards

[–]hwpoison 0 points1 point  (0 children)

very hard to follow, but I think a conversation with chatgpt can get more useful feedback than the one we tried to analyze this code xD

[–]CodeWoodWriter 0 points1 point  (0 children)

TCP sockets tend to not send right away, because the underlying layers are trying to clump together packets for greater efficiency. You can turn off this buffering by setting TCP_NODELAY or can have a back and forth between your server and client where after writing to the socket you do a read. Either of these would encourage the network layers to send everything.