Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

doubt about socket programing

Thread view: 
chandu - 28 Feb 2006 10:10 GMT
my client side programme

import java.net.*;
import java.io.*;

class vissock1
  {
     public static void main(String args[]) throws IOException
         {
         Socket s=new Socket("172.18.19.203",80);
         InputStream in=s.getInputStream();
        OutputStream out=s.getOutputStream();
        DataInputStream d=new DataInputStream(System.in);
        int j;
        for(int i=0;i<3;i++)
          {
           try
             {
             int k=Integer.parseInt(d.readLine());
             out.write(k);
               }
             catch(Exception e)
              {
                  System.out.println("enter valid no");
               }
          while((j=in.read())!=-1)
         {
         System.out.print((char) j);
         }

       }

  }
}

my server side programme

import java.net.*;
import java.io.*;

class ser
        {
         public static void main(String args[]) throws IOException
              {
            ServerSocket s1=new ServerSocket(80);
            Socket s=s1.accept();
            InputStream in1=s.getInputStream();
              OutputStream out1=s.getOutputStream();
            while(1==1)
               {
                int j=in1.read();
                if(j%2==0)
                  {
                       out1.write("even\n".getBytes());
                    }
                 else
                 {
                 out1.write("odd\n".getBytes());
                }
     }
  }
}

i am able to get only one input from keyboard and accordingly the
response from server for that input.and then program is hanging
up...when i remove  the code for reading server response from the for
loop(in client code)  and keep out side the for loop i am getting
output.what is the reason.please help me.
Matt Humphrey - 28 Feb 2006 12:57 GMT
> my client side programme
>
[quoted text clipped - 25 lines]
>          System.out.print((char) j);
>          }

<snip>

> i am able to get only one input from keyboard and accordingly the
> response from server for that input.and then program is hanging
> up...when i remove  the code for reading server response from the for
> loop(in client code)  and keep out side the for loop i am getting
> output.what is the reason.please help me.

Your client loop to read the reply from the server is waiting for EOF.  I
think you want to stop reading after \n instead.  Hint: socket streams are
not packet-oriented at all. Also, DataInputStream.readLine() is
deprecated--look at switching to BufferedReader. Note also that if the user
enters an invalid number you program will still attempt to read a response
that will never come.  Read reply should only be coupled with a successful
write--and you can use BufferedReader on the reply here also.

Cheers,
Matt Humphrey  matth@ivizNOSPAM.com  http://www.iviz.com/
chandu - 02 Mar 2006 10:46 GMT
thank you for your reply..can you suggest me any link for better
understainding of streams in java..
Matt Humphrey - 02 Mar 2006 13:46 GMT
> thank you for your reply..can you suggest me any link for better
> understainding of streams in java..

Try Roedy's pages on I/O.  http://mindprod.com/jgloss/io.html They're not
stream-specific but it's a good starting place.  And don't forget the Java
API.    It may also seem that you should read some about TCP/IP because some
issues affect how you read data. http://mindprod.com/jgloss/tcpip.html

Cheers,
Matt Humphrey matth@ivizNOSPAM.com  http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.