java - Socket connection works over LAN but not WAN (internet) -
i have question i'm sure simple, don't know answer.
i have created chat program allows 2 users connect directly each other provided know port , ip of other user.
socket
try { if (ip != null && checkip(ip)) { connection = new socket(ip, port); out = new printwriter(connection.getoutputstream(), true); in = new bufferedreader(new inputstreamreader( connection.getinputstream())); system.out.println("connected"); printtimestamp(); styleddocument doc = maintext.getstyleddocument(); style style = maintext.addstyle("connection", null); styleconstants.setforeground(style, color.orange); try { doc.insertstring(doc.getlength(), "connection open to: " + ip + "\n", style); } catch (exception e) { e.printstacktrace(); } listenserver.stop(); system.out.println("making thread"); runnable2 = new inputlistener(); thread2 = new thread(runnable2); thread2.start(); } } catch (unknownhostexception e) { system.err.println("don't know host: " + ip); } catch (ioexception e) { system.err.println("couldn't i/o connection to: " + ip); }
serversocket
try { serversocket = new serversocket(clientwindow.port); settings.work = true; serversocket.setsotimeout(3000); } catch (ioexception e) { system.err.println("could not listen on port: " + clientwindow.port); settings.work = false; } while (running) { try { connection1 = serversocket.accept(); } catch (ioexception e) { system.err.println("accept failed port: " + clientwindow.port); } catch(nullpointerexception ne){ joptionpane.showmessagedialog(clientwindow.window, "there fatal error in startup.\nplease close instances of chattable before attempting open program again.", "fatal error", joptionpane.error_message); running = false; } try { if (connection1 != null) { out = new printwriter(connection1.getoutputstream(), true); in = new bufferedreader(new inputstreamreader(connection1.getinputstream())); system.out.println("accepted on server"); runnable2 = new inputlistener(); thread2 = new thread(runnable2); thread2.start(); running = false; styleddocument doc = maintext.getstyleddocument(); style style = maintext.addstyle("connection", null); styleconstants.setforeground(style, color.orange); try { doc.insertstring(doc.getlength(), "[ " + getcurrenttimestamp() + " ] connection open to: " + connection1.getremotesocketaddress() + "\n", style); } catch (exception e) {} } } catch (ioexception e) { system.err.println("couldn't i/o."); } }
this system works on lan, when try , use on internet throws ioexception socket code. using tcp or udp? mean trying do. have tips how work? matter of port not being open?
thanks!
is using tcp or udp?
tcp
what mean trying do. have tips how work?
see below.
is matter of port not being open?
most likely, should able more detail ioexception
confirm cause.
in question have answered provided details on port forwarding may find useful; c++ sockets router
you may need configure firewall allow ports external access depending on environment.
Comments
Post a Comment