简体   繁体   中英

When using send() to send data from a text file from client to server through TCP stream, how do I send all the data but only 4 bytes at a time?

Below is an excerpt from my client.cpp file:

//Variables previously declared
char buffer[1024];
char sendbuffer[100];
int sockfd, b;

//Opens specified file
FILE *fp = fopen(argv[3], "rb"); 

while( (b = fread(sendbuffer, 1, sizeof(sendbuffer), fp)) > 0 ) 
{
    send(sockfd, sendbuffer, b, 0);
}


I am new to client-server programming, and I'm far from being extremely proficient in C++.

When I use the code above, it's successful sending the inf, but it obviously isn't going to send the data 4 bytes at a time.

If I modified the line containing send() as shown below without making other necessary changes, I'm certain that it would be incorrect.

send(sockfd, sendbuffer, 4, 0);

It's also a pain to debug because when I make a change to the code, I have to continuously simulate a client-server interaction, which takes time to set up.

What would be the most efficient way to send this text file data 4 bytes at a time?

Also, can anyone suggest a tool or method for quickly debugging client-server programs?

Let me know if more information is needed. Thanks

Well, you can try to send 4 bytes at a time and it will probably work but you have no control of how many bytes a stream socket will actually send. You have to check the return value. I do not think you need to debug the program at all. Logging is better because it does not introduce time delays like debugging does, and time is money in the networking world.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM