我的目标是从python套接字向java套接字发送消息。我确实看了上面提到的资源。但是我很难让Python客户端与Java服务器通信。主要是因为python中的(行尾)与java中的不同。
说我从python客户端写:消息1:abcd消息2:efgh消息3:q(退出)
在java服务器上:我收到消息1:abcdefghq后跟异常,因为python客户端已经从它的末尾关闭了套接字。
有人可以为java和python之间的一致谈话提出解决方案。
我使用的参考:http://www.prasannatech.net/2008/07/socket-programming-tutorial.html
更新:我忘了添加,我正在研究TCP。
我的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 );
}
}
}
我的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(客户):
发送(Q型或Q退出):abcd(按ENTER键)
发送(Q型或Q退出):efgh(按ENTER键)
发送(Q型或Q退出):q(按ENTER键)
ON JAVA CONSOLE(服务器):
TCPServer在端口5000上等待客户端
客户端/127.0.0.1:1335已连接
收到:abcdefghq
答案 0 :(得分:7)
您需要将\n
附加到数据结尾...
即。 :
client_socket.send(data + '\n')
答案 1 :(得分:1)
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"
`
现在这是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);
}
}
答案 2 :(得分:1)
这是一个相同的工作代码: 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
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
答案 3 :(得分:0)
我知道它已经晚了但是特别针对你的情况我建议使用RabbitMQ RPC调用。他们的网站上有很多Python,Java和其他语言的例子: