当前位置:网站首页>Socket communication
Socket communication
2022-07-06 16:05:00 【Handsome black cat Sheriff】
python The sender
from socket import *
serverName = '*.*.*.*'
serverPort = *
clientSocket =socket(AF_INET,SOCK_STREAM) # establish socket
clientSocket.connect((serverName,serverPort)) # Connect
sentence = input('Input->:') # Enter the data to be transmitted
clientSocket.send(sentence.encode("utf8")) # Send... To the receiving end
clientSocket.close() # Close socket bytes
python The receiver
import socket
# establish socket
tcp_server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Local information
address = ('',702)
# binding
tcp_server_socket.bind(address)
tcp_server_socket.listen(128)
print(' Start server listening ')
while True:
# Waiting for a new client connection
client_socket, clientAddr = tcp_server_socket.accept()
while True:
# Receive the data sent by the other party
recv_data = client_socket.recv(1024) # receive 1024 Bytes
if recv_data:
print(' The received data is :', recv_data.decode('utf8'))
else:
break
client_socket.close()
tcp_server_socket.close()
java The receiver
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 {
// For the sake of simplicity , All the abnormal information is thrown out
int port = 702;
// Define a ServerSocket Listen on the port
ServerSocket server = new ServerSocket(port);
System.out.println("start");
//server Try to receive other Socket Connection request for ,server Of accept The method is blocked
Socket socket = server.accept();
// After establishing a connection with the client , We can get it socket Of InputStream, And read the information sent by the client .
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();
}
}
边栏推荐
猜你喜欢
C language must memorize code Encyclopedia
Nodejs+vue online fresh flower shop sales information system express+mysql
7-1 understand everything (20 points)
入门C语言基础问答
[exercise-7] crossover answers
【高老师UML软件建模基础】20级云班课习题答案合集
差分(一维,二维,三维) 蓝桥杯三体攻击
渗透测试 ( 4 ) --- Meterpreter 命令详解
B - 代码派对(女生赛)
渗透测试 ( 1 ) --- 必备 工具、导航
随机推荐
Matlab comprehensive exercise: application in signal and system
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
socket通讯
对iptables进行常规操作
快速转 TypeScript 指南
7-1 懂的都懂 (20 分)
Alice and Bob (2021 Niuke summer multi school training camp 1)
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
Research Report on shell heater industry - market status analysis and development prospect forecast
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
China potato slicer market trend report, technical dynamic innovation and market forecast
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
Find 3-friendly Integers
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
F - birthday cake (Shandong race)
Information security - security professional name | CVE | rce | POC | Vul | 0day
Shell Scripting
If you want to apply for a programmer, your resume should be written like this [essence summary]
Common configuration files of SSM framework