




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Exam; Class: NETWORK PROGRAMMING; Subject: COMPUTER SCIENCE; University: Clemson University; Term: Fall 2008;
Typology: Exams
1 / 8
This page cannot be seen from the preview
Don't miss anything!
int socket(int family,int type,int proto); int bind( mysock, (struct sockaddr) &myaddr, sizeof(myaddr) ); int listen( int sockfd, int backlog); int accept( int sockfd, struct sockaddr cliaddr, socklen_t *addrlen); int connect( int sockfd,const struct sockaddr *server, socklen_t addrlen); **int read( int fd, char buf, int max); int write( int fd, char buf, int num);
#include <stdio.h> /* standard C i/o facilities / #include <unistd.h> / Unix System Calls / #include <sys/types.h> / system data type definitions / #include <sys/socket.h> / socket specific definitions / #include <netinet/in.h> / INET constants and stuff / #include <arpa/inet.h> / IP address conversion stuff */ int main() { int ld, sd, addrlen,length, n; struct sockaddr_in skaddr, from; char buff[100]; if ((ld = socket( PF_INET, SOCK_STREAM, 0 )) < 0) { perror("Problem creating socket\n"); exit(1); } skaddr.sin_family = AF_INET; skaddr.sin_addr.s_addr = htonl(INADDR_ANY); skaddr.sin_port = htons(0); if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) { exit(0); } length = sizeof( skaddr ); if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) { exit(1); } if (listen(ld,5) < 0 ) { exit(1); } while (1) {
if ( (sd = accept( ld, (struct sockaddr*) &from, &addrlen)) < 0) { exit(1); } length = sizeof( skaddr ); if (getsockname(sd, (struct sockaddr *) &skaddr, &length)<0) { exit(1); } while ((n=read(sd,buff,100))>0) { write(1,buff,n); } close(sd); } }