Python连接Java Socket服务端的实现方法

时间:2021-05-20

java服务端单线程示例

import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;/** * Server */public class Server { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(2580); Socket socket = server.accept(); InputStream is = socket.getInputStream(); OutputStream out=null; byte[] bytes = new byte[2048]; int len = 0; String results = null; try { len = is.read(bytes, 0, 2048); results = new String(bytes, 0, len); System.out.println("from Client information:" + results); out=socket.getOutputStream(); out.write("J-S-P_ I am Server".getBytes()); } catch (IOException e) { e.printStackTrace(); } server.close(); System.out.println("结束"); }}

Python客户端单线程示例

from socket import *serverName = '127.0.0.1'serverPort = 2580BUFSIZ = 2048ADDR = (serverName, serverPort)clientSocket = socket(AF_INET, SOCK_STREAM)clientSocket.connect(ADDR)clientSocket.send("J-S-P_ I am Client".encode('utf-8'))returnData = clientSocket.recv(BUFSIZ)print('from Server information:%s' % returnData.decode('utf-8'))clientSocket.close()print("结束")

到此这篇关于Python连接Java Socket服务端的实现方法的文章就介绍到这了,更多相关Python连接Java Socket 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章