当前位置:网站首页>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();
}
}
边栏推荐
- Accounting regulations and professional ethics [1]
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
- Cost accounting [22]
- [analysis of teacher Gao's software needs] collection of exercises and answers for level 20 cloud class
- C语言数组的概念
- nodejs爬虫
- China's PCB connector market trend report, technological innovation and market forecast
- D - Function(HDU - 6546)女生赛
- Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
- Find 3-friendly Integers
猜你喜欢
C语言是低级和高级的分水岭
信息安全-威胁检测引擎-常见规则引擎底座性能比较
Essai de pénétration (1) - - outils nécessaires, navigation
Penetration test (7) -- vulnerability scanning tool Nessus
信息安全-安全编排自动化与响应 (SOAR) 技术解析
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Nodejs+vue网上鲜花店销售信息系统express+mysql
C语言必背代码大全
Information security - threat detection engine - common rule engine base performance comparison
滲透測試 ( 1 ) --- 必備 工具、導航
随机推荐
Nodejs+vue online fresh flower shop sales information system express+mysql
动态规划前路径问题
China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
Cost accounting [20]
Cost accounting [22]
Perinatal Software Industry Research Report - market status analysis and development prospect forecast
7-1 懂的都懂 (20 分)
Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
Information security - security professional name | CVE | rce | POC | Vul | 0day
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
[exercise-6] (UVA 725) division = = violence
Opencv learning log 30 -- histogram equalization
[exercise-9] Zombie's Treasury test
VS2019初步使用
HDU - 6024 Building Shops(女生赛)
C语言是低级和高级的分水岭
0-1背包問題(一)
Research Report on shell heater industry - market status analysis and development prospect forecast
frida hook so层、protobuf 数据解析
0-1背包问题(一)