2012-11-07 98 views
0

我發現這個驚人的教程raywenderlich.com:扭曲蟒蛇服務器和Java聊天

http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

它創造一個扭曲的Python服務器,並與應用iphone聊天說話,我就會讓服務器也談與Java聊天,現在我寫一些代碼,這是蟒蛇服務器:

from twisted.internet.protocol import Protocol, Factory 
from twisted.internet import reactor 


class IphoneChat(Protocol): 
def connectionMade(self): 
    #self.transport.write("""connected""") 
    self.factory.clients.append(self) 
    print "clients are ", self.factory.clients 

def connectionLost(self, reason): 
    self.factory.clients.remove(self) 

def dataReceived(self, data): 
    #print "data is ", data 
    a = data.split(':') 
    if len(a) > 1: 
     command = a[0] 
     content = a[1] 

     msg = "" 
     if command == "iam": 
      self.name = content 
      msg = self.name + " has joined" 

     elif command == "msg": 
      msg = self.name + ": " + content 

     print msg 

     for c in self.factory.clients: 
      c.message(msg) 

def message(self, message): 
    self.transport.write(message + '\n') 


factory = Factory() 
factory.protocol = IphoneChat 
factory.clients = [] 

reactor.listenTCP(1025, factory) 
print "Chat server started" 
reactor.run() 

然後我創建了一個簡單的Java例子來連接到服務器:

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

public class MyClassSocket { 

private ObjectInputStream sInput;  // to read from the socket 
private ObjectOutputStream sOutput;  // to write on the socket 
private Socket socket; 

private String server, username; 
private int port; 

MyClassSocket(String server, int port, String username) { 
    this.server = server; 
    this.port = port; 
    this.username = username; 
} 

private void display(String msg) { 

    System.out.println(msg);  // println in console mode 

} 

public boolean start() { 

    try { 
     socket = new Socket(server, port); 
    } 
    // if it failed not much I can so 
    catch(Exception ec) { 
     display("Error connectiong to server:" + ec); 
     return false; 
    } 

    String msg = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort(); 
    display(msg); 

    try 
    { 

     sInput = new ObjectInputStream(socket.getInputStream()); //<-------- block here 
     sOutput = new ObjectOutputStream(socket.getOutputStream()); 
    } 
    catch (IOException eIO) { 
     display("Exception creating new Input/output Streams: " + eIO); 
     return false; 
    } 

    return true; 

} 

public static void main(String[] args) { 

    MyClassSocket client = new MyClassSocket("localhost",1025,"PieroJava"); 
    client.start(); 
} 

} 

代碼凍結點我在上面的代碼註釋,我可以看到在服務器終端,有一個新的連接,我可以在客戶端看到我連接,但不過去這裏:

sInput = new ObjectInputStream(socket.getInputStream()); 

我該怎麼辦?

+1

你怎麼知道這是客戶端阻塞的線? –

+0

我已經把一些System.out.println第一次,並在該行後,只打印第一... – Piero

+1

異常情況怎麼樣?你確定異常會被報告嗎?從http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Socket.html#getInputStream()它聽起來不像getInputStream會因爲任何原因而阻塞,所以很奇怪你觀察到它被阻塞。 –

回答

2

我建議不要使用ObjectInputStream和ObjectOutputStream。無論如何,讓它們與Python服務器良好地交互可能會很困難,而且它們也不會實現一個非常好的協議。我推薦使用更好的交叉語言支持的簡單協議,如​​。

在任何情況下,new ObjectInputStream似乎都會阻塞,直到它可以從連接中讀取字節。如果要使其成功,基於Twisted的服務器將需要將Java對象流的開頭寫入連接。

+0

好的,謝謝你的回答... – Piero