Archive for the [Programming] Category

Network Socket (一)

Posted in [Linux], [Programming] on 十二月 26, 2009 by maximliu
socket这个概念学过计算机的都知道,但是到底什么是socket呢? 这个抽象的概念在linux操作系统下面是怎么实现的呢?这篇个系列的短文尝试着
以简单的语言和实例来解释这个重要的概念。
  • 什么是套接字?
    • socket is a standard way to perform network communication through the OS;
    • it is a programmer’s abstraction to take care of details of OSI comm;
    • it sends and receives data;
  • 有几种不同的套接字?
    • steam socket – connection-oriented, reliable (on TCP)
    • datagram socket – connectionless, unreliable (on UDP)
  • 有那些基本的套接字应用函数?(Linux)
    • socket (int domain, int type, int prot)
      • create a socket
      • return a file descriptor
      • domain :  spec. protocol family of socket fd
      • type : spec. types of socket, e.g. datagram, stream data
      • prot : almost always be 0. Selects protocol from family
    • connect (int fd, struct sockadd *remothost, scoklen_t addr_length)
      • connect a socket to to a remote host
    • bind (int fd, struct sockaddr *localadd, socklen_t addr_length)
      • binds a socket to local address
    • listen (int fd, int backlog_queue_size)
      • listens for connection and queue requests into backlog
    • accept (int fd, sockadd *remothost, socklen_t *addr_length)
      • accepts an incoming request
    • send (int fd, void *buffer, size_t n, int flags)
      • sends n data from buffer to socket fd
    • recv (int fd, void *buffer, size_t n, int flags)
      • receive n bytes from socket fd into buffer
Advertisements