1 / 28

Socket Programming

Socket Programming. Socket address structure in <sys/socket.h>: struct sockaddr{ u_short sa_family; /* address family: AF_xxx value */ char sa_data[14]; /* up to 14 bytes of protocol- */ /* specific address */ }. Socket Programming. Socket address structure in <netinet/in.h>:

genero
Download Presentation

Socket Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programming • Socket address structure in <sys/socket.h>: struct sockaddr{ u_short sa_family; /* address family: AF_xxx value */ char sa_data[14]; /* up to 14 bytes of protocol- */ /* specific address */ }

  2. Socket Programming • Socket address structure in <netinet/in.h>: (Internet family) struct in_addr{ u_long s_addr; /* 32-bit netid/hostid */ /* network byte ordered */ }

  3. Socket Programming struct sockaddr_in{ short sin_family; /* AF_INET */ u_short sin_port; /* 16-bit port number */ /* network byte ordered */ struct in_addr sin_addr; /* 32-bit netid/hostid */ /* network byte ordered */ char sin_zero[8]; /* unused */ }

  4. Socket Programming • socket : specify the type of communication protocol desired (TCP, UDP etc.) #include <sys/types.h> #include <sys/socket.h> int socket(int family, int type, int protocol); family: AF_UNIX (UNIX internal protocol) AF_INET (Internet protocols) AF_NS (Xerox NS protocols) AF_IMPLINK (IMP link layer) (AF: address family)

  5. Socket Programming type: SOCK_STREAM (stream socket, TCP) SOCK_DGRAM (datagram socket, UDP) SOCK_RAW (raw socket) SOCK_SEQPACKET (sequenced packet socket) SOCK_RDM (reliably delivered message socket) (not implemented yet) protocol: typically set to 0 for most user applications The output of the system call is the socket descriptor.

  6. Socket Programming • bind: assign a name to an unnamed socket #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct sockaddr *myaddr, int addrlen); sockfd: socket descriptor (return by*myaddr: a pointer to a protocol-specific address addrlen: the size of this address structure The output > 0 if success; otherwise, it fails.

  7. Socket Programming • connect: establish a connection with a server #include <sys/types.h> #include <sys/socket.h> int connect (int sockfd, struct sockaddr *servaddr, int addrlen); same as the system call bind. The output > 0 if success; otherwise, it fails.

  8. Socket Programming • listen: is used by a connection-oriented server to indicate that it is willing to receive connections #include <sys/types.h> #include <sys/socket.h> int listen (int sockfd, struct backlog); backlog: specify how many connection requests can be queued by the system while it waits for the server to execute the accept system call (default as 5). The output > 0 if success; otherwise, it fails.

  9. Socket Programming • accept: take the first connection request on the queue and create another socket with the same properties as sockfd. #include <sys/types.h> #include <sys/socket.h> int accept (int sockfd, struct sockaddr *peer, int *addrlen); peer: the address of connected peer process (the client). addrlen: the size of peer. The output of the system call is the socket descriptor.

  10. Socket Programming • send, sendto, recv and recvform #include <sys/types.h> #include <sys/socket.h> int send(int sockfd, char *buff, int nbytes, int flags); int sendto(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); int recv(int sockfd, char *buff, int nbytes, int flags); int recvfrom(int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen); flag: usually zero

  11. Socket Programming • close: close a socket #include <sys/types.h> #include <sys/socket.h> int close (int fd); • bzero: writes the specified number of null bytes to the specified destination. bzero(char *dest, int nbytes);

  12. Socket Programming • htonl: convert host-to-network to long integer • htons: convert host-to-network to short integer #include <sys/types.h> #include <netinet/in.h> u_long htonl(u_long hostlong); u_short htons(u_long hostshort);

  13. Socket Programming • inet_addr: convert a character string in dotted-decimal notation to a 32-bit Internet address #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> unsigned long inet_addr(char *ptr);

  14. connect ( ) socket ( ) socket ( ) bind ( ) listen ( ) connection establishment accept ( ) blocks until connection from client data (request) write ( ) read ( ) process request data (reply) write ( ) read ( ) Socket Programming Connection-oriented Protocol Server Client

  15. bind ( ) socket ( ) socket ( ) bind ( ) recvfrom ( ) blocks until data received from a client data (request) sendto ( ) process request data (reply) sendto ( ) recvfrom ( ) Socket Programming Connectionless Protocol Server Client

  16. Socket Programming int tcp_serv() /* server program: setup a TCP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in cli_addr, serv_addr; if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) err_dump(“server: can't open stream socket”); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* INADDR_ANY: any Internet interface on the system */

  17. Socket Programming serv_addr.sin_port = htons(SERV_TCP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) err_dump(“server: can't bind local address”); listen(socfd, 5); clilen = sizeof(cli_addr); if ((newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen)) < 0) err_dump(“server: accept error”); }

  18. Socket Programming int tcp_client() /* client program: setup a TCP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in serv_addr; bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR); serv_addr.sin_port = htons(SERV_TCP_PORT);

  19. Socket Programming if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0) err_sys(“client: can't open stream socket”); if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) err_sys(“client: can't bind local address”); /* do the transmission */ close(sockfd); }

  20. Socket Programming tcp_send(s_act, msg_ptr, msg_len) /* ipc_send: send data message “msg” within stream domain */ int s_act; /* actual socket descriptor of connection */ char *msg_ptr; /* pointer to data msg unit */ int msg_len; /* length of data msg unit */ { int cc = 0, rc; cc = send(s_act, msg_ptr, msg_len, 0); /*UNIX system call */ if (cc == -1) {perror("send: ! "); rc = cc; } /* error report */ else rc = OK (=1); return (rc); }

  21. Socket Programming tcp_receive(s_act, buf_ptr, buf_len) /* receive data within UNIX*/ int s_act; /* socket descriptor of ipc connection */ union u_du *buf_ptr; /* pointer to message buffer */ int *buf_len; /* length of message buffer */ { int cc, rc; cc = recv(s_act, buf_ptr, *buf_len, 0); /* UNIX syst call */ if (cc == -1) { perror("recv: "); rc = cc; } else {*buf_len = cc; rc = OK (= 1); } return (rc); }

  22. Socket Programming int udp_serv() /* server program: setup a UDP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in cli_addr, serv_addr; if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) err_dump(“server: can't open datagram socket”); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* INADDR_ANY: any Internet interface on the system */

  23. Socket Programming serv_addr.sin_port = htons(SERV_UDP_PORT); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) err_dump(“server: can't bind local address”); /* do the transmission */ }

  24. Socket Programming int udp_client() /* client program: setup a UDP connection */ { int sockfd, newsockfd, clilen; struct sockaddr_in cli_addr, serv_addr; bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR); serv_addr.sin_port = htons(SERV_UDP_PORT); if ((sockfd = socket (AF_INET, SOCK_DGRAM, 0)) < 0) err_dump(“client: can't open datagram socket”);

  25. Socket Programming bzero((char *) &cli_addr, sizeof(cli_addr)); cli_addr.sin_family = AF_INET; cli_addr.sin_addr.s_addr = htonl(INADDR_ANY); cli_addr.sin_port = htons(0); if (bind(sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)) < 0) err_dump(“client: can't bind local address”); /* do the transmission */ close(sockfd); }

  26. Socket Programming udp_send( msg_ptr, msg_len, addr_ptr, addr_len ) char *msg_ptr; /* pointer to message */ int msg_len; /* length of message */ struct sockaddr_in *addr_ptr; /* socket address */ int addr_len; /* length of socket address */ { int cc; addr_ptr.sin_addr.s_addr = htonl(st_addr); /*convert address*/ cc = sendto(s_udp, msg_ptr, msg_len, 0 addr_ptr, addr_len ); if (cc == -1) {perror("sendto: "); return(cc);} else return(OK); }

  27. Socket Programming udp_receive(msg_ptr, msg_len, addr_ptr, addr_len) char *msg_ptr; /* pointer to message “msg” */ int msg_len; /* length of message */ struct sockaddr_in *addr_ptr; /* socket address */ int addr_len; /* length of socket address */ { int cc; cc = recvfrom(s_udp, msg_ptr, *msg_len, 0, addr_ptr, addr_len ); if (cc == -1) {perror("recvfrom: "); return(cc); } else return(OK); }

  28. Thread Programming (Try the program) Use online help to find out the information about thread programming. Try: man mutex man thr_create man thr_join …... • #define _REENTRANT • #include <stdio.h> • #include <thread.h> • #define NUM_THREADS 12 • void *change_global_data(void *); /* for thr_create() */ • main(int argc,char * argv[]) { • int i=0; • for (i=0; i< NUM_THREADS; i++) { • thr_create(NULL, 0, change_global_data, NULL, 0, NULL); • } • while ((thr_join(NULL, NULL, NULL) == 0)); • } • void * change_global_data(void *null) { • static mutex_t Global_mutex; • static int Global_data = 0; • mutex_lock(&Global_mutex); • Global_data++; • sleep(1); • printf("%d is global data\n",Global_data); • mutex_unlock(&Global_mutex); • return NULL; • }

More Related