Need Desperate Help...Java Sockets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JatP

    Need Desperate Help...Java Sockets

    hi Everyone
    I am trying to create a server and client to send files from one side
    to the other. I can send files from one side to the other using
    bufferedinput/output streams but when trying to send normal messages
    with printwriter my files being sent across are messed up. Empty jpg's
    are being sent with wrong file sizes. My code as follows:


    public class TCPServer
    {
    public static final int BUFFER_SIZE = 4096;
    public static final int RCV_PORT = 45000;

    public static void main(String[] args)
    {
    ServerSocket server;
    Socket socket;
    File file;
    PrintWriter out;
    BufferedReader in;
    String line;

    file = new File("C:Project ");

    try
    {
    server = new ServerSocket(RC V_PORT);
    socket = server.accept() ;

    OutputStream os = socket.getOutpu tStream();
    InputStream is = socket.getInput Stream();
    out = new PrintWriter(os, true);
    in = new BufferedReader( new InputStreamRead er(is));

    out.println("OK "); //WHEN I HAVE THIS LINE MY FILE BEING SEND IT
    MESSED UP. BUT WITHOUT THIS THE FILE IS SENT PERFECTLY


    SendFile(file, "test.JPG", os);
    out.close();
    in.close();
    socket.close();
    } catch (IOException e) { }

    }

    static void SendFile(File file, String filename, OutputStream os)
    {
    try
    {
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    File nfile = new File(file,filen ame);
    FileInputStream Fis = new FileInputStream (nfile);
    BufferedInputSt ream ins = new BufferedInputSt ream(Fis);
    BufferedOutputS tream outs = new BufferedOutputS tream(os);
    while(true)
    {
    count = ins.read(buffer , 0, BUFFER_SIZE);
    if (count <= 0)
    break;
    outs.write(buff er, 0, count);
    System.out.prin tln(buffer);
    }
    outs.flush();
    outs.close();
    ins.close();
    } catch (IOException e) {System.out.pri ntln(e);}
    }

    }

    CLIENT
    ---------

    public class TCPClient
    {
    public static final int BUFFER_SIZE = 4096;
    public static final String FILE_NAME = "WORK.jpg";
    public static void main(String[] args)
    {
    int port;
    String addr;
    try
    {
    Socket socket = new Socket(addr,por t);
    OutputStream os = socket.getOutpu tStream();
    InputStream is = socket.getInput Stream();
    PrintWriter out = new PrintWriter(os, true);
    BufferedReader in = new BufferedReader( new InputStreamRead er(is));

    String line = in.readLine(); //WHEN I HAVE THIS CODE ON THE CLIENT
    SIDE
    if (line.compareTo ("OK")!=0) //WITH RESPECT TO WHAT I HAVE ON THE
    SERVER SIDE
    return; //THE FILE BEING RECEIVED IS MESSED UP, OTHERWISE IS PERFECTLY
    RECEIVED

    RcvFile(is);

    out.close();
    in.close();
    socket.close();
    } catch (IOException e) { }

    }

    static void RcvFile(InputSt ream is)
    {
    try
    {
    BufferedInputSt ream ins = new BufferedInputSt ream(is);
    BufferedOutputS tream outs = new BufferedOutputS tream(new
    FileOutputStrea m(FILE_NAME));
    byte[] buffer = new byte[BUFFER_SIZE];

    while(true)
    {
    int count = ins.read(buffer , 0, BUFFER_SIZE);
    if (count <= 0)
    {
    break;
    }
    outs.write(buff er, 0, count);
    System.out.prin tln(buffer);
    }
    outs.flush();
    ins.close();
    outs.close();
    }
    catch(IOExcepti on e) {}
    }

    }




    Please let me know what i am doing wrong. From all the playing around
    i have done it seems that once i bind and send anything via the
    printwriter the Bufferedinputst ream doesn't work properly.

    Thanks a million

    JatP
  • Stephan Wehner

    #2
    Re: Need Desperate Help...Java Sockets

    Did you look at the line separator on client and server side?
    The documentation of PrintWriter says

    * The println() methods
    * use the platform's own notion of line separator rather than the newline
    * character.

    See also the private void newLine() method in PrintWriter.

    Stephan

    _______________ _______________ ____________
    Stephan Wehner
    Editor, Traffic Life: Passionate Tales and Exit Strategies
    An anthology about our car culture and alternatives with
    short stories, poems, cartoons and lots of other art
    Companion website for the book Traffic Life, an anthology about car culture and alternatives.




    vicky235ca@yaho o.com (JatP) wrote in message news:<8e875047. 0403071616.28ca 75ea@posting.go ogle.com>...[color=blue]
    > hi Everyone
    > I am trying to create a server and client to send files from one side
    > to the other. I can send files from one side to the other using
    > bufferedinput/output streams but when trying to send normal messages
    > with printwriter my files being sent across are messed up. Empty jpg's
    > are being sent with wrong file sizes. My code as follows:
    >
    >
    > public class TCPServer
    > {
    > public static final int BUFFER_SIZE = 4096;
    > public static final int RCV_PORT = 45000;
    >
    > public static void main(String[] args)
    > {
    > ServerSocket server;
    > Socket socket;
    > File file;
    > PrintWriter out;
    > BufferedReader in;
    > String line;
    >
    > file = new File("C:Project ");
    >
    > try
    > {
    > server = new ServerSocket(RC V_PORT);
    > socket = server.accept() ;
    >
    > OutputStream os = socket.getOutpu tStream();
    > InputStream is = socket.getInput Stream();
    > out = new PrintWriter(os, true);
    > in = new BufferedReader( new InputStreamRead er(is));
    >
    > out.println("OK "); //WHEN I HAVE THIS LINE MY FILE BEING SEND IT
    > MESSED UP. BUT WITHOUT THIS THE FILE IS SENT PERFECTLY
    >
    >
    > SendFile(file, "test.JPG", os);
    > out.close();
    > in.close();
    > socket.close();
    > } catch (IOException e) { }
    >
    > }
    >
    > static void SendFile(File file, String filename, OutputStream os)
    > {
    > try
    > {
    > byte[] buffer = new byte[BUFFER_SIZE];
    > int count;
    > File nfile = new File(file,filen ame);
    > FileInputStream Fis = new FileInputStream (nfile);
    > BufferedInputSt ream ins = new BufferedInputSt ream(Fis);
    > BufferedOutputS tream outs = new BufferedOutputS tream(os);
    > while(true)
    > {
    > count = ins.read(buffer , 0, BUFFER_SIZE);
    > if (count <= 0)
    > break;
    > outs.write(buff er, 0, count);
    > System.out.prin tln(buffer);
    > }
    > outs.flush();
    > outs.close();
    > ins.close();
    > } catch (IOException e) {System.out.pri ntln(e);}
    > }
    >
    > }
    >
    > CLIENT
    > ---------
    >
    > public class TCPClient
    > {
    > public static final int BUFFER_SIZE = 4096;
    > public static final String FILE_NAME = "WORK.jpg";
    > public static void main(String[] args)
    > {
    > int port;
    > String addr;
    > try
    > {
    > Socket socket = new Socket(addr,por t);
    > OutputStream os = socket.getOutpu tStream();
    > InputStream is = socket.getInput Stream();
    > PrintWriter out = new PrintWriter(os, true);
    > BufferedReader in = new BufferedReader( new InputStreamRead er(is));
    >
    > String line = in.readLine(); //WHEN I HAVE THIS CODE ON THE CLIENT
    > SIDE
    > if (line.compareTo ("OK")!=0) //WITH RESPECT TO WHAT I HAVE ON THE
    > SERVER SIDE
    > return; //THE FILE BEING RECEIVED IS MESSED UP, OTHERWISE IS PERFECTLY
    > RECEIVED
    >
    > RcvFile(is);
    >
    > out.close();
    > in.close();
    > socket.close();
    > } catch (IOException e) { }
    >
    > }
    >
    > static void RcvFile(InputSt ream is)
    > {
    > try
    > {
    > BufferedInputSt ream ins = new BufferedInputSt ream(is);
    > BufferedOutputS tream outs = new BufferedOutputS tream(new
    > FileOutputStrea m(FILE_NAME));
    > byte[] buffer = new byte[BUFFER_SIZE];
    >
    > while(true)
    > {
    > int count = ins.read(buffer , 0, BUFFER_SIZE);
    > if (count <= 0)
    > {
    > break;
    > }
    > outs.write(buff er, 0, count);
    > System.out.prin tln(buffer);
    > }
    > outs.flush();
    > ins.close();
    > outs.close();
    > }
    > catch(IOExcepti on e) {}
    > }
    >
    > }
    >
    >
    >
    >
    > Please let me know what i am doing wrong. From all the playing around
    > i have done it seems that once i bind and send anything via the
    > printwriter the Bufferedinputst ream doesn't work properly.
    >
    > Thanks a million
    >
    > JatP[/color]

    Comment

    Working...