inetserver - exit(1 listen(soc 5 historically 5 was good...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/***** inetserver.c *****/ #include <stdio.h> #include <string.h> #include <strings.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> /* Internet domain header */ int main() { int soc, ns, k; char buf[256]; /* set up listening socket soc */ if ((soc = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { perror("server:socket"); exit(1); } /* set options on socket so that we can reuse the port quickly */ int on = 1; if (setsockopt(soc, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on)) == -1) { perror("setsockopt -- REUSEADDR"); } /* get ready to bind the socket to an address */ struct sockaddr_in self; self.sin_family = AF_INET; self.sin_port = htons(PORT); self.sin_addr.s_addr = INADDR_ANY; bzero(&(self.sin_zero), 8); printf("Listening on %d\n", PORT); if (bind(soc, (struct sockaddr *)&self, sizeof(self)) == -1) { perror("server:bind");
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: exit(1); } listen(soc, 5); // historically 5 was good. Now too small /* accept connection request */ printf("Calling accept\n"); /* when accept returns it gives us a new socket ns on which we will communicate */ /* need peer set up before we can send to acccept */ struct sockaddr_in peer; peer.sin_family = AF_INET; unsigned int peer_len = sizeof(peer); ns = accept(soc, (struct sockaddr *)&peer, &peer_len); if (ns < 0) { perror("server:accept"); close(soc); exit(1); } printf("I've accepted a connection\n"); /* data transfer on connected socket ns */ k = read(ns, buf, sizeof(buf)); char host[256]; gethostname(host, 256); printf("SERVER ON %s RECEIVED: %s\n", host, buf); write(ns, buf, k); sleep(5); // notice that we only transmit 8 chars this time not the 9th which is the null terminator write(ns, "cool eh?", 8); close(ns); close(soc); return(0); }...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern