本节内容

  1. Socket介绍
  2. Socket参数介绍
  3. 基本Socket实例
  4. Socket实现多连接处理
  5. 通过Socket实现简单SSH
  6. 通过Socket实现文件传送
  7. 作业:开发一个支持多用户在线的FTP程序

1. Socket介绍

概念

Anetwork socketis an endpoint of a connection across a. Today,most communication between computers is based on the; therefore most network sockets areInternet sockets. More precisely,a socket is a(abstract reference) that a local program can pass to the networking(API) to use the connection,for example "send this data on this socket".

For example,to send "Hello,world!" viato port 80 of the host with address 1.2.3.4,one might get a socket,connect it to the remote host,send the string,then close the socket.

实现一个socket至少要分以下几步,(伪代码)

Asocket APIis an(API),usually provided by the,that allows application programs to control and use network sockets. Internet socket APIs are usually based on thestandard. In the Berkeley sockets standard,sockets are a form of(afilehandle),due to thethat "everything is a file",and the analogies between sockets and files: you can read,write,open,and close both.  

Asocket addressis the combination of anand a,much like one end of a telephone connection is the combination of aand a particular. Sockets need not have an address (for example for only sending data),but if a programbindsa socket to an address,the socket can be used to receive data sent to that address. Based on this address,internet sockets deliver incoming data packets to the appropriate applicationor.

Socket Families(地址簇)

These constants represent the address (and protocol) families,used for the first argument to. If theconstant is not defined then this protocol is unsupported. More constants may be available depending on the system.

Socket Types

These constants represent the socket types,used for the second argument to. More constants may be available depending on the system. (Onlyandappear to be generally useful.)

2. Socket 参数介绍

family=AF_INET,type=SOCK_STREAM,proto=0,fileno=None

Create a new socket using the given address family,socket type and protocol number. The address family should be(the default),,,or. The socket type should be(the default),,or perhaps one of the otherconstants. The protocol number is usually zero and may be omitted or in the case where the address family isthe protocol should be one ofor. Iffilenois specified,the other arguments are ignored,causing the socket with the specified file descriptor to return. Unlike,filenowill return the same socket and not a duplicate. This may help close a detached socket using.

familytypeproto

Build a pair of connected socket objects using the given address family,socket type,and protocol number. Address family,and protocol number are as for thefunction above. The default family isif defined on the platform; otherwise,the default is.

addresstimeoutsource_address

Connect to a TCP service listening on the Internetaddress(a 2-tuple),and return the socket object. This is a higher-level function than: ifhostis a non-numeric hostname,it will try to resolve it for bothand,and then try to connect to all possible addresses in turn until a connection succeeds. This makes it easy to write clients that are compatible to both IPv4 and IPv6.

Passing the optionaltimeoutparameter will set the timeout on the socket instance before attempting to connect. If notimeoutis supplied,the global default timeout setting returned byis used.

If supplied,source_addressmust be a 2-tuplefor the socket to bind to as its source address before connecting. If host or port are ‘’ or 0 respectively the OS default behavior will be used.

host,port,family=0,type=0,flags=0

sk.bind(address)

  s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。

sk.listen(backlog)

  开始监听传入连接。backlog指定在拒绝连接之前,可以挂起的最大连接数量。

backlog等于5,表示内核已经接到了连接请求,但服务器还没有调用accept进行处理的连接个数最大为5 这个值不能无限大,因为要在内核中维护连接队列

sk.setblocking(bool)

  是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。

sk.accept()

  接受连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。

  接收TCP 客户的连接(阻塞式)等待连接的到来

sk.connect(address)

  连接到address处的套接字。一般,address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。

sk.connect_ex(address)

  同上,只不过会有返回值,连接成功时返回 0 ,连接失败时候返回编码,例如:10061

sk.close()

  关闭套接字

sk.recv(bufsize[,flag])

  接受套接字的数据。数据以字符串形式返回,bufsize指定最多可以接收的数量。flag提供有关消息的其他信息,通常可以忽略。

sk.recvfrom(bufsize[.flag])

  与recv()类似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。

sk.send(string[,flag])

  将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容全部发送。

sk.sendall(string[,flag])

  将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。

内部通过递归调用send,将所有内容发送出去。

sk.sendto(string[,flag],address)

  将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。

sk.settimeout(timeout)

  设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如 client 连接最多等待5s )

sk.getpeername()

  返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。

sk.getsockname()

  返回套接字自己的地址。通常是一个元组(ipaddr,port)

sk.fileno()

  套接字的文件描述符

file,offset=0,count=None

发送文件 ,但目前多数情况下并无什么卵用。

3. 基本Socket实例

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。