Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38175
| X-Received | by 10.224.175.65 with SMTP id w1mr12923535qaz.7.1360051768232; Tue, 05 Feb 2013 00:09:28 -0800 (PST) |
|---|---|
| X-Received | by 10.49.60.40 with SMTP id e8mr2000436qer.40.1360051768208; Tue, 05 Feb 2013 00:09:28 -0800 (PST) |
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!p13no12179502qai.0!news-out.google.com!k2ni8440qap.0!nntp.google.com!p13no12179501qai.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Tue, 5 Feb 2013 00:09:28 -0800 (PST) |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=50.46.150.184; posting-account=FzvKZAoAAABcf_pRXz5DcUGbDer7_Jzq |
| NNTP-Posting-Host | 50.46.150.184 |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <ce1ca918-2bbe-440b-95eb-89dc257ffc9f@googlegroups.com> (permalink) |
| Subject | Java NIO server and Python asyncore client |
| From | foobarometer@gmail.com |
| Injection-Date | Tue, 05 Feb 2013 08:09:28 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Received-Bytes | 5100 |
| Xref | csiph.com comp.lang.python:38175 |
Show key headers only | View raw
Can someone help answer this?
http://stackoverflow.com/questions/14698020/java-nio-server-and-python-asyncore-client
Blocking python client works, asyncore doesn't work.
Server.java:
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));
}
}
}
}
}
}
Asyncore Python Code:
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 Blocking client:
# 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))
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Java NIO server and Python asyncore client foobarometer@gmail.com - 2013-02-05 00:09 -0800 Re: Java NIO server and Python asyncore client dieter <dieter@handshake.de> - 2013-02-06 08:42 +0100 Re: Java NIO server and Python asyncore client Petri Heinilä <hevi00@gmail.com> - 2013-02-15 01:41 -0800
csiph-web