1 / 14

Socket Interfaces

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

astra
Download Presentation

Socket Interfaces

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Interfaces Professor Jinhua Guo CIS527 Fall 2002

  2. 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

  3. 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.

  4. 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

  5. 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

  6. 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); }

  7. 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);

  8. Naming Sockets int bind(sd, name, namelen) int sd; struct sockaddr *name; int namelen;

  9. 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

  10. 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

  11. 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);

  12. 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;

  13. 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

  14. Connection-Oriented Communication Using Stream Sockets

More Related