python實現tcp通訊

2021-09-28 00:26:55 字數 873 閱讀 4062

tcp協議服務端

# coding=utf-8

import socket

s=socket.socket(socket.af_inet,socket.sock_stream)

# type: socket

s.bind((''

,8888))

s.listen(1)

print

('伺服器啟動...'

)conn,address=s.accept(

)print

(address)

data=conn.recv(

1024

)print

('從客戶端接收訊息::'

.format

(data.decode())

)conn.send(

'123'

.encode())

conn.close(

)s.close(

)

tcp協議客戶端

# coding=utf-8

import socket

s=socket.socket(socket.af_inet,socket.sock_stream)

s.connect(

('127.0.0.1'

,8888))

s.send(b'hello'

)data=s.recv(

1024

)print

('從伺服器端接收訊息:'

.format

(data.decode())

)s.close(

)

注意要首先啟動服務端的**在啟動客戶端**

TCP通訊C 實現

tcp客戶端與伺服器端通訊模型 伺服器端實現 include include pragma comment lib,ws2 32 using namespace std void main if lobyte wsadata.wversion 1 hibyte wsadata.wversion 1 ...

socket實現TCP通訊

tcp是一種可靠的,面向連線的協議。在socket中,建立tcp連線的過程大致如下 伺服器端 1.初始化套接字 2.建立伺服器socket 3.將本機位址與伺服器socket繫結在一起 4.伺服器開始監聽 5.伺服器確認請求 確認之前時一種阻塞的狀態 accept 函式生成乙個新的socket。6....

C 實現TCP通訊

服務端 include initsock.h include include include include using namespace std cinitsock initsock 初始化winsock庫 sockaddr in serveraddr sockaddr in remoteadd...