Twistd-Telnet-Python服务器,Java客户端,客户端在连接关闭之前从未收到字符串,此时所有字符串连接在一起并发送
我有一个用python编写的运行twisted(twistd)的服务器和一个用Java编写的客户机。我们的想法是,我们将能够通过该服务器在客户端之间发送编码数据字符串。但是,我们发现客户端从未向服务器发送字符串(它从未在服务器上记录为已收到)。有人有什么想法吗 我在下面包含了客户端和服务器的代码 客户:Twistd-Telnet-Python服务器,Java客户端,客户端在连接关闭之前从未收到字符串,此时所有字符串连接在一起并发送,java,python,twisted,telnet,Java,Python,Twisted,Telnet,我有一个用python编写的运行twisted(twistd)的服务器和一个用Java编写的客户机。我们的想法是,我们将能够通过该服务器在客户端之间发送编码数据字符串。但是,我们发现客户端从未向服务器发送字符串(它从未在服务器上记录为已收到)。有人有什么想法吗 我在下面包含了客户端和服务器的代码 客户: Socket s = new Socket("localhost", 1025); InputStream is = s.getInputStream(); Output
Socket s = new Socket("localhost", 1025);
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
System.out.println("Before Read");
System.out.println(is.read());
System.out.println("After Read");
os.write("Hello from java!".getBytes());
服务器:
class MyChat(basic.LineReceiver):
__opponent = None
def connectionMade(self):
print "SLOG"
self.factory.clients.append(self)
print "SLOG 1"
self.factory.notInGame.append(self)
print "SLOG 2"
while (len(self.factory.notInGame) >= 2):
x = self.factory.notInGame.pop(0)
y = self.factory.notInGame.pop(0)
x.__opponent = y
y.__opponent = x
print "SLOG FINISH YAY"
def connectionLost(self, reason):
print "SLOG Lost a client!"
self.factory.clients.remove(self)
if (self.__opponent == None):
self.factory.notInGame.remove(self)
else:
self.__opponent.__opponent = None
self.factory.notInGame.append(self.__opponent)
def lineReceived(self, data):
print "SLOG Sender data received"
if self.__opponent == None:
self.transport.write("E0") # not in game
print "SLOG E0"
return
self.__opponent.transport.write(data)
from twisted.internet import protocol
from twisted.application import service, internet
factory = protocol.ServerFactory()
factory.protocol = MyChat
factory.clients = []
factory.notInGame = []
application = service.Application("chatserver")
#internet.TCPServer(1025, factory).setServiceParent(application)
reactor.listenTCP(1025, factory)
谢谢你的帮助
Sam
LineReceiver
在缓冲区中累积数据,并在接收到整行数据时调用lineReceived
回调。默认情况下,行由字节序列“\r\n”
终止
看起来您的Java应用程序发送了
“Hello from Java!”
。由于它不发送“\r\n”
LineReceiver
从未决定呼叫lineReceived
谢谢-这就是问题所在-我几分钟前就发现了它。我现在遇到的问题是Java read只想读取一个字节!