socket interfaces
Download
Skip this Video
Download Presentation
Socket Interfaces

Loading in 2 Seconds...

play fullscreen
1 / 14

Socket Interfaces - PowerPoint PPT Presentation


  • 89 Views
  • Uploaded on

Socket Interfaces. Professor Jinhua Guo CIS527 Fall 2002. What Are Sockets. A socket is an endpoint of communication to which a name can be bound. The socket interface provides access to transport protocols

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 ' Socket Interfaces' - astra


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
socket interfaces

Socket Interfaces

Professor Jinhua Guo

CIS527 Fall 2002

what are sockets
What Are Sockets
  • A socket is an endpoint of communication to which a name can be bound.
    • The socket interface provides access to transport protocols
  • Sockets were designed to implement the client-server model for interprocess communication
sockets domain
Sockets Domain
  • UNIX domain sockets are named with UNIX paths. UNIX domain sockets communicate only between processes on a single host.
  • Internet domain communication uses the TCP/IP suite.
socket types
Socket Types
  • Stream sockets enable processes to communicate using TCP.
    • SOCK_STREAM
  • Datagram sockets enable processes to use UDP to communicate.
    • SOCK_DGRAM
    • Raw sockets
    • SOCK_RAW
socket libraries
Socket Libraries
    • The socket interface routines are in a library that must be linked with the application.

- libsocket.so, libsocket.a

  • Compile and Link

- e.g. gcc main.c –o main –lsocket -lnsl

interprocess communication ipc
Interprocess Communication (IPC)
  • Issues
    • Creating sockets
    • Naming (identifying) sockets
    • Sending/receiving data over sockets
  • Mechanism
    • UNIX system calls and library routines

if ((code = syscall()) < 0) {

perror(“syscall”);

exit(1);

}

socket creation
Socket Creation
  • s = socket(domain, type, protocol);

domain: AF_INET, AF_UNIX

type: SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW

protocol: 0

Example:

s = socket(AF_INET, SOCK_STREAM, 0);

naming sockets
Naming Sockets

int bind(sd, name, namelen)

int sd;

struct sockaddr *name;

int namelen;

socket address
Socket Address
  • Several types of socket addresses
  • You will use sockaddr_in, because you use AF_INET
  • sockaddr_in is a C structure with 3 important fields:
    • sin_family
    • sin_addr
    • sin_port
internet address and ports
Internet Address and Ports
  • sin_addr values

- four bytes

e.g. esun1 is 141.215.10.123 (see /etc/hosts)

- if you specify INADDR_ANY for local host address, all host address apply

  • sin_port values
    • 0 – 1024 reserved for system
    • well known ports are import
      • http is port 80
      • telnet is port 23
    • if you specify 0, the system picks a port
data transfer
Data Transfer
  • You can send or receive a message with the normal read and write interfaces:
    • - write(s, buf, sizeof buf);
    • - read(s, buf, sizeof buf);
  • You can also use send and recv:
    • - send(s, buf, sizeof buf, flags);
    • - recv(s, buf, sizeof buf, flags);
other system calls
Other System Calls
  • int connect(sd, name, namelen)
    • - specifies peer with which sd is to be associated
  • int listen(sd, backlog)
    • -specifies maximum backlog of connections a server will allow
  • int accept (sd, addr, addrlen)
    • extracts first connection off queue of pending connections
  • int close (sd)
    • -deletes descriptor from system tables;
slide13

htonl(), htons(), ntohl(), ntohs()

    • convert between network byte order and host byte order
    • Required since some machines are big endian and others are little endian (network order is big endian)
    • big endian
    • address byte
    • 0 0 1 2 3
    • 4 4 5 6 7
    • little endian
    • address byte
    • 0 3 2 1 0
    • 4 7 6 5 4
    • Convert to network order before sending and back to host order after receiving
ad