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: 83% (12 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?
-
If Oui watches sells 300 watches at $48 per watch and has variable costs of $18 per watch and fixed costs of $4,000, what is the projected profit?
-
A researcher wants to determine whether children are more likely to be born on certain days of the week. She will sample 350 births and record the day of the week for each. The null hypothesis is...
-
An object is fired vertically upward from the surface of the Earth (of radius RE) with an initial speed vi that is comparable to but less than the escape speed vesc. (a) Show that the object attains...
-
Kellogg pays $3.50 in annual per share dividends to its common stockholders, and its recent stock price was $93.80. Assume that Kellogg's cost of equity capital is 5.0%. Estimate Kellogg's expected...
-
There is a parcel of land next to the Playful Paws, Inc. building. Ellen, the owner of this property, approached John to discuss the idea of selling it to him. John is interested. Ellen knows that...
-
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?
-
The manager of the OBrian Glass Company is planning the production of automobile windshields for the next four months. The demand for the next four months is projected to be as shown in the following...
-
Please help me with a complete outline..A very detailed outline for the following questions. I am doing class presentation on it.. I have picked puma and Adidas soccer cleats Compare two competing...
-
Determine whether the following series is convergent or divergent: 5n (3 - 5. 13n -) and determine its n=1 value. - Enter a real number (the value of the series) if the series is convergent, if it...
-
2. The transactions of Spade Company appear below: 1/1 K. Spade, owner, invested $100,750 cash in the company in exchange for common stock 1/5 The company purchased supplies for $1,250 cash. 1/10 The...
-
Comparing Three Depreciation Methods Dexter Industries purchased packaging equipment on January 8 for $187,000. The equipment was expected to have a useful life of four years, or 5,200 operating...
-
Mackey Publishing Company ( Mackey ) is a publisher of novels. The monthly equipment maintenance cost for Mackey is considered to be a mixed cost. The variable portion of the cost is related to the...
-
Write the compound statement in words. Let p: Joe has an iPad. q: Brie has a MacBook ~ p ~q
-
Design a circuit which negative the content of any register and store it in the same register.
-
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...
-
Santiago Inc. processes a base chemical into plastic. Standard costs and actual costs for direct materials, direct labor, and factory overhead incurred for the manufacture of 74,000 units of product...
-
Describe how a nursing facility might go about marketing itself and the services it provides to the community. When might management use a public relations firm? provide a details answer with...
-
Assume that three identical units of merchandise are purchased as follows: 1 unit purchased on July 1 st for $ 1 0 7 1 unit purchased on July 1 5 th for $ 1 2 4 1 unit purchased on July 2 5 th for $...
Stochastic Calculus For Finance Ii Continuous Time Models 1st Edition - ISBN: 7506272881 - Free Book
Study smarter with the SolutionInn App