2016-04-05 19 views
1

Bir Python sunucusuna ve bir java istemcisine sahibim. java'dan python'a bir dize gönderildiğinde, bu tam değil. Örnek:Python sunucusu, java istemcisinden tam dizeyi almıyor

Java Kod gönderme: Helloooooooooo

Recieved Python gibi: Burada

he 

lloooooooo 

oo 

kodlardır

Python sunucusu:

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 Müşteri:

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(); 
} 

} Klaus D.is diyerek ne Boşuna

+0

(yerine Sendall ait göndermek kullanarak), piton tarafı bir kerede tüm dizeyi yazdırmak istediğiniz yapmak, ve bunun parçaları değil? Evet, tam olarak – craigsparks

+0

. Bütün dizeyi istiyorum çünkü daha sonra –

+0

kullanacağım işlem için tanımlayıcı olarak kullanacağım. Daha sonra, veri tamamlanana kadar soketten veri toplaman gerekecek, belki de \ n '. –

cevap

0

Java Client çöküyor durumunda incelikle soketi kapatmaktır: Ben iyi yolları olabilir eminim rağmen, basit tampon kullanılan veriyi sunucuya yolladım.

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 Sunucu: ben cevap yol çok değiştirilmesi gerekiyordu netleştirmek için

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 o bekler en fazla fazla veri, bu boyut ulaşılmadan önce içinde olanı işlemeye karar verebilirsiniz nasıl garanti eder. Sadece değişti:

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) 

atexit programınızı