import socket import time #IPV4,TCP协议 sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #绑定ip和端口,bind接受的是一个元组 sock.bind((‘172.23.23.89‘,54378)) #设置监听,其值阻塞队列长度,一共可以有5+1个客户端和服务器连接 sock.listen(5) a=[1,2,3,4] while True: # 将发送数据转化为String s=str(a) # 等待客户请求 connection,address = sock.accept() # 打印客户端地址 print("client ip is:",address) # 接收数据,并存入buf buf = connection.recv(40960) print(buf.decode(‘utf-8‘)) # 发送数据 connection.send(bytes(s, encoding="utf-8")) # 关闭连接 connection.close() time.sleep(1) # 关闭服务器 sock.close()
clc clear tcpclient = tcpip(‘172.23.23.89‘, 54378, ‘Timeout‘, 60,‘OutputBufferSize‘,10240,‘InputBufferSize‘,10240);%连接这个ip和这个端口的UDP服务器 %t.BytesAvailableFcnMode=‘byte‘ %while(1) %a=1:10 fopen(tcpclient); fwrite(tcpclient,‘please sent‘);%发送一段数据给tcp服务器。服务器好知道matlab的ip和端口 while(1) %轮询,直到有数据了再fread nBytes = get(tcpclient,‘BytesAvailable‘); if nBytes>0 break; end end receive = fread(tcpclient,nBytes);%读取tcp服务器传来的数据 %fread(t) %关闭连接 fclose(tcpclient); data=str2num(char(receive(2:end-1)‘)); %将ASCII码转换为str,再将str转换为数组 disp(data) %end delete(tcpclient);
原文地址:https://www.cnblogs.com/xnlcc/p/10067731.html
时间: 2024-11-09 00:57:25