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: 84% (13 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?
-
In what ways do the new types of financial instruments differ from traditional financial instruments? What additional risks are associated with such securities?
-
Consider rewards and punishments or other reinforcements you have experienced at work. What was the effect on your motivation to learn or change your behavior?(p. 93)
-
Instead of worrying about job satisfaction, companies should be trying to create environments where performance is enabled. What do you think this statement means? Explain. Whats your reaction to...
-
Hagerup Inc. is considering a new project. The initial cash outflows are $18,000. The Company believes the project will yield cash inflows of $4,320 in year 1, $6,480 in year 2, $5,400 in year 3, and...
-
Havel Robotics Company (a U.S.-based firm) exports 25,000 industrial robots per year to China under an agreement that covers a 5-year period. In China, the robots are sold for the RMB (Chinese...
-
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?
-
Explain what is meant by division and classification.
-
In 2024, the Westgate Construction Company entered into a contract to construct a road for Santa Clara County for $10,000,000. The road was completed in 2026. Information related to the contract is...
-
Briefly describe the case you have chosen. Categorize the social worker's experience as vicarious trauma, compassion fatigue, or burnout. Provide justification. Identify the social worker's score on...
-
Given f(x) below, find f'(x). f(x) = = m 5z In (2) et dt
-
Olsen & Alain, CPAs (O&A) performed the audit of Rocky Point Brewery (RPB), a public company in 20X1 and 20X2. In 20X2, O&A also performed tax services for the company. Which statement best describes...
-
Exercise 9-4 (Algo) Prepare a Flexible Budget Performance Report [LO9-4] Vulcan Flyovers offers scenic overflights of Mount Saint Helens, the volcano in Washington State that explosively erupted in...
-
Under optimum conditions, the eye will perceive a flash if about 60 photons arrive at the cornea. How much energy is this in joules if the wavelength of the light is 550 nm?
-
The executor of Gina Purcells estate has recorded the following information: Assets discovered at death (at fair value): Cash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ....
-
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...
-
Comparative financial statements for Weller Corporation, a merchandising company, for the year ending December 31 appear below. The company did not issue any new common stock during the year. A total...
-
Mrquered Mrquered
-
You plan to invest $10,00 today in an investment account earning 5% interest. You then plan to invest an additional $1,000 into this account each year for the next twenty years. How much money will...
Study smarter with the SolutionInn App