Creating java server problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NitinSawant
    Contributor
    • Oct 2007
    • 271

    Creating java server problem

    Hello friends, actually i have a problem with following programs:
    First:---------
    import java.net.*;
    import java.io.*;
    public class SimpleServer{
    public static void main(String[] a){
    ServerSocket s=null;
    try{
    s=new ServerSocket(54 32);
    }catch(IOExcept ion e){
    e.printStackTra ce();
    }
    while(true){
    try{
    Socket s1=s.accept();
    OutputStream s1out=s1.getOut putStream();
    BufferedWriter bw=new BufferedWriter( new OutputStreamWri ter(s1out));
    bw.write("Hello Net World!!");
    bw.close();
    s1.close();
    }catch(IOExcept ion e){
    e.printStackTra ce();
    }
    }
    }
    }

    Second:----------
    import java.net.*;
    import java.io.*;

    public class SimpleClient {
    public static void main(String[] a) {
    try
    {
    Socket s1 = new Socket("127.0.0 .1", 5432);
    InputStream is = s1.getInputStre am();
    DataInputStream dis = new DataInputStream (is);
    System.out.prin tln(dis.readUTF ());
    br.close();
    s1.close();
    }
    catch (ConnectExcepti on con)
    {
    System.err.prin tln("Coult not connect");
    }
    catch (IOException ex)
    {
    //ex.printStackTr ace();
    }
    }
    }

    when i compile and run these programs in two different cmd windows nothing is shown as output, pls help!!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Better use a (Buffered)Reade r on the client side because the server side uses
    a BufferedWriter for the printing purposes. A DataInputStream is normally used
    for reading binary data.

    kind regards,

    Jos

    Comment

    • NitinSawant
      Contributor
      • Oct 2007
      • 271

      #3
      thanx bro, can you pls tell me how to use that reader???

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Nitin646
        thanx bro, can you pls tell me how to use that reader???
        Simply read the API documentation. A BufferedReader even has a readLine()
        method. If you use a PrintWriter on the server side all will be fine.

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Nitin646
          thanx bro, can you pls tell me how to use that reader???
          Similar to how you have done the writing with the BufferedWriter above. This time you'll be reading so use BufferedReader.

          P.S Use code tags when posting code
          P.P.S Nice avatar

          Comment

          • NitinSawant
            Contributor
            • Oct 2007
            • 271

            #6
            thanx a lot r035198x, JosAH
            I've modified it and it is working fine ..

            [CODE=java]
            import java.net.*;
            import java.io.*;

            public class SimpleClient {
            public static void main(String[] a) {
            try
            {
            Socket s1 = new Socket("127.0.0 .1", 5432);
            InputStream is = s1.getInputStre am();
            BufferedReader rdr=new BufferedReader( new InputStreamRead er(is));
            String str=rdr.readLin e();
            System.out.prin tln(str);
            rdr.close();
            s1.close();
            }
            catch (ConnectExcepti on con)
            {
            System.err.prin tln("Coult not connect");
            }
            catch (IOException ex)
            {
            //ex.printStackTr ace();
            }
            }
            }
            [/CODE]
            Last edited by r035198x; Apr 3 '08, 11:27 AM. Reason: Added =java to the opening code tag

            Comment

            Working...