Most simple basic of internet programming based on two different machines sharing the same local net
This blog is just shown for the most simple basic of internet programming based on two different machines sharing the same local net.While the client sends data to server, server just sends back data with timestamps
Server
(1)Parameter configure
import socket
from time import ctime
Host="" # host name,default to server machine's IP
Port=1300
addr=(Host,Port)
Bufsize=1024 # The maximum amount of data to be received at once is specified by bufsize
(2)Create server
Server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
AF-->address family,such as AF_LOCAL,AF_INET;SOCK_STREAM specifies using TCP,while SOCK_DGRAM specifies UDP.
Server.bind(addr)
Server.listen(5)
while True:
print('...Waiting to be connected...')
client,address=Server.accept() # default to be block mode,that is blocking while waiting. input() also has a flavor of block
while True:
data=client.recv(Bufsize)
if not data:
break
print(data)
client.send(bytes(ctime(),'utf8')+data)
client.close()
Server.close()
** In a summary**, server is just created as a socket object through socket.socket(),and then bind() this socket object with the machine, and then listen(), following that, going into
Client
Parameter config
import socket
Host='192.168.1.7'
Host number can be checked out by this: On the server machine,cmd-->ipconfig/all--->IPv4 address:192.168.1.7
Port=1300 # The same with server's port
Address=(Host,Port)
BufSize=1024
Create client
Client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
Client.connect(Address)
While True:
data=input('>>>')
if not data:
break
Client.send(bytes(data,'utf8'))
data=Client.recv(BufSize)
if not data:
break
print(data.decode('utf8'))
Client.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix