当前位置:网站首页>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();
}
}
边栏推荐
- Analysis of protobuf format of real-time barrage and historical barrage at station B
- mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
- C basic grammar
- 洛谷P1102 A-B数对(二分,map,双指针)
- The most complete programming language online API document
- 【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
- [exercise-5] (UVA 839) not so mobile (balance)
- Truck History
- 【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
猜你喜欢
Information security - threat detection - detailed design of NAT log access threat detection platform
快速转 TypeScript 指南
[exercise-5] (UVA 839) not so mobile (balance)
C语言是低级和高级的分水岭
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
洛谷P1102 A-B数对(二分,map,双指针)
Record of force deduction and question brushing
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
PySide6 信号、槽
随机推荐
[exercise-7] crossover answers
Opencv learning log 31 -- background difference
E. Breaking the Wall
[exercise-2] (UVA 712) s-trees
最全编程语言在线 API 文档
渗透测试 ( 1 ) --- 必备 工具、导航
Understand what is a programming language in a popular way
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
Ball Dropping
[exercise 4-1] cake distribution
Gartner:关于零信任网络访问最佳实践的五个建议
Penetration test (3) -- Metasploit framework (MSF)
想应聘程序员,您的简历就该这样写【精华总结】
STM32 how to use stlink download program: light LED running light (Library version)
Borg maze (bfs+ minimum spanning tree) (problem solving report)
[exercise-5] (UVA 839) not so mobile (balance)
Information security - Analysis of security orchestration automation and response (soar) technology
Write web games in C language
渗透测试 ( 4 ) --- Meterpreter 命令详解
7-1 understand everything (20 points)