参考: http://www.cnblogs.com/alex3714/articles/5830365.html

Socket

  • network socket is an endpoint of a connection across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. More precisely, a socket is a handle (abstract reference) that a local program can pass to the networking application programming interface (API) to use the connection, for example "send this data on this socket".
  • 实现步骤 (伪代码:)
  • Socket socket = getSocket(type = "TCP")  #设定好协议类型
    connect(socket, address = "1.2.3.4", port = "80") #连接远程机器
    send(socket, "Hello, world!") #发送消息
    close(socket) #关闭连接

     

 

简单Socket 实例

1. 客户端

#!/usr/bin/python
# -*- coding: utf-8 -*-

# 客户端
import socket
client = socket.socket()  # 声明socket类型, 同时生成socket链接对象
client.connect(('localhost', 6969))

client.send(b"Hellow World")  # 只能接收字节类型, 所以要加b转一下

data = client.recv(1024)  # 收1024个字节
print("recv: ", data)


client.close()
客户端

相关文章: