ADVANCED NETWORK PROGRAMMING BİLGİN METİN ADVANCED SOCKET OPTIONS  IPv4&IPv6 Interoperability  IOCTL Options  Advanced I/O Functions  Nonblocking I/O.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Nonblocking I/O Blocking vs. non-blocking I/O
Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
Threads Programming Thread creation Synchronization.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming Application Programming Interface.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
I/O Multiplexing© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science.
Tutorial 8 Socket Programming
1 Advanced I/O Computer Network Programming. 2 Regarding Project1 –Use RCS to store different revisions of your programs –this is a requirement! –you.
Fork Fork is used to create a child process. Most network servers under Unix are written this way Concurrent server: parent accepts the connection, forks.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
Networking S04, Recitation, Section A
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Elementary TCP Sockets
1 Non-blocking I/O Computing Network Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
IT1352-NETWORK PROGRAMMING AND MANAGEMENT
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Sirak Kaewjamnong Computer Network Systems
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Chapter 17 routing sockets. abstract Introduction datalink socket address structure reading and writing sysctl operation get_ifi_info function interface.
UNIX Network Programming1 Chapter 16. ioctl Operations.
Nonblocking I/O Blocking vs. non-blocking I/O Nonblocking input, output, accept, and connect Readings –UNP Ch16 1.
UNIX Network Programming1 Chapter 15. Nonblocking I/O.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
TELE 402 Lecture 10: Unix domain … 1 Overview Last Lecture –Daemon processes and advanced I/O functions This Lecture –Unix domain protocols and non-blocking.
Chapter18 broadcasting. contents Introduction broadcast address unicast versus broadcast dg_cli function using broadcasting Race conditions.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
TELE 402 Lecture 9: Daemon … 1 by Dr Z. Huang Overview Last Lecture –Broadcast and multicast This Lecture –Daemon processes and advanced I/O functions.
Introduction to Socket
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
Introduction A Simple Daytime Client A Simple Daytime Server
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
UNIX Network Programming1 Chapter 13. Advanced I / O Functions.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Ioctl Operations. ioctl Function Interface Configuration  Netstat, ifconfig command 에서 사용.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
I/O Multiplexing.
Chapter 5. TCP Client-Server Example
Chapter 10 IPv4 and IPv6 Interoperability
Elementary UDP Sockets
Network Programming CSC- 341
Chapter 8 Elementary UDP Socket
UNIX Domain sockets The Linux Programming Interface (ch 57)
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
UDP Sockets Programming
TCP Sockets Programming
Advanced Network Programming spring 2007
Elementary UDP Sockets connectionless, unreliable, datagram
Advanced I/O Functions
TCP Client-Server Example
Internet Networking recitation #8
Presentation transcript:

ADVANCED NETWORK PROGRAMMING BİLGİN METİN ADVANCED SOCKET OPTIONS  IPv4&IPv6 Interoperability  IOCTL Options  Advanced I/O Functions  Nonblocking I/O Functions  Thrads  Client&Server Design Alternatives

IPv4&IPv6 Interoperability  Over the coming years there will probably be a gradual transition of the internet from IPv4 to IPv6.  During this transition phase, it is important that existing IPv4 applications continue to work with newer IPv6 applications.

IPv4&IPv6 Interoperability TCP IPv4 UDP IPv6IPv4IPv6 IPv4IPv6 IPv4 mapped IPv4IPv6 IPv4 client and IPv6 server An IPv4 socket can always be changed to IPv6 socket. Any IPv4 address already associated with the socket are converted to IPv4 mapped IPv6 address

TCP IPv4 UDP IPv6IPv4IPv6 IPv4IPv6 IPv4 mapped IPv4IPv6 IPv4&IPv6 Interoperability IPv4 server and IPv6 client An IPv6 socket can be changed to IPv4 socket only if its address already associated with the socket is IPv4 mapped IPv6 address

IPv4&IPv6 Interoperability Int af; socklen_t clilen; Struct sockaddr_in6 cli; Struct hostent *ptr; af=AF_INET6; setsockopt(STDIN_FILENO, IPPROTOv6, IPV6_ADDRFORM, &af, sizeof(af)); clien=sizeof(cli); getpeername(0, &cli, &clilen); Ptr=gethostbyaddr(&cli, sinb_addr, 16, AF_INET6);

IOCTL Options  The IOCTL function has traditionally been the system interface used for everything that did not put into some nice defined category.  Possix is geting rid of IOCTL by creating specific wrapper functions.  Nevertheless numerous functions remain for obtaining the interface information, accessing the route table and the arp cache for example. İnt ioctl(int fd, int request,..., *argument); Some important request options  FIONBIO : The nonblocking flag is cleared or turned on depending on third argument (zero or non-zero)  FIONREAD: Return in the integer pointed to by third argument to ioctl, the number of bytes currently in the socket receive buffer.

IOCTL Options #include"unp.h" #include int main(int argc, char **argv) { intfamily, sockfd; charstr[INET6_ADDRSTRLEN]; char**pptr; unsigned char*ptr; struct arpreqarpreq; struct sockaddr_in*sin; pptr = my_addrs(&family); for ( ; *pptr != NULL; pptr++) { printf("%s: ", Inet_ntop(family, *pptr, str, sizeof(str))); switch (family) { case AF_INET: sockfd = Socket(AF_INET, SOCK_DGRAM, 0); sin = (struct sockaddr_in *) &arpreq.arp_pa; bzero(sin, sizeof(struct sockaddr_in)); sin->sin_family = AF_INET; memcpy(&sin->sin_addr, *pptr, sizeof(struct in_addr)); Ioctl(sockfd, SIOCGARP, &arpreq); ptr = &arpreq.arp_ha.sa_data[0]; printf("%x:%x:%x:%x:%x:%x\n", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)); break; default: err_quit("unsupported address family: %d", family); }} exit(0); }

Advanced I/O Functions Socket timeouts.  Alarm  Select  SO_RCVTIMEO and SO_SNDTIMEO Variations of the read and write functions. I. Recv and send II. Readv and writev III. Recvmsg and sendmsg How to determine how much data is in the socket buffer. I. ioctl with FIONREAD parameter II. MSG_PEEK option with recv and send functions

Advanced I/O Functions #include"unp.h" /* connect_timeo */ static voidconnect_alarm(int); int connect_timeo(int sockfd, const SA *saptr, socklen_t salen, int nsec) { Sigfunc*sigfunc; intn; sigfunc = Signal(SIGALRM, connect_alarm); if (alarm(nsec) != 0) err_msg("connect_timeo: alarm was already set"); if ( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0) { close(sockfd); if (errno == EINTR) errno = ETIMEDOUT; } alarm(0);/* turn off the alarm */ Signal(SIGALRM, sigfunc);/* restore previous signal handler */ return(n); } static void connect_alarm(int signo) { return;/* just interrupt the connect() */ } void /* end connect_timeo */ Connect_timeo(int fd, const SA *sa, socklen_t salen, int sec) { if (connect_timeo(fd, sa, salen, sec) < 0) err_sys("connect_timeo error"); }

NONBLOCKING I/O  By default sockets are blocking  We can divide the socket calls that may block into for categories  Input Operations  Output operations  Accept  Inıtiating outgoing connections

NONBLOCKING I/O #include /* STR_CLI */ #defineMAXLINE512 str_cli(fp, sockfd) register FILE*fp; register intsockfd; { intn; charsendline[MAXLINE], recvline[MAXLINE + 1]; while (fgets(sendline, MAXLINE, fp) != NULL) { n = strlen(sendline); if (writen(sockfd, sendline, n) != n) err_sys("str_cli: writen error on socket"); /* Now read a line from the socket and write it to stdr out*/ n = readline(sockfd, recvline, MAXLINE); if (n < 0) err_dump("str_cli: readline error"); recvline[n] = 0;/* null terminate */ fputs(recvline, stdout); } if (ferror(fp)) err_sys("str_cli: error reading file"); }

NONBLOCKING I/O SERVER parent child fork stdout stdin CLIENT

NONBLOCKING I/O #include"unp.h“ /* STR_CLI FUNTION USING FORK */ void str_cli(FILE *fp, int sockfd) { pid_tpid; charsendline[MAXLINE], recvline[MAXLINE]; if ( (pid = Fork()) == 0) {/* child: server -> stdout */ while (Readline(sockfd, recvline, MAXLINE) > 0) Fputs(recvline, stdout); kill(getppid(), SIGTERM);/* in case parent still running */ exit(0); } /* parent: stdin -> server */ while (Fgets(sendline, MAXLINE, fp) != NULL) Writen(sockfd, sendline, strlen(sendline)); Shutdown(sockfd, SHUT_WR);/* EOF on stdin, send FIN */ pause(); return; }

THREADS  FORK works well... In traditional UNIX model, when a process needs something performed by Another entity, it forks a child process and lets the child perform the processing.  But fork is expensive!  Memory is copied from the parent to the child  IPC is required to pass information between the parent and child after the fork Threads help with both problem:  Thread creation can be 10 or 100 times faster than process creation.  All threads within a process share the same global memory

THREADS SERVER Copyto Thread Main( ) Thread Pthread_create stdout stdin CLIENT

#include"unpthread.h" void*copyto(void *); static intsockfd;/* global for both threads to access */ static FILE*fp; void str_cli(FILE *fp_arg, int sockfd_arg) { charrecvline[MAXLINE]; pthread_ttid; sockfd = sockfd_arg;/* copy arguments to externals */ fp = fp_arg; Pthread_create(&tid, NULL, copyto, NULL); while (Readline(sockfd, recvline, MAXLINE) > 0) Fputs(recvline, stdout); } void * copyto(void *arg) { charsendline[MAXLINE]; while (Fgets(sendline, MAXLINE, fp) != NULL) Writen(sockfd, sendline, strlen(sendline)); Shutdown(sockfd, SHUT_WR);/* EOF on stdin, send FIN */ return(NULL); /* 4return (i.e., thread terminates) when end-of-file on stdin */ } THREADS

CLIENT&SERVER DESIGN ALTERNATIVES Traditional Approaches  Iterative Server  Concurrent Server  Select option  Concurrent Servers with threads Modern Approaches  Preforking Concurrent Server  Prethreading concurrent server

CLIENT-SERVER DESIGN ALTERNATIVES  If the server is not heavily used, traditional concurrent server model, with one fork per client is fine.  Preforking and Prethreading Concurrent server approach reduces process control CPU time compared to the traditional one by a factor of 10 or more.

ADVANCED NETWORK PROGRAMMING ADVANCED SOCKET OPTIONS  IPv4&IPv6 Interoperability  IOCTL Options  Advanced I/O Functions  Nonblocking I/O Functions  Thrads  Client&Server Design Alternatives