0
그래서 여기에 문제가 있습니다. 클라이언트에서받은 기본 자바 서버가 있습니다. 클라이언트는 파이썬으로 작성되었습니다. 서버에서 연결을 확인하는 메시지를 보내는 것처럼 첫 번째 연결을 만들 수 있습니다. 하지만 클라이언트가 서버를 보내길 원할 때는 아무 것도하지 않습니다. 클라이언트가 전송하지 않거나 서버가 수신하지 않는 문제가 있는지 확실하지 않습니다.파이썬 클라이언트로 자바 서버 연결하기
다음은 서버의 코드입니다 :
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)
자바 API에'RESTful'을 사용하고 파이썬 클라이언트에'requests'를 사용하는 것이 간단한 방법이 아니겠습니까? –
잘 모르겠습니다. 그러나 내가이 코드를 잘못 읽은 이유를 설명 할 수 있습니까? – kessi
'\ n'이 어떻게 든 파괴되었다고 생각합니다. 그러나 저는 파이썬에 익숙하지 않습니다. – Fildor