java - Address already in use: JVM_Bind. Sockets Chat -
i have program chat sockets, 1 multicast , unicast other, when trying run both following error:
java.net.bindexception: address in use: jvm_bind @ java.net.dualstackplainsocketimpl.bind0(native method) @ java.net.dualstackplainsocketimpl.socketbind(dualstackplainsocketimpl.java:96) @ java.net.abstractplainsocketimpl.bind(abstractplainsocketimpl.java:376) @ java.net.plainsocketimpl.bind(plainsocketimpl.java:175) @ java.net.serversocket.bind(serversocket.java:376) @ java.net.serversocket.<init>(serversocket.java:237) @ java.net.serversocket.<init>(serversocket.java:128) @ chat.chatprivado_servidor$hilo.run(chatprivado_servidor.java:47)
apparently error in chatprivado_servidor class thread, put them code me please. excuse bad english
class chatprivado_servidor
class hilo extends thread { string msgin = ""; public void run() { try{ ss = new serversocket(1500); s = ss.accept(); din = new datainputstream(s.getinputstream()); dout = new dataoutputstream(s.getoutputstream()); while(!msgin.equals("exit")){ msgin = din.readutf(); textarea.settext(textarea.gettext().trim()+"\n "+msgin); } }catch(exception e){ e.printstacktrace(); } } }
class chatmulticast
private void send(string mensaje) throws ioexception { string mensaje2 = nombre + ": " + mensaje; byte[] buffer_e = mensaje2.getbytes(); datagrampacket paquete_enviado = new datagrampacket(buffer_e, 0, buffer_e.length, direccion, 4321); socket.send(paquete_enviado); } class reader extends thread { public void run() { byte[] buffer_r = new byte[512]; datagrampacket paquete_recibido = null; while (true) { try { paquete_recibido = new datagrampacket(buffer_r, buffer_r.length); socket.receive(paquete_recibido); } catch (ioexception e) { e.printstacktrace(); } string linea = new string(paquete_recibido.getdata(), 0, paquete_recibido.getlength()); } } } chatmulticast(string n) { nombre = n; try { direccion = inetaddress.getbyname("228.5.6.7"); socket = new multicastsocket(4321); socket.joingroup(direccion); } catch (exception e) { e.printstacktrace(); system.exit(1); }
}
i have placed ports , accepts me none
it looks you're trying create multiple serversockets
on same port.
your code structure incorrect.
you should create single serversocket
, have loop accepts connections , spawns thread each accepted socket, , class provides runnable
threads, 1 instance per thread, run()
method contains loop reading requests , responding them until end of stream detected.
i suggest read custom networking section of oracle java tutorial.
Comments
Post a Comment