Naming a Socket - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Naming a Socket

Description:

Naming a Socket. Recall network sketch. Open a socket. Name the socket ... int ret_val = bind(sock, (struct sock_addr *) &sa, sizeof(sa) ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 15
Provided by: phi140
Category:
Tags: naming | sizeof | socket

less

Transcript and Presenter's Notes

Title: Naming a Socket


1
Naming a Socket
  • Recall network sketch
  • Open a socket.
  • Name the socket
  • Associate with another socket.
  • Send messages between sockets.
  • Close socket.
  • Requires declaring and filling in socket address
    variable
  • struct sockaddr_in

2
  • struct sockaddr_in sa
  • sa.sin_family AF_INET
  • sa.sin_port htons (3000)
  • sa.sin_addr.s_addr inet_addr (32.33.14.55 )

3
Completing the Naming with bind()
  • Requires sockaddr_in and unbound socket.
  • SOCKET sock
  • sock socket(AF_INET, SOCK_STREAM, 0)
  • int ret_val bind(sock, (struct sock_addr )
    sa, sizeof(sa))
  • Now sock is bound to the IP Address, Port Number
    and protocol declared within the sockaddr_in
    structure.
  • In client/server computing, the client does not
    have to bind its socket.

4
Step3 Associate with another socket.
  • Client and sever set up the association in
    different ways.
  • Client
  • Fill in a struct sockaddr_in structure with IP
    Address and Port of the server.
  • Call connect() function with open socket (bound
    or unbound) and address of server.
  • Once return from connect() function, the TCP
    connection has been established.
  • Can send/receive messages.

5
Step3 Associate with another socket.
  • Server
  • Name a socket and bind it to IP address and port
    number of service.
  • Call the listen() function to listen for
    connection requests.
  • Once detect request, call accept() function to
    complete the connection.

6
Client Actions
  • SOCKET sock
  • struct sockaddr_in sa // Fill in with local
    IP, port
  • sock socket(AF_INET, SOCK_STREAM, 0)
  • Success Returns socket descriptor.
  • Fail Returns INVALID_SOCKET
  • int ret_val bind(sock, (struct sock_addr )
    sa, sizeof(sa))
  • Success Returns 0
  • Fail Returns SOCKET_ERROR.

7
Prepare to Connect
  • Fill in sockaddr_in structure with IP address and
    port number of server.
  • sa.sin_family AF_INET
  • sa.sin_port htons (Server_Port)
  • sa.sin_addr.s_addr inet_addr(xxx.xxx.xx.XX)
  • int ret_val connect(sock, (struct sock_addr )
    sa, sizeof(sa))
  • Success Returns 0
  • Fail Returns SOCKET_ERROR

8
  • Connect will fail if the server is not in the
    listening state and accepts the connection.
  • On success, the client has established a virtual
    TCP connection on which it can send and receive
    data reliably.

9
Server
  • 1) Declare two sockets and one struct
    sockaddr_in
  • SOCKET l_sock, a_sock
  • struct sockaddr_in sa
  • 2) Name one of the sockets (fill in sa with local
    information and call bind with one. Assume
    l_sock)
  • 3) ret_val listen(l_sock, 1) // 1 is wait
    queue length
  • Success Returns 0
  • Fail Returns SOCKET_ERROR

10
Server
  • a_sock accept( l_sock, (struct sock_addr )
    sa, sizeof (sa))
  • Success Returns valid socket (a_sock in this
    case)
  • Fail Returns INVALID_SOCKET
  • sa filled in by OS with name of connected socket.
  • Use a_sock to send and receive messages.
  • l_sock still listening for more connection
    requests.

11
Sending/Receiving Messages
  • Must use a connected socket.
  • char buf1024
  • ret_val send(a_sock, buf, 1024, 0)
  • on success returns the actual number of bytes
    sent.
  • if fails returns SOCKET_ERROR.
  • Note May not send all data requested.

12
Receive
  • Must use a connected socket.
  • char buf1024
  • int ret_val recv(a_sock, buf, 1024, 0)
  • Success Returns number of bytes actually
    received
  • Fail Returns SOCKET_ERROR
  • NOTE May not return all requested data.

13
  • int done 0
  • int bytes_received 0
  • while (!done)
  • bytes_received recv(a_sock, buf
    bytes_received,
  • 1024 - bytes_received, 0)
  • if (bytes_received 1024)
  • done 1

14
Synchronous Communication Calls
  • recv() is a synchronous communications call.
  • Application will block until there is some data
    available on the socket.
Write a Comment
User Comments (0)
About PowerShow.com