socket client 发起连接。
流程为:
创建接口
发起连接
创建接口参数同socket server相同
发起连接的函数为socket.connect(ip,port)
这个地方的ip与port为socket server端的ip和监听port。
代码示例:
# -*- coding: utf-8 -*-
'''
This is a testing program
the program is used to test socket client
'''
import socket
import sys
def start_tcp_client(ip, port):
#server port and ip
server_ip = ip
servr_port = port
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
tcp_client.connect((server_ip, server_port))
except socket.error:
print 'fail to setup socket connection'
tcp_client.close()