Socket programming sample

Hi All,

Since I am using jdk1.0.2, which does not have HttpUrlConnection class, I am using pure Socket class to retrieve files from a WWW server. However, my code just could not work properly. Here is what I do:

      DataInputStream ins;
      DataOutputStream outs;
      String response;

      try{
            Socket s = new Socket("www.yahoo.com",80);

            System.out.print("Connected.\n");
            ins = new DataInputStream(s.getInputStream());
            outs = new DataOutputStream(s.getOutputStream());

            String str = new String("GET /index.html HTTP/1.0 \n");
                  
            outs.writeBytes(str);
            outs.flush();
            outs.close();
            System.out.print("Send "+ str);

            System.out.print(ins.readLine());

            while ((response = ins.readLine())!= null)
                  {
                  System.out.print(response);
                  }

            System.out.print("\n\nDone.\n");
            }
      catch (Exception e)
            {
            System.out.print(e.toString());
            }


It seems that when I read from the socket, it returns nothing, and just blocks. Anyone knows how to solve this, or did I do something obviously wrong?

Thanks

Chengyan
lll888Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
you must send several HTTP headers and one more blank line (CR LF) at the end of the request

typical request is

GET /index.html HTTP/1.0
User-Agent: Mozilla/4.0 (Windows NT 5.0;US) Opera 3.60  [en]
Accept: image/gif, image/x-xbitmap, image/jpeg, image/png, */*
Host: 127.0.0.1
<blank line>


get the HTTP spec from www.faqs.org (STD section)
0
rainmalCommented:
iii888 what your missing is just the "\n\n" in this line
String str = new String("GET /index.html HTTP/1.0 \n");

also dont close the socket until you have received the data.

Here try this code.

DataInputStream ins;
                 DataOutputStream outs;
                 String response;

                 try{
                 Socket s = new Socket("www.yahoo.com",80);

                 System.out.print("Connected.\n");
                 ins = new DataInputStream(s.getInputStream());
                 outs = new DataOutputStream(s.getOutputStream());

                 String str = new String("GET /index.html HTTP/1.0 \n\n");

                 outs.writeBytes(str);
                 outs.flush();
                 //outs.close();
                 System.out.print("Send "+ str);

                 System.out.print(ins.readLine());

                 while ((response = ins.readLine())!= null)
                 {
                 System.out.print(response);
                 }
               
                 outs.close();
                 System.out.print("\n\nDone.\n");
                 }
                 catch (Exception e)
                 {
                 System.out.print(e.toString());
                  outs.close();
                 }
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rainmalCommented:
Oops ...pls take out outs.close() from the outer try block

If you want to close the streams cause of an exception (which you should do) you should have another try..catch block preferebly after ins and outs are initalized.
 
0
mbormannCommented:
typical example of such code is ,have a reference outside try {} catch{}

like

Socket s=null;
DataInputStream din=null;
DataOutputStream dout=null;

try
{
//initialize all of them here
}
catch(Exception ee)
{}
finally
{
  try
  {
     din.close();
     dout.close();
     s.close();
  }
  catch(Exception ignoreThis)
  {}
}

:(
0
lll888Author Commented:
Thanks for all the help, now it works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.