socket

Posted yaohu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket相关的知识,希望对你有一定的参考价值。

# coding:utf-8

from socket import *

HOST = ‘127.0.0.1‘
PORT = 8088
BUFSIZ =1024
ADDR = (HOST,PORT)

tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data1 = input(‘>>‘)
#data = str(data)
if not data1:
break
tcpCliSock.send(data1.encode())
data1 = tcpCliSock.recv(BUFSIZ)
if not data1:
break
print(data1.decode(‘utf-8‘))
tcpCliSock.close()
===============================================
# coding:utf-8

from socket import *
from time import ctime

HOST = ‘127.0.0.1‘
PORT = 8088
BUFSIZ = 1024
ADDR = (HOST,PORT)

tcpSerSock = socket(AF_INET,SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

while True:
print(‘waiting for connection...‘)
tcpCliSock, addr = tcpSerSock.accept()
print(‘...connnecting from:‘, addr)

while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
#tcpCliSock.send(‘[%s] %s‘ %(bytes(ctime(),‘utf-8‘),data))
tcpCliSock.send((‘[%s] %s‘ % (ctime(), data)).encode())
tcpCliSock.close()
tcpSerSock.close()

以上是关于socket的主要内容,如果未能解决你的问题,请参考以下文章

socket通信

java实例检查端口是否被占用

网络通信与Socket

分布式理论,架构设计Socket和IO模型

分布式理论,架构设计Socket和IO模型

android做一个简单上传文件的功能,socket连接问题