Recitation_03-Networking_and_Concurrency

Recitation_03-Networking_and_Concurrency - 9/27/11 1 1 CSE...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/27/11 1 1 CSE 421/521 - Operating Systems Fall 2011 Recitations Presented by .......... University at Buffalo September ...., 2011 Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar 2 Network Programming 9/27/11 2 3 TCP Client-Server view Connection-oriented socket connections 4 Server Side Socket Details 9/27/11 3 5 Client Side Socket Details 6 Example: A Simple Time Server #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define PORTNUM 8824 #define oops(msg) { perror(msg) ; exit(1) ; } 9/27/11 4 7 void main(int ac, char **av) { struct sockaddr_in saddr; /* build our address here */ struct hostent *hp; /* this is part of our */ char hostname[256]; /* address */ int slen,sock_id,sock_fd; /* line id, file desc */ FILE *sock_fp; /* use socket as stream */ char *ctime(); /* convert secs to string */ long time(), thetime; /* time and the val */ gethostname( hostname , 256); /* where am I ? */ hp = gethostbyname( hostname ); /* get info about host */ bzero( &saddr, sizeof(saddr) ); /* zero struct */ /* fill in hostaddr */ bcopy( hp->h_addr, &saddr.sin_addr, hp->h_length); saddr.sin_family = AF_INET ; /* fill in socket type */ saddr.sin_port = htons(PORTNUM); /* fill in socket port */ sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a socket */ if ( sock_id == -1 ) oops( "socket" ); if ( bind(sock_id, &saddr, sizeof(saddr)) != 0 )/* bind it to */ 8 while ( 1 ){ sock_fd = accept(sock_id, NULL, NULL); /* wait for call */ printf("** Server: A new client connected!"); if ( sock_fd == -1 ) oops( "accept" ); /* error getting calls */ sock_fp = fdopen(sock_fd,"w"); /* we'll write to the */ if ( sock_fp == NULL ) /* socket as a stream */ oops( "fdopen" ); /* unless we can't */ thetime = time(NULL); /* get time */ /* and convert to strng */ fprintf( sock_fp, "**************************************\n"); fprintf( sock_fp, "** From Server: The current time is: "); fprintf( sock_fp, "%s", ctime(&thetime) ); fprintf( sock_fp, "**************************************\n"); fclose( sock_fp ); /* release connection */ fflush(stdout); /* force output */ } } 9/27/11 5 9 Client-Server Implementation 10 main(int argc, char **argv){ int len, port_sk, client_sk; char *errmess; port_sk = tcp_passive_open(port); /* establish port */ if ( port_sk < 0 ) { perror("socket"); exit(1); } printf("start up complete\n"); client_sk = tcp_accept(port_sk); /* wait for client to connect */ close(port_sk); /* only want one client, so close port_sk */ for(;;) { /* talk to client */ len = read(client_sk,buff,buf_len); //listen printf("client says: %s\n",buff); .......
View Full Document

Page1 / 19

Recitation_03-Networking_and_Concurrency - 9/27/11 1 1 CSE...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online