basic socket programming l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Basic Socket Programming PowerPoint Presentation
Download Presentation
Basic Socket Programming

Loading in 2 Seconds...

play fullscreen
1 / 16

Basic Socket Programming - PowerPoint PPT Presentation


  • 333 Views
  • Uploaded on

Basic Socket Programming. TCP/IP overview. TCP interface Reference: UNIX Network Programming, by Richard Stevens. UNIX man page. Overview of TCP/IP protocols Application layer(ssh,http,etc) Transport layer (TCP, UDP) Network layer (IPv4, IPv6) Host to Network layer (Ethernet).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Basic Socket Programming' - Faraday


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
basic socket programming
Basic Socket Programming
  • TCP/IP overview.
  • TCP interface
  • Reference:
    • UNIX Network Programming, by Richard Stevens.
    • UNIX man page.
slide2
Overview of TCP/IP protocols
    • Application layer(ssh,http,etc)
    • Transport layer (TCP, UDP)
    • Network layer (IPv4, IPv6)
    • Host to Network layer (Ethernet)
slide3
Some concepts:
    • Entity (process/hardware/system calls)
    • Protocol: How peer entities interact with each other.
    • Service interface: How upper layer entities interact with lower layer entities.
slide4
Socket Programming: the use of TCP and UDP.
  • TCP: Transmission control protocol.
      • connection-oriented, reliable, full duplex, byte stream service
      • Interface: socket, bind, listen, accept, connect, read, write, close.
slide5
An analogy:

Socket: telephone

Bind: assign telephone number to a telephone

Listen: turn on the ringer so that you can hear the phone call

Connect: dial a phone number

Accept: answer the phone

Read/write: talking

Close: ???

slide6
To send:
    • Socket, connect write
  • To receive:
    • Socket, bind, listen, accept read
  • TCP endpoint:
    • IP address + port number
slide7
Basic TCP sockets.

#include <sys/socket.h>

int socket(int family, int type, int protocol);

      • Family: AF_INET (PF_INET).
      • Type: SOCK_STREAM (TCP) SOCK_DGRAM (UDP)
      • Protocol: = 0
      • Return descriptor, -1 on error.
slide8
Connect:

#include <sys/socket.h>

int connect(int sockfd, const struct sockaddr *servaddr,

socklen_t addrlen);

      • Servaddr: socket address structure (ip address and port)
slide9
Socket Address structure:

struct in_addr {

in_addr_t s_addr;

}

struct sockaddr_in {

uint8_t sin_len;

sa_family_t sin_family;

in_port_t sin_port;

struct in_addr sin_addr;

char sin_zero[8];

}

      • Always use sockaddr_in type for manipulation and convert it to sockaddr. See example1.c.

struct sockaddr {

uint8_t sa_len;

sa_family_t sa_family;

char sa_data[14];

}

slide10
Bind
    • Client does not have to bind, system assigns a dynamic port number.

#include <sys/socket.h>

int bind(int sockfd, const struct sockaddr &myaddr, socklen_t addlen);

slide11
Myaddr:

(address, port) = (INADDR_ANY, 0)

system assigns addr and port.

= (INADDR_ANY, !0)

system selects addr, user selects port

=(Local IP address, 0)

user selects addr, system selects port

=(Local IP address,!0)

user selects both addr and port

See example2.c

slide12
Listen
    • Convert a socket into a passive socket

#include <sys/socket.h>

int listen(int sockfd, int backlog)

      • Backlog: number of connections that the kernel should queue for the socket.
slide13
Accept:
      • Blocking by default

#include <sys/socket.h>

int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);

      • Return client’s address in cliaddr
      • See example2.c
slide14
What happen when we run example2.c as server on diablo and example1.c as client on linprog?
    • Sockaddr_in revisit
      • sin_port and sin_addr must be in network byte order.
        • Check example3.c, what is the difference between diablo and quake?
slide15
Some useful functions to convert the byte orders

#include <netinet/in.h>

uint16_t htons(uint16_t host16bitvalue);

uint32_t htonl(uint32_t host32bitvalue);

uint16_t ntohs(uint16_t net16bitvalue);

Uint32_t ntohl(uint32_t net32bitvalue);

    • See example3.c
slide16
Some byte manipulation functions:

#include <strings.h>

Void *memset(void *dst, int c, size_t len);

Void *memcpy(void *dst, void *src, size_t nbytes);

Void *memcmp(const void *ptr1, const void *ptr2, size_t nbytes);

  • Address conversion functions
      • inet_aton/inet_addr/inet_ntoa