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?
-
What is the velocity, as a fraction of c, of a. A proton with 500 GeV total energy? b. An electron with 2.0 GeV total energy?
-
Explain the difference between physical and logical schemas.
-
Blue Jay Delivery Service is incorporated on January 2, 2010, and enters into the following transactions during its first month of operations: January 2: Filed articles of incorporation with the...
-
Under Working Capital Management and Debt What are some of the advantages and disadvantages of short-term vs long-term debt? What are some actions a firm can take to shorten its cash? Public and...
-
Problem 9 (50 Points) An air-cooled aluminum heat sink is used to keep electronics cool (see image and diagram). The cooling requirements are being significantly increased. To increase the rate of...
-
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?
-
Describe preliminary, primary, secondary, or tertiary wastewater treatment processes.
-
An employer can be held liable a. if an employee intentionally injures someone. b. for the negligence of an employee who goes on a frolic or detour if the employees deviation is reasonably...
-
Give an example of a non-delegable duty for purposes of independent contractors.
-
The doctrine of respondeat superior does not apply if an employee is a. acting outside the scope and furtherance of their employment. b. serving their employer and meeting their personal needs. c....
-
____________ ____________ means let the person higher up answer.
-
Explain the level of control an employer must retain for the worker to be considered an employee and not an independent contractor.
-
Another type of system that is employed in engineering work is a group of parallel components or a parallel system. In this more conservative approach, the probability that the system operates is...
-
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...
-
Sophia operates a large singing school with 22 employees. She receives advance fee payments from students in respect of each course (a course consists of 26 lessons). If a student does not attend all...
-
What role does agency play in the Hypodermic Model of Communication? Long after the model has been abandoned by experts in persuasion and mass media, why does this model live on as common sense?
-
Daniel is a resident individual who carries on a retail business but does not use the SBE method of accounting for income in his business. On 30 June of the current tax year he had trade debtors of...
Study smarter with the SolutionInn App