java python客户端和java服务器之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4185242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Communication between python client and java server
提问by Amandeep Chugh
My aim is to send a message from python socket to java socket. I did look out on the resource mentioned above. However I am struggling to make the Python client talk to Java server. Mostly because (End of line) in python is different from that in java.
我的目标是从 python 套接字向 java 套接字发送消息。我确实留意了上面提到的资源。但是,我正在努力使 Python 客户端与 Java 服务器通信。主要是因为python中的(行尾)与java中的不同。
say i write from python client: message 1: abcd message 2: efgh message 3: q (to quit)
说我从 python 客户端写:消息 1:abcd 消息 2:efgh 消息 3:q(退出)
At java server: i receive message 1:abcdefghq followed by exception because the python client had closed the socket from its end.
在 java 服务器上:我收到消息 1:abcdefghq 后跟异常,因为 python 客户端已经从它的末端关闭了套接字。
Could anybody please suggest a solution for a consistent talk between java and python.
任何人都可以为java和python之间的一致对话提出一个解决方案。
Reference I used: http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
我使用的参考:http: //www.prasannatech.net/2008/07/socket-programming-tutorial.html
Update: I forgot to add, I am working on TCP.
更新:我忘了补充,我正在研究 TCP。
My JAVA code goes like this:(server socket)
我的 JAVA 代码是这样的:(服务器套接字)
String fromclient;
ServerSocket Server = new ServerSocket (5000);
System.out.println ("TCPServer Waiting for client on port 5000");
while(true)
{
Socket connected = Server.accept();
System.out.println( " THE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED ");
BufferedReader inFromClient = new BufferedReader(new InputStreamReader (connected.getInputStream()));
while ( true )
{
fromclient = inFromClient.readLine();
if ( fromclient.equals("q") || fromclient.equals("Q") )
{
connected.close();
break;
}
else
{
System.out.println( "RECIEVED:" + fromclient );
}
}
}
My PYTHON code : (Client Socket)
我的 PYTHON 代码:(客户端套接字)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5000))
while 1:
data = raw_input ( "SEND( TYPE q or Q to Quit):" )
if (data <> 'Q' and data <> 'q'):
client_socket.send(data)
else:
client_socket.send(data)
client_socket.close()
break;
OUTPUT::
输出::
ON PYTHON CONSOLE(Client):
在 Python 控制台(客户端)上:
SEND( TYPE q or Q to Quit):abcd ( pressing ENTER)
发送(键入 q 或 Q 退出):abcd(按 ENTER)
SEND( TYPE q or Q to Quit):efgh ( pressing ENTER)
发送(键入 q 或 Q 退出):efgh(按 ENTER)
SEND( TYPE q or Q to Quit):q ( pressing ENTER)
发送(键入 q 或 Q 退出):q(按 ENTER)
ON JAVA CONSOLE(Server):
在 JAVA 控制台(服务器)上:
TCPServer Waiting for client on port 5000
TCPServer 正在等待端口 5000 上的客户端
THE CLIENT /127.0.0.1:1335 IS CONNECTED
客户端 /127.0.0.1:1335 已连接
RECIEVED:abcdefghq
已收到:abcdefghq
回答by Shital
Append \n
to the end of data
:
附加\n
到data
:
client_socket.send(data + '\n')
回答by Ankit Shah
ya..you need to add '\n' at the end of the string in python client..... here's an example... PythonTCPCLient.py
ya..你需要在python客户端的字符串末尾添加'\n'.....这是一个例子...... PythonTCPCLient.py
`
`
#!/usr/bin/env python
import socket
HOST = "localhost"
PORT = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.sendall("Hello\n")
data = sock.recv(1024)
print "1)", data
if ( data == "olleH\n" ):
sock.sendall("Bye\n")
data = sock.recv(1024)
print "2)", data
if (data == "eyB}\n"):
sock.close()
print "Socket closed"
`
`
Now Here's the java Code: JavaServer.java`
现在,这里的Java代码: JavaServer.java`
import java.io.*;
import java.net.*;
class JavaServer {
public static void main(String args[]) throws Exception {
String fromClient;
String toClient;
ServerSocket server = new ServerSocket(8080);
System.out.println("wait for connection on port 8080");
boolean run = true;
while(run) {
Socket client = server.accept();
System.out.println("got connection on port 8080");
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
fromClient = in.readLine();
System.out.println("received: " + fromClient);
if(fromClient.equals("Hello")) {
toClient = "olleH";
System.out.println("send olleH");
out.println(toClient);
fromClient = in.readLine();
System.out.println("received: " + fromClient);
if(fromClient.equals("Bye")) {
toClient = "eyB";
System.out.println("send eyB");
out.println(toClient);
client.close();
run = false;
System.out.println("socket closed");
}
}
}
System.exit(0);
}
}
` Reference:Python TCP Client & Java TCP Server
回答by riyasyash
here is a working code for the same: Jserver.java
这是相同的工作代码:Jserver.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Jserver{
public static void main(String args[]) throws IOException{
ServerSocket s=new ServerSocket(5000);
try{
Socket ss=s.accept();
PrintWriter pw = new PrintWriter(ss.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br1 = new BufferedReader(new InputStreamReader(ss.getInputStream()));
//String str[20];
//String msg[20];
System.out.println("Client connected..");
while(true)
{
System.out.println("Enter command:");
pw.println(br.readLine());
//System.out.println(br1.readLine());
}
}
finally{}
}
}
Client.py
客户端.py
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 5000 # Reserve a port for your service.
s.connect((host, port))
while 1:
print s.recv(5000)
s.send("message processed.."+'\n')
s.close
回答by Nikolay Frick
I know it is late but specifically for your case I would recommend RabbitMQ RPC calls. They have a lot of examples on their web in Python, Java and other languages:
我知道现在已经晚了,但特别是对于您的情况,我建议使用 RabbitMQ RPC 调用。他们的网站上有很多 Python、Java 和其他语言的示例: