Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Socket establishes connection but unable to pass command through stream

Posted on 2006-06-21
15
Medium Priority
?
273 Views
Last Modified: 2010-03-31
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("GET ");
               cmdText=cmdText.append(file);
               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(mySocket.getOutputStream(),"latin1");  //Connecting to TCP/IP Stream //
               } catch (UnsupportedEncodingException 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.getInputStream(), "latin1");
// Connecting to TCP/IP Stream //
                       } catch (UnsupportedEncodingException 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.readLine());
                                       System.out.println(input);


                                 }
                       } catch (IOException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                       }
}
0
Comment
Question by:sanjoybasu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
15 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 16949001
Where does it get stuck? Do you have an acitve connection? Are you behind a firewall?
0
 

Author Comment

by:sanjoybasu
ID: 16949016
No I am not behind firewall. Once TCP session establishes nothing happens.
0
 
LVL 35

Expert Comment

by:girionis
ID: 16949061
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 :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sanjoybasu
ID: 16949114
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16949142
Try

cmdText=cmdText.append(" HTTP/1.0/r/n/r/n");

If that doesn't work, please post the value of that StringBuffer
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 672 total points
ID: 16949147
Correction:

cmdText=cmdText.append(" HTTP/1.0\r\n\r\n");
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 664 total points
ID: 16949154
Ok, are you sure you are connecting to the right host? What are the "host" and "port" variables?
0
 
LVL 35

Expert Comment

by:girionis
ID: 16949166
> cmdText=cmdText.append(" HTTP/1.0\r\n\r\n");

Nice catch.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16949302
Why not try URLConnection?
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 664 total points
ID: 16949310
0
 
LVL 4

Expert Comment

by:Yagantappa
ID: 16950467
For http requests, you need to send "Content-length". Without it, requests will not be complete.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16950625
I doubt you need to do that for requests. It is only for response.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question