[英]Java NIO server and Python asyncore client
我有以下的Java NIO服務器,以及下面的python asyncore客戶端。 服務器顯示“ Accepted ... \\ n”,但是,永遠不會調用客戶端的handle_connect。 有人可以幫助我解決服務器的問題,並幫助我通過客戶端連接到服務器嗎?
Java NIO服務器:
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.Selector;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
class Server
{
public Selector sel;
public ServerSocketChannel ssc;
public SocketChannel channel;
public static void main(String[] args) throws Exception
{
Server s = new Server();
s.openSocket(12000);
s.run();
}
private void openSocket(int port) throws Exception
{
InetSocketAddress address = new InetSocketAddress("0.0.0.0", port);
ssc = ServerSocketChannel.open();
ssc.configureBlocking(false);
ssc.socket().bind(address);
sel = Selector.open();
ssc.register(sel, SelectionKey.OP_ACCEPT);
}
public void run() throws Exception
{
while (true)
{
sel.select();
Set<SelectionKey> keys = sel.selectedKeys();
Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext())
{
SelectionKey key = (SelectionKey) i.next();
i.remove();
if (!key.isValid())
{
continue;
}
if (key.isAcceptable())
{
channel = ssc.accept();
channel.configureBlocking(false);
System.out.println("Accepted...\n");
channel.register(sel, SelectionKey.OP_READ);
}
if (key.isReadable())
{
if (channel == key.channel())
{
System.out.println("Readable\n");
ByteBuffer buffer = ByteBuffer.wrap(new byte[1024]);
int pos = channel.read(buffer);
buffer.flip();
System.out.println(new String(buffer.array(), 0, pos));
}
}
}
}
}
}
Python異步客戶端:
import socket
import select
import asyncore
class Connector(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.buffer = "hi"
self.create_socket()
self.connect((host, port))
def handle_connect(self):
print("[]---><---[]") # not called <------------------
def handle_read(self):
pass
def writable(self):
len(self.buffer) > 0
def handle_write(self):
sent = self.send(self.buffer)
print("[]--->" + self.buffer[0:sent])
self.buffer = self.buffer[sent:]
def handle_close(self):
print("[]...x...[]")
self.close()
connector = Connector("localhost", 12000, Handler())
asyncore.loop()
Python正常工作的客戶端:
# Echo client program
import socket
import sys
HOST = 'localhost' # The remote host
PORT = 12000 # The same port as used by the server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
print("socket")
except OSError as msg:
s = None
continue
try:
s.connect(sa)
print("connected")
except OSError as msg:
s.close()
s = None
continue
break
if s is None:
print('could not open socket')
sys.exit(1)
print("Sending")
s.sendall(bytes("Hey server", "UTF-8"))
data = s.recv(1024)
# s.close()
print('Received', repr(data))
編輯為Java添加了isReadable並添加了正常工作的python客戶端。
在實現一種繁瑣的異步方法中,您犯了兩個錯誤:
def writable(self):
len(self.buffer) > 0
此方法返回None
(因為您忘記了該語句的return
部分)。 第一個錯誤是None
的布爾值是錯誤的,因此從不認為Connector
是可寫的。 第二個錯誤是您在嘗試建立連接時需要檢查可寫性。 由於writable
總是返回false,包括在連接嘗試期間,因此建立連接不會取得任何進展。
我建議改用Twisted 。 它並不能使您自己實現低級緩沖區管理和連接設置代碼,因此實際上可以產生更高效,更短且更易於編寫的代碼。
異步實際上應該被視為歷史文物,並且從不實際使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.