Socket Programming Review - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Socket Programming Review

Description:

127.0.0.1 is address of localhost or loop-back address. Fall 2000. Datacom 1. 4 ... 1.0.0.127 is address of localhost or loop-back address ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 10
Provided by: larry306
Category:

less

Transcript and Presenter's Notes

Title: Socket Programming Review


1
Socket Programming Review
  • Examples
  • Client and Server-Diagnostics
  • UDP versus TCP
  • Echo

2
Socket Interface-an Application Programming
Interface (API)
  • Create the Socket for a specific transport layer
    protocol
  • ssocket(PF_INET,SOCK_STREAM,0) or
  • ssocket(PF_INET,SOCK_DGRAM,0)
  • returned value is a handle e.g. 3,4 etc.
  • PF_INET
  • SOCK_STREAM means TCP 1 client/server
  • SOCK_DGRAM means UDP multi-clients/server

3
Data structures hostent hp
  • hp-gth_name (character) contains host name
  • hp-gth_length number of bytes in IP address
  • hp-gth_addr4 contains IP address
  • byte1byte2byte3byte4
  • A.B.C.D 0ltA,B,C,Dlt255
  • 127.0.0.1 is address of localhost or loop-back
    address

4
Data structures sockaddr_in sin
  • sin.sin_family AF_INET
  • sin.sin_addr.s_addr IP address in reverse byte
    order DCBA
  • 1.0.0.127 is address of localhost or loop-back
    address
  • sin.sin_port Server port number in some strange
    format

5
Socket Interface Server (a)
  • Creating socket
  • ssocket(PF_INET,SOCK_STREAM,0)
  • Binding a Socket to a Port Numberthis is the
    local address of this application
  • bind (s, (struct sockaddr)sin, sizeof(sin))
  • The entry for IP address in sin.sin_addr.s_addr
    will be the clients IP addressit cannot be
    specified until connection is accepted.

6
Socket Interface Server (b)
  • Defining the queue length for the receiving
    socket
  • int listen(s, MAX_PENDING)
  • Specify max number of calls waiting
  • Passive listening
  • new_s accept (s, (struct sockaddr)sin,len)
  • Senders IP address is returned as
    sin.sin_addr.s_addr
  • New socket is created and its number is returned

7
Socket Interface Server (c)
  • Receiving a message
  • recv(new_s, buf, sizeof(buf),0)
  • Receive message, store in buf
  • Closing new socket
  • close(new_s)

8
Socket Interface Client (a)
  • Create a socket
  • ssocket(PF_INET,SOCK_STREAM,0)
  • Connecting to a server-active open
  • connect (s, (struct sockaddr) sin, sizeof(sin))
  • Servers IP address is stored in
    sin.sin_addr.s_addr
  • Servers Port is stored in sin.sin_port
  • Returns accept or not accepted

9
Socket Interface Client (b)
  • Sending message to a server
  • send(s, buf, len, 0)
  • buf is a pointer to the message
  • len is length of message string
Write a Comment
User Comments (0)
About PowerShow.com