当前位置:网站首页>socket通讯
socket通讯
2022-07-06 09:27:00 【帅帅气气的黑猫警长】
python发送端
from socket import *
serverName = '*.*.*.*'
serverPort = *
clientSocket =socket(AF_INET,SOCK_STREAM) # 创建socket
clientSocket.connect((serverName,serverPort)) # 连接
sentence = input('Input->:') #输入要传输的数据
clientSocket.send(sentence.encode("utf8")) #向接收端发送
clientSocket.close() #关闭套字节
python接收端
import socket
# 创建socket
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 本地信息
address = ('',702)
# 绑定
tcp_server_socket.bind(address)
tcp_server_socket.listen(128)
print('启动服务器监听')
while True:
# 等待新的客户端连接
client_socket, clientAddr = tcp_server_socket.accept()
while True:
# 接收对方发送过来的数据
recv_data = client_socket.recv(1024) # 接收1024个字节
if recv_data:
print('接收到的数据为:', recv_data.decode('utf8'))
else:
break
client_socket.close()
tcp_server_socket.close()
java接收端
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.ServerSocket;
import java.net.Socket;
public class test {
public static void main(String args[]) throws IOException {
//为了简单起见,所有的异常信息都往外抛
int port = 702;
//定义一个ServerSocket监听在端口上
ServerSocket server = new ServerSocket(port);
System.out.println("start");
//server尝试接收其他Socket的连接请求,server的accept方法是阻塞式的
Socket socket = server.accept();
//跟客户端建立好连接之后,我们就可以获取socket的InputStream,并从中读取客户端发过来的信息了。
Reader reader = new InputStreamReader(socket.getInputStream());
int len;
int k=1;
StringBuilder sb = new StringBuilder();
while (k>0 ) {
char chars[] = new char[60];
len=reader.read(chars);
System.out.println(chars);
System.out.println("-----------------");
}
reader.close();
socket.close();
server.close();
}
}
边栏推荐
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- 差分(一维,二维,三维) 蓝桥杯三体攻击
- 信息安全-威胁检测引擎-常见规则引擎底座性能比较
- SSM框架常用配置文件
- Accounting regulations and professional ethics [1]
- C语言学习笔记
- Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
- C语言数组的概念
- Opencv learning log 33 Gaussian mean filtering
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
猜你喜欢
动态规划前路径问题优化方式
基于web的照片数码冲印网站
Penetration test (8) -- official document of burp Suite Pro
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Information security - threat detection engine - common rule engine base performance comparison
程序员的你,有哪些炫技的代码写法?
TCP的三次握手与四次挥手
Penetration test (7) -- vulnerability scanning tool Nessus
【练习-7】Crossword Answers
随机推荐
The most complete programming language online API document
Accounting regulations and professional ethics [2]
Information security - threat detection engine - common rule engine base performance comparison
Find 3-friendly Integers
Nodejs+vue网上鲜花店销售信息系统express+mysql
Gartner: five suggestions on best practices for zero trust network access
Accounting regulations and professional ethics [3]
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
b站 实时弹幕和历史弹幕 Protobuf 格式解析
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Opencv learning log 33 Gaussian mean filtering
F - Birthday Cake(山东省赛)
信息安全-威胁检测引擎-常见规则引擎底座性能比较
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
【练习-10】 Unread Messages(未读消息)
Determine the Photo Position
China potato slicer market trend report, technical dynamic innovation and market forecast
If you want to apply for a programmer, your resume should be written like this [essence summary]
Opencv learning log 13 corrosion, expansion, opening and closing operations
[exercise -11] 4 values why sum is 0 (and 4 values of 0)