Socket address structure in <sys/socket.h>: - PowerPoint PPT Presentation

About This Presentation
Title:

Socket address structure in <sys/socket.h>:

Description:

Title: Workshop I Author: csguest1 Last modified by: CS Created Date: 9/15/2000 3:52:51 AM Document presentation format: On-screen Show Company: City University of ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 29
Provided by: csguest1
Category:

less

Transcript and Presenter's Notes

Title: Socket address structure in <sys/socket.h>:


1
Socket Programming
  • Socket address structure in ltsys/socket.hgt
  • struct sockaddr
  • u_short sa_family / address family AF_xxx
    value /
  • char sa_data14 / up to 14 bytes of
    protocol- /
  • / specific address /

2
Socket Programming
  • Socket address structure in ltnetinet/in.hgt
  • (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_zero8 / unused /

4
Socket Programming
  • socket specify the type of communication
    protocol desired (TCP, UDP etc.)
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • 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 ltsys/types.hgt
  • include ltsys/socket.hgt
  • int bind(int sockfd, struct sockaddr myaddr,
    int addrlen)
  • sockfd socket descriptor (return bymyaddr a
    pointer to a protocol-specific address
  • addrlen the size of this address structure
  • The output gt 0 if success otherwise, it fails.

7
Socket Programming
  • connect establish a connection with a server
  • include ltsys/types.hgt
  • include ltsys/socket.hgt
  • int connect (int sockfd, struct sockaddr
    servaddr, int addrlen)
  • same as the system call bind.
  • The output gt 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 ltsys/types.hgt
  • include ltsys/socket.hgt
  • 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 gt 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 ltsys/types.hgt
  • include ltsys/socket.hgt
  • 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 ltsys/types.hgt
  • include ltsys/socket.hgt
  • 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 ltsys/types.hgt
  • include ltsys/socket.hgt
  • 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 ltsys/types.hgt
  • include ltnetinet/in.hgt
  • 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 ltsys/socket.hgt
  • include ltnetinet/in.hgt
  • include ltarpa/inet.hgt
  • unsigned long inet_addr(char ptr)

14
Socket Programming
Connection-oriented Protocol
Server
Client
15
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))
    lt 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)) lt 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)) lt 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))
    lt 0)
  • err_sys(client can't open stream socket)
  • if (connect(sockfd, (struct sockaddr )
    serv_addr, sizeof(serv_addr)) lt 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))
    lt 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)) lt 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))
    lt 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)) lt 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)
  • define _REENTRANT
  • include ltstdio.hgt
  • include ltthread.hgt
  • define NUM_THREADS 12
  • void change_global_data(void ) / for
    thr_create() /
  • main(int argc,char argv)
  • int i0
  • for (i0 ilt 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)

Use online help to find out the information about
thread programming. Try man mutex man
thr_create man thr_join ...
Write a Comment
User Comments (0)
About PowerShow.com