sanjoybasu
asked on
Socket establishes connection but unable to pass command through stream
Learning Java so please bear with me..
I am trying this exercise (not school homework) where
I am passing URL String eg http:/www.myurl.com/mydir/myfile.html to the class. Constructor dissects into host using getHost() and file name getFile().
Then there is a method which tries following:
1)Establish connection at port 80 with host
2)Connect OutputStreamWriter to Socket output Stream
3)Use OutputStreamWriter write method to pass string GET /mydir/myfile.html HTTP1.0
4)Similarly connect InputStreamReader to Socket input stream , BufferReader to InputStreamReader. Try to dump BufferReader to a file.
Compiles without error. But when run nothing happens. Tried to see the behevour of client using Ethereal. 3 way TCP hand shake happens but nothing happens beyond that point. Not sure what went wrong with coding please help
Code of the method only
public void myGetClient(){
Socket mySocket=null;
Writer command=null;
Reader fetch=null;
BufferedReader storeFetch=null;
FileWriter fileOut=null;
// Generating HTTP Command //
StringBuffer cmdText=new StringBuffer();
cmdText=cmdText.append("GE T ");
cmdText=cmdText.append(fil e);
cmdText=cmdText.append(" HTTP/1.0/r/n");
cmdText.toString();
String cmdteXt=cmdText.toString() ;
System.out.println(cmdteXt );
// Command Generated //
// Establish TCP/IP Connection //
try {
mySocket=new Socket(host,port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Send Command to TCP/IP stream //
try {
command=new OutputStreamWriter(mySocke t.getOutpu tStream(), "latin1"); //Connecting to TCP/IP Stream //
} catch (UnsupportedEncodingExcept ion e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
command.write(cmdteXt); // Dumping into TCP/IP stream //
command.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Command Sent. Hopefully !!! //
// See what is there on other side //
try {
fetch=new InputStreamReader(mySocket .getInputS tream(), "latin1");
// Connecting to TCP/IP Stream //
} catch (UnsupportedEncodingExcept ion e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
storeFetch=new BufferedReader(fetch); /
try {
fileOut=new FileWriter("Download.html" ); // Open File
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String input;
while(( input=storeFetch.readLine( ))!=null){
fileOut.write(storeFetch.r eadLine()) ;
System.out.println(input);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am trying this exercise (not school homework) where
I am passing URL String eg http:/www.myurl.com/mydir/myfile.html to the class. Constructor dissects into host using getHost() and file name getFile().
Then there is a method which tries following:
1)Establish connection at port 80 with host
2)Connect OutputStreamWriter to Socket output Stream
3)Use OutputStreamWriter write method to pass string GET /mydir/myfile.html HTTP1.0
4)Similarly connect InputStreamReader to Socket input stream , BufferReader to InputStreamReader. Try to dump BufferReader to a file.
Compiles without error. But when run nothing happens. Tried to see the behevour of client using Ethereal. 3 way TCP hand shake happens but nothing happens beyond that point. Not sure what went wrong with coding please help
Code of the method only
public void myGetClient(){
Socket mySocket=null;
Writer command=null;
Reader fetch=null;
BufferedReader storeFetch=null;
FileWriter fileOut=null;
// Generating HTTP Command //
StringBuffer cmdText=new StringBuffer();
cmdText=cmdText.append("GE
cmdText=cmdText.append(fil
cmdText=cmdText.append(" HTTP/1.0/r/n");
cmdText.toString();
String cmdteXt=cmdText.toString()
System.out.println(cmdteXt
// Command Generated //
// Establish TCP/IP Connection //
try {
mySocket=new Socket(host,port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Send Command to TCP/IP stream //
try {
command=new OutputStreamWriter(mySocke
} catch (UnsupportedEncodingExcept
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
command.write(cmdteXt); // Dumping into TCP/IP stream //
command.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Command Sent. Hopefully !!! //
// See what is there on other side //
try {
fetch=new InputStreamReader(mySocket
// Connecting to TCP/IP Stream //
} catch (UnsupportedEncodingExcept
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
storeFetch=new BufferedReader(fetch); /
try {
fileOut=new FileWriter("Download.html"
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
String input;
while(( input=storeFetch.readLine(
fileOut.write(storeFetch.r
System.out.println(input);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Where does it get stuck? Do you have an acitve connection? Are you behind a firewall?
ASKER
No I am not behind firewall. Once TCP session establishes nothing happens.
Maybe you need to send more data, like Content-length etc. Have a look how it is done here: http://javaalmanac.com/egs/java.net/PostSocket.html with a POST request. Also, you do not need several try... catch blocks of code, all can be done within one :)
ASKER
girionis I disagree with your earlier comment of not sending more data. If that was the case I should see HTTP request to server atleast then may be some kind of HTTP 4xx client error in Ethereal. I dont see any HTTP request sent to server. Also when I access the same URL using standard browser it send exactly same GET /mydir/myfile.html HTTP1.0 as I am trying to send and server acks with HTTP code 200.
Try
cmdText=cmdText.append(" HTTP/1.0/r/n/r/n");
If that doesn't work, please post the value of that StringBuffer
cmdText=cmdText.append(" HTTP/1.0/r/n/r/n");
If that doesn't work, please post the value of that StringBuffer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> cmdText=cmdText.append(" HTTP/1.0\r\n\r\n");
Nice catch.
Nice catch.
Why not try URLConnection?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For http requests, you need to send "Content-length". Without it, requests will not be complete.
I doubt you need to do that for requests. It is only for response.