Download presentation
Presentation is loading. Please wait.
Published byKatrina McDonald Modified over 9 years ago
1
zSocket address structure in : struct sockaddr{ u_shortsa_family;/* address family: AF_xxx value */ charsa_data[14];/* up to 14 bytes of protocol- */ /* specific address */ } Socket Programming
2
zSocket address structure in : (Internet family) struct in_addr{ u_longs_addr;/* 32-bit netid/hostid */ /* network byte ordered */ } Socket Programming
3
struct sockaddr_in{ shortsin_family;/* AF_INET */ u_shortsin_port;/* 16-bit port number */ /* network byte ordered */ struct in_addr sin_addr;/* 32-bit netid/hostid */ /* network byte ordered */ charsin_zero[8];/* unused */ } Socket Programming
4
zsocket : specify the type of communication protocol desired (TCP, UDP etc.) #include 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) Socket Programming
5
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. Socket Programming
6
zbind: assign a name to an unnamed socket #include 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. Socket Programming
7
zconnect: establish a connection with a server #include int connect (int sockfd, struct sockaddr *servaddr, int addrlen); same as the system call bind. The output > 0 if success; otherwise, it fails. Socket Programming
8
zlisten: is used by a connection-oriented server to indicate that it is willing to receive connections #include 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. Socket Programming
9
zaccept: take the first connection request on the queue and create another socket with the same properties as sockfd. #include 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. Socket Programming
10
zsend, sendto, recv and recvform #include 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 Socket Programming
11
zclose: close a socket #include int close (int fd); zbzero: writes the specified number of null bytes to the specified destination. bzero(char *dest, int nbytes); Socket Programming
12
zhtonl: convert host-to-network to long integer zhtons: convert host-to-network to short integer #include u_long htonl(u_long hostlong); u_short htons(u_long hostshort); Socket Programming
13
zinet_addr: convert a character string in dotted- decimal notation to a 32-bit Internet address #include unsigned long inet_addr(char *ptr); Socket Programming
14
read ( ) connect ( ) socket ( ) write ( ) socket ( ) bind ( ) listen ( ) read ( ) accept ( ) write ( ) blocks until connection from client process request connection establishment data (request) data (reply) Connection-oriented Protocol Server Client Socket Programming
15
Connectionless Protocol Server Client recvfrom ( ) bind ( ) socket ( ) sendto ( ) data (request) data (reply) socket ( ) bind ( ) recvfrom ( ) sendto ( ) blocks until data received from a client process request Socket Programming
16
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 */ Socket Programming
17
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”); } Socket Programming
18
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); Socket Programming
19
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); } Socket Programming
20
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); } Socket Programming
21
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); } Socket Programming
22
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 */ Socket Programming
23
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 */ } Socket Programming
24
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”); Socket Programming
25
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); } Socket Programming
26
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); } Socket Programming
27
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); } Socket Programming
28
z#define _REENTRANT z#include z#define NUM_THREADS 12 zvoid *change_global_data(void *); /* for thr_create() */ z main(int argc,char * argv[]) { z int i=0; z for (i=0; i< NUM_THREADS; i++) { z thr_create(NULL, 0, change_global_data, NULL, 0, NULL); z } z while ((thr_join(NULL, NULL, NULL) == 0)); z} zvoid * change_global_data(void *null) { z static mutex_t Global_mutex; z static int Global_data = 0; z mutex_lock(&Global_mutex); z Global_data++; z sleep(1); z printf("%d is global data\n",Global_data); z mutex_unlock(&Global_mutex); z return NULL; z} 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 …...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.