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

网友投稿 394 2022-11-06


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

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 serNabibWvYlEver = 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 = 2580

BUFSIZ = 2048

ADDR = (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 inforhttp://mation:%s' % returnData.decode('utf-8'))

clientSocket.close()

print("结束")


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:api管理工具(API管理工具 mock)
下一篇:愿你习惯曲终人散-纪念博客200万PV
相关文章

 发表评论

暂时没有评论,来抢沙发吧~