java - Byte[] sent across SocketChannel but not received -
i've been having trouble project requires bit of networking, data sent on socketchannel
never received. able replicate issue simple localhost chatroom program (sorry if it's bit messy):
public class main { private sender sender; private receiver receiver; public static void main(string[] args) { main foo = new main(); //the ports switched in other running version of foo.receiver = new receiver("192.168.1.108", 12348); foo.sender = new sender("192.168.1.108", 12347); foo.takeuserinput(); } private void takeuserinput() { while(true) { system.out.println("enter something"); bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); string input = null; try { input = br.readline(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } sender.send(input); } } } public class receiver implements closeable { private inetsocketaddress bindaddress; private serversocketchannel server; private listenthread listenthread; public receiver(string address, int port) { bindaddress = new inetsocketaddress(address, port); bind(); listen(); } public void bind() { try { server = serversocketchannel.open(); server.configureblocking(true); server.bind(bindaddress); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println("bound port " + bindaddress.getport()); } public void listen() { listenthread = new listenthread(); listenthread.start(); } private class listenthread extends thread { private socketchannel client; public void run() { try { client = server.accept(); system.out.println("received connection " + client.getlocaladdress()); } catch (ioexception e) { e.printstacktrace(); } while((server.isopen()) && (client.isopen())) { byte[] bytes = new byte[4096]; bytebuffer buffer = bytebuffer.wrap(bytes); try { system.out.println("reading"); client.read(buffer); system.out.println(new string(buffer.array())); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } } @override public void close() throws ioexception { server.close(); listenthread.client.close(); } } public class sender implements closeable { private inetsocketaddress connectaddress; private socketchannel clientchannel; public sender(string address, int port) { connectaddress = new inetsocketaddress(address, port); connect(); } public void connect() { while((clientchannel == null) || (!(clientchannel.isconnected()))) { try { clientchannel = socketchannel.open(connectaddress); } catch (ioexception e) { e.printstacktrace(); } } try { system.out.println("connected " + clientchannel.getlocaladdress()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public void send(string message) { byte[] bytes = message.getbytes(); bytebuffer buffer = bytebuffer.wrap(bytes); try { clientchannel.write(buffer); } catch (ioexception e) { e.printstacktrace(); } system.out.println("sent message"); } @override public void close() throws ioexception { clientchannel.close(); } }
here's logs 1 version:
bound port 12348 reading connected /192.168.1.108:64699 enter thing sent message enter
and other:
bound port 12347 reading connected /192.168.1.108:64698 enter
so, know both programs establish connections other, , start reading, when send on socketchannel 1 end, other remains stuck on read()
call in listenthread
.
how can make client read sent?
i can see 2 issues.
as mentioned @ethan f, ports different. should use same port number.
your
listen()
method in receiver class never called. need call method accept connection.foo.receiver = new receiver("192.168.1.108", 12348); foo.receiver.listen();
Comments
Post a Comment