English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

The task is to write a TCP client that establishes a connection with a TCP server (both written in C) which accepts the connection. The client is to send the contents of a file that it has opened for reading to the server. The server then reconstructs the file when it receives the client data. My program uses the socket bind listen accept functions to communicate with the client, but the problem is the reception of the data from the client. How can I make the server continuously receive and write to the file (can I use fork() here?) and the client continuously read from the file and send to the server? I tried using a while loop in the server but got the weirdest error, too big to post here. However, I think the while loop in the client that enables the coninous reading and sending works.

2007-11-12 13:31:13 · 4 answers · asked by discombobulated girl 4 in Computers & Internet Programming & Design

4 answers

client:
char buffer[1024];
f=fopen("file"...);

// while we haven't reached end of file:
while (!feof(f)) {
int r=fread(buffer, sizeof(buffer), 1, f);
if (r<0) error(); // handle error (see strerror(), errno)
if (r==0) break; // end of file reached
// now we've read a chunk from the input file, send it
// i assume sock is a socket connected to the server
w=send(sock, buffer, r, 0);
if (w!=r) error();
}
// close the connection
close(sock);


server: accept() returns a socket 'sock' from the connected client. while we can read data, write it to a file.
f=fopen("output", "wb");

while (1) {
int r=recv(sock, buffer, sizeof(buffer), 0);
if (r<0) error();
if (r==0) break; // connection was closed by the client
fwrite(buffer, r, 1, f); // append data to the file
}
// close the client socket and the file
close(sock);
fclose(f);

as you can see, it's not so tricky. you also don't need to fork, unless you have MANY incoming connections with huge files. in that case, a fork could be useful.

in this example, the client sends data and then closes the connection to signal that all data was sent. therefore you don't know in the server how much data you will receive. if you need that size (and maybe additional information) before actually receiving the file, you have to send that information before sending the file.

2007-11-12 19:49:28 · answer #1 · answered by cruppstahl 4 · 1 0

no longer anymore, at one time C++ replaced into only an extension of C. on condition that then, C has replaced, and so has C++. despite the fact that that being stated this is common for the same software to assemble the two C and C++. the tactic is the same, yet there are in elementary terms some adjustments int he libraries which you want the compiler to link to.

2016-10-16 07:46:34 · answer #2 · answered by bobbee 4 · 0 0

If you are restricted to a particular port you will have to optimize the traffic to those protocols If there is plenty of redundancy you could do a do while loop.

2007-11-12 21:29:02 · answer #3 · answered by iggytog 3 · 1 0

Gosh I'm sorry Ebony, I don't know the answer. If I can come up with someone who might, then I'll have them contact you.
Have a good day!

2007-11-12 14:41:19 · answer #4 · answered by mandbturner3699 5 · 1 1

fedest.com, questions and answers