i trying write code allow me connect remote server , receive response it. remote address : www.euref-ip.net:2101
when trying connect web page works fine. when trying connect through java code cant response back. here code far:
public class client implements runnable{ private string nserver = ""; private int nport = 0; public client(string server, int port){ this.nserver = server; this.nport = port; } @override public void run() { try { socketaddress sockaddr = new inetsocketaddress(nserver, nport); socket s = new socket(); s.connect(sockaddr, 10 * 1000); if (s.isconnected()) { s.setsotimeout(20 * 1000); dataoutputstream out = new dataoutputstream (s.getoutputstream()); datainputstream in = new datainputstream (s.getinputstream()); while (true) { // send message server string requestmsg = "get / http/1.0\r\n"; requestmsg += "user-agent: client v1\r\n"; requestmsg += "accept: */* \r\n"; requestmsg += "connection: keep alive\r\n"; out.write(requestmsg.getbytes()); out.flush(); // receive response int ln = in.available(); byte [] bytes = new byte [ln]; in.read(bytes); system.out.println(new string(bytes) + "\n"); thread.sleep(2000); } } } catch (unknownhostexception ex) {system.out.println(ex);} catch (ioexception ex) {system.out.println(ex);} catch (interruptedexception ex) {system.out.println(ex);} }}
right ln
variable 0 , reading empty response. doing wrong? suggestions? appreciated.
your http request incomplete, need add empty line @ end indicating end of header section here end of request.
try this:
string requestmsg = "get / http/1.0\r\n"; ... requestmsg += "connection: keep alive\r\n"; requestmsg += "\r\n";
Comments
Post a Comment