In the Internet File Server example (Figure 6-6), can the connect( ) system call on the client
Question:
Figure 6-6
Transcribed Image Text:
* This page contains a client program that can request a file from the server program * on the next page. The server responds by sending the whole file. */ #include
* This page contains a client program that can request a file from the server program * on the next page. The server responds by sending the whole file. */ #include #include #include #include #define SERVER_PORT 12345 #define BUF_SIZE 4096 /* arbitrary, but client & server must agree */ /* block transfer size */ int main(int argc, char **argv) { int c, s, bytes; char buf[BUF_SIZEJ; struct hostent *h; /* buffer for incoming file / * info about server */ struct sockaddr_in channel; /* holds IP address */ if (argc != 3) fatal("Usage: client server-name file-name"); h = gethostbyname(argv[1]); if (!h) fatal("gethostbyname failed"); /* look up host's IP address */ S = socket(PF_INET, SOCK STREAM, IPPROTO_TCP); if (s <0) fatal("socket"); memset(&channel, 0, sizeof(channel)); channel.sin_family= AF_INET; memcpy(&channel.sin_addr.s_addr, h->haddr, h->hlength); channel.sin_port= htons(SERVER_PORT); c= connect(s, (struct sockaddr *) &channel, sizeof(channel)); if (c < 0) fatal("connect failed"); /* Connection is now established. Send file name including O byte at end. */ write(s, argv[2], strlen(argv[2])+1); I* Go get the file and write it to standard output. */ while (1) { bytes = read(s, buf, BUF_SIZE); if (bytes <= 0) exit(0); write(1, buf, bytes); } } /* read from socket */ In check for end of file */ /* write to standard output */ fatal(char *string) { printf("%s\n", string); exit(1); } #include #include #include #include /* This is the server code / #include #define SERVER PORT 12345 /* arbitrary, but client & server must agree */ /* block transfer size */ #define BUF_SIZE 4096 #define QUEUE SIZE 10 int main(int argc, char *argv]) { int s, b, I, fd, sa, bytes, on = 1; char buf[BUF_SIZEJ; struct sockaddr_in channel; I* buffer for outgoing file / /* holds IP address */ /* Build address structure to bind to socket. */ memset(&channel, 0, sizeof(channel)); channel.sin_family = AF_INET; channel.sin_addr.s_addr = htonl(INADDRANY); channel.sin_port = htons(SERVER_PORT); /* zero channel */ * Passive open. Wait for connection. */ S = socket(AF_INET, SOCK STREAM, IPPROTO_TCP); * create socket */ if (s < 0) fatal("socket failed"); setsockopt(s, SOLSOCKET, SO REUSEADDR, (char •) &on, sizeof(on)); b= bind(s, (struct sockaddr *) &channel, sizeof(channel)); if (b < 0) fatal("bind failed"); I= isten(s, QUEUE_SIZE); if (I < 0) fatal("listen failed"); /* Socket is now set up and bound. Wait for connection and process it. */ while (1) { sa = accept(s, 0, 0); if (sa < 0) tatal("accept failed"); /* specify queue size */ /* block for connection request / read(sa, buf, BUF_SIZE); /* read file name from socket */ /* Get and return the file. */ fd = open(buf, O_RDONLY); if (fd < 0) tatal("open failed"); /* open the file to be sent back */ while (1) { bytes = read(fd, buf, BUF_SIZE); /* read from file */ if (bytes <= 0) break; write(sa, buf, bytes); } close(fd); close(sa); } /* check for end of file */ /* write bytes to socket / /* close file */ /* close connection */
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 80% (10 reviews)
The connect ma...View the full answer
Answered By
Muhammad Umair
I have done job as Embedded System Engineer for just four months but after it i have decided to open my own lab and to work on projects that i can launch my own product in market. I work on different softwares like Proteus, Mikroc to program Embedded Systems. My basic work is on Embedded Systems. I have skills in Autocad, Proteus, C++, C programming and i love to share these skills to other to enhance my knowledge too.
3.50+
1+ Reviews
10+ Question Solved
Related Book For
Question Posted:
Students also viewed these Computer science questions
-
Match the following terms with their definitions: Term 1. Vulnerability 2. Exploit 3. Authentication 4. Authorization 5. Demilitarized zone (DMZ) 6. Deep packet inspection 7. Router 8. Social...
-
In this problem you are to compare reading a file using a single-threaded file server and a multithreaded server. It takes 12 msec to get a request for work, dispatch it, and do the rest of the...
-
1. TRUE or FALSE? The ARPANet, the precursor to today's Internet, was funded primarily by the U.S. Department of Defense. 2. TRUE or FALSE? Because the ARPANet utilized ordinary phone lines to...
-
Sunblessed Juice Company sells bags of oranges and cartons of orange juice. Sunblessed grades oranges on a scale of 1 (poor) to 10 (excellent). At present, Sunblessed has 100,000 pounds of grade 9...
-
How should an auditor test for proper authorization in the expenditure cycle?
-
True or false: The average or mean of a function on the interval [a, b] defines a linear operator A : C0[a, b] R. S(x) dx A[f] =
-
Jupiter, with an inertia 317.83 times that of Earth, is at an average distance of \(7.784 \times 10^{11} \mathrm{~m}\) from the Sun. At what distance from the centre of the Sun is the center of mass...
-
Nan Ferdinand has been operating an apartment-locator service as a proprietorship. She and Misti Morris have decided to form a partnership. Ferdinands investment consists of cash, $6,000; accounts...
-
10. A plastic ball is rising in water with terminal speed v. If we use a different plastic whose density is double that of the original ball but still less that density of water, keeping the radius...
-
Lessee Ltd (1.e., the lessee) signed a contract with Lessor Ltd (1.e., the lessor) to lease items of office equipment. You are provided with the following lease details: Commencement date Lease term...
-
In Figure 6-20, suppose a new flow E is added that takes a path from R1 to R2 to R6. How does the max-min bandwidth allocation change for the five flows?
-
What are the disadvantages of using small, fixed-length cells in ATM?
-
Why might Venezuelas monetary authorities decide to cut interest rates in this situation? To work Problems 28 to 30, use the information that Venezuelas economy is expected to witness a staggering...
-
Samantha normally requires 1 3 7 0 0 kJ ( about 3 2 7 4 Calories ) of food energy per day. If Samantha consumes 1 4 3 8 5 kJ per day, she will steadily gain weight. How much time must Samantha spend...
-
Assume that you have been hired by the Ministry of Health of Malaysia to study the determinants of smoking behavior and that you estimate the following cross-sectional model based on data for all 13...
-
Ques 4:Consider a coffee shop that you may have visited or seen lately (e.g., Tim Hortons or Starbucks). Assume that there is one franchise owner, one manager, one baker, and two coffee servers (five...
-
A company is thinking about dropping one of its product lines. The monthly income statement for the product line is below: Revenue $100,000 Variable Costs. $29,000. Salary of Product Line $10,000...
-
EMP cost in the manufacturing of 270,000 units are as follows: Variable Costs Direct Labor $2,200,000 Direct Materials $1,900,000 Variable Selling Expenses $900,000 Variable Overhead $940,000 Fixed...
-
Do individuals who want the government to help people improve their standard of living also believe that government should do more to solve the countrys problems? Conduct a test to determine whether...
-
A Firm intends to invest some capital for a period of 15 years; the Firm's Management considers three Options, each consisting of purchasing a machinery of a specific brand, different for each...
-
Suppose an IP packet is fragmented into 10 fragments, each with a 1% (independent) probability of loss. To a reasonable approximation, this means there is a 10% chance of losing the whole packet due...
-
Path MTU is the smallest MTU of any link on the current path (route) between two hosts. Assume we could discover the Path MTU of the path used in the previous exercise and that we use this value as...
-
Suppose a TCP message that contains 2048 bytes of data and 20 bytes of TCP header is passed to IP for delivery across two networks of the Internet (i.e., from the source host to a router to the...
-
Ray, a high-net worth individual, is a new client of your CPA firm during tax season in the spring of 20X1. Ray tells you about an investment of his during 20X0. When you tell Ray that his investment...
-
Give an example of an opportunity cost that an accountant would not count as a cost. Why would the accountant ignore this cost? An accountant would not count the owners opportunity cost of...
-
We choose the parameter where L(0) = Rd to minimize the regularized empirical risk, L(0) + Xr(0), (x, y) is the empirical loss, r(0) is the regularizer, and > >0 is the regularization parameter. (The...
Study smarter with the SolutionInn App