0
所以这里的东西,我有发回什么都从它接收到的客户一个基本的Java服务器。客户端是用python编写的。我能够在服务器向客户端发送确认连接的消息时进行第一次连接。但是当我想让客户端发送服务器时,什么都不做。我不确定是否客户端的问题没有发送或服务器没有收到。连接Java服务器与Python客户端
下面是该服务器的代码:
int portNumber = Integer.parseInt(args[0]);
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter outs =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
String inputLine, outputLine;
outputLine = "Hello socket, I'm server";
outs.println(outputLine);
outs.println("I' connected");
while ((inputLine = in.readLine()) != null) {
outputLine = inputLine;
outs.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
} }
和这里的客户:
import socket
HOST = "localhost"
PORT = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
print (socket.getaddrinfo(HOST,PORT))
buffer_size = 100
while True :
data = sock.recv(buffer_size)
print ('you recieved :' , data)
test = input('send here\n')
sock.sendall(bytes(test, 'utf-8'))
print ('you sent : ' , test)
将使用'RESTful'的Java API和使用Python客户端'requests'不会是简单的方法? –
我不知道。但是,是否有可能解释我的代码出错了? – kessi
我怀疑'\ n'会以某种方式被销毁。但我对Python不熟悉。 – Fildor