2016-04-05 8 views
1

私はPythonサーバーとJavaクライアントを持っています。文字列がJavaからPythonに送られるとき、それは完全ではありません。例:送信PythonサーバーはJavaクライアントから完全な文字列を受け取りません

Javaコード:PythonでHelloooooooooo

受け取ったとして:

import socket 

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

server_address = ("localhost", 10000) 
print 'starting up on %s port %s' % server_address 
sock.bind(server_address) 

sock.listen(5) 

while True: 
    print 'waiting for a connection' 
    connection, client_address = sock.accept() 

    try: 
     print 'connection from', client_address 

     while True: 
      data = connection.recv(1024) 
      print 'received: ' + data 
      print type(data) 
      if data: 
       print 'sending data back to the client' 
       connection.sendall(data) 
      else: 
       print 'no more data from', client_address 
       break 

    finally: 
     connection.close() 

Javaクライアント:

ここ

he 

lloooooooo 

oo 

コードが

Pythonのサーバーです

package server; 

import java.io.*; 
import java.net.*; 

class Reciever_test { 
public static void main(String args[]) throws Exception { 
     String sentence; 
     String modifiedSentence; 
     BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
    Socket clientSocket = new Socket("localhost", 10000); 
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
    while(true) 
    { 
     sentence = inFromUser.readLine(); 
     outToServer.writeBytes(sentence + "\n"); 
     modifiedSentence = inFromServer.readLine(); 
     System.out.println("FROM SERVER:" + modifiedSentence); 
    } 

    //clientSocket.close(); 
} 

}クラウスD.isが言って何の無駄に

+0

明確にするためには、Python側が一度に文字列全体を印刷したいです、それの部分ではない? – craigsparks

+0

はい、正確です。私は後で使用する操作の識別子として後で使用するので、文字列全体が必要です –

+0

ソケットからデータが完全になるまで収集する必要があります。おそらく '\ n'が含まれるまでです。 –

答えて

0

Javaクライアントがクラッシュした場合にのatexitが優雅にソケットをクローズすることです:ちょうど変更 私はサーバーにデータを送信する方法。

public static void main(String args[]) throws Exception { 
    String sentence; 
    String modifiedSentence; 
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
    Socket clientSocket = new Socket("localhost", 10000); 
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); 
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); 
    Scanner sc = new Scanner(System.in); 
    while(true) 
    { 


     while (true) { 
      String input = sc.nextLine(); 
      out.print(input); 
      out.flush(); 
      modifiedSentence = inFromServer.readLine(); 
      System.out.println("FROM SERVER:" + modifiedSentence); 
     } 
    } 

Pythonのサーバー:私は(sendallの代わりに送って使用して)あまりにも変更する必要がありました応える 方法

while True: 
print 'waiting for a connection' 
connection, client_address = sock.accept() 

try: 
    print 'connection from', client_address 

    while True: 
     data = connection.recv(4096) 
     print data 
     print type(data) 
     if data: 
      print 'sending data back to the client' 
      connection.send(data + "\n") 
     else: 
      print 'no more data from', client_address 
      break 

finally: 
    connection.close() 
0

RECVそれは待つだろうせいぜい多くのデータが、それはサイズに到達する前に、それが持っているものを扱うことにしたかもしれない方法保証。私はより良い方法があるかもしれません確信しているが、私は、シンプルバッファを使用:

import socket 
import atexit 

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server_address = ("localhost", 10000) 
print ('starting up on %s port %s' % server_address) 
sock.bind(server_address) 

sock.listen(5) 

def close(connection): 
    connection.close() 

while True: 
    print ('waiting for a connection') 
    connection, client_address = sock.accept() 
    atexit.register(close, connection) 

    try: 
     print ('connection from', client_address) 

     buffer=[] 
     while True: 

      data = connection.recv(1024) 
      for b in data: 
       buffer.append(b) 
      print ('received: ' + str(data)) 
      print (type(data)) 

      if str(buffer[-1]) == '\n': #if changed to use the buffer to decide when done receiving, the newline is the terminator 
       print ('sending data back to the client') 
       connection.sendall(''.join(buffer)) 
       buffer = [] 
      # else: 
      #  print ('no more data from', client_address) 
      #  break 

    finally: 
     close(connection) 

あなたのプログラムが

関連する問題