用python做socket程式設計,從實現上來講,是非常簡單的。下面是從他的幫助文件裡面取出來的兩段**,顯示了服務端和客戶端。
服務端:
# echo server program
import socket
host =
'127.0.0.1' # symbolic name meaning the local host
port = 50007 # arbitrary non- privileged port
s = socket. socket( socket. af_inet, socket. sock_stream)
s. bind(
( host, port)
)s. listen( 1)
conn, addr = s. accept(
'connected by'
, addr
while 1:
data = conn. recv( 1024)
ifnot data:
break
conn. send( data)
conn. close(
)
客戶端:
# echo client program
import socket
host =
'127.0.0.1' # the remote host
port = 50007 # the same port as used by the server
s = socket. socket( socket. af_inet, socket. sock_stream)
s. connect(
( host, port)
)s. send(
'hello, world'
)data = s. recv( 1024)
s. close(
'received'
, repr( data)
很簡單的**,在服務端啟動程式後,進入訊息等待迴圈。當客戶端連線後並傳送訊息,而在服務端收到訊息後,傳送訊息,並在客戶端及服務端關閉socket.
linux網路程式設計1 最簡單的socket程式設計
下面是socket程式設計的伺服器端 先看乙個圖,1 客戶端複製 1 include 2 include 3 include 4 include 56 include 7 include 8 include 910 include 錯誤 1112 define maxline 80 13 defin...
簡單的socket程式設計
服務端tcp server.c include include include include include include include define port 8888 埠位址 define backlog 2 監聽佇列長度 int main int argc,char argv memse...
簡單Socket程式設計
01server端 02 03 include 04 include 05 pragma comment lib,ws2 32.lib 06voidmain 07 18else 19 24socket sersocket socket af inet,sock stream,0 建立了可識別套接字 ...