TCP connection reply

Discussion in 'Java' started by derek, Dec 12, 2004.

  1. derek

    derek Guest

    Hi,

    I am writing a TCP program which has 2 sides. The client side sends
    application command to the server and the server just reply data. The
    server & client side code is shown in the following. I have debugged
    the server side that it has replied data. However, the client side
    does not receive any data. Could anyone tell me why? Thanks!

    Client side:

    :
    :
    try
    {
    Socket socket=new Socket("localhost",0xaa);
    OutputStream os=socket.getOutputStream();
    InputStream is=socket.getInputStream();

    int i=0;
    int j=0;

    byte[] b=new byte[4];

    b[i++]=3;
    b[i++]=(byte)0xa0;
    b[i++]=(byte)0xa6;
    b[i++]=(byte)0x50;
    os.write(b);

    ThreadSocketTimer tSocketTimer=new
    ThreadSocketTimer(this,is,2000,2+32);
    tSocketTimer.start();
    tSocketTimer.join();

    debug.println(tSocketTimer.v.size());

    }
    catch(IOException ee)
    {
    app.handleError(this,ee);
    }
    catch(InterruptedException ee)
    {
    app.handleError(this,ee);
    }
    :
    :

    Server Side:

    class ThreadSS extends Thread
    {
    int bc;

    final byte StatusOK=0;

    BufferedInputStream bis;
    BufferedOutputStream bos;


    int len;
    Vector data;

    Debug debug=new Debug();

    ServerSocket ss;

    Component parent;

    ThreadSS(Component parent)
    {
    this.parent=parent;

    try
    {
    ss=new ServerSocket(0xaa);
    }
    catch(Exception e)
    {
    app.handleError(parent,e);
    return;
    }
    }

    public void run()
    {
    try
    {
    while(true)
    {
    Socket socket=ss.accept();

    bis=new BufferedInputStream(socket.getInputStream());
    bos=new BufferedOutputStream(socket.getOutputStream());

    while(true)
    {
    sleep(1);
    int i;
    try
    {
    i=socket.getInputStream().read();
    }
    catch(SocketException se)
    {
    break;
    }

    if(i==-1)
    {
    }
    else
    {
    if(data==null)
    {
    bc=i;
    data=new Vector();
    len=0;
    }
    else
    {
    data.add(new Byte((byte)i));

    len++;
    if(len==bc)
    {
    resolve();
    data=null;
    }
    }
    }
    }
    }
    }
    catch(Exception e)
    {
    app.handleError(parent,e);
    return;
    }
    }

    void resolve()
    {
    byte b=((Byte)data.get(0)).byteValue();
    switch(b)
    {
    case CommandGetFont:
    byte[] c=new byte[2];

    c[0]=((Byte)data.get(1)).byteValue();
    c[1]=((Byte)data.get(2)).byteValue();
    bos.write(c);//******<---------have run this code but client
    side does not received.
    break;
    }
    }
    }
    derek, Dec 12, 2004
    #1
    1. Advertising

  2. derek

    Sudsy Guest

    derek wrote:
    <snip>
    > bis=new BufferedInputStream(socket.getInputStream());
    > bos=new BufferedOutputStream(socket.getOutputStream());

    <snip>

    You have to flush the stream. After all you /did/ specify a buffered
    stream and it's probably waiting for more data (buffering) before
    sending.
    bos.flush() will tell it to go ahead and send what it has.

    --
    Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.
    Sudsy, Dec 12, 2004
    #2
    1. Advertising

  3. derek

    Guest

    Thanks!
    , Dec 14, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abraham Khalil
    Replies:
    2
    Views:
    427
    Abraham Khalil
    Jan 26, 2004
  2. Kathryn Bean
    Replies:
    1
    Views:
    672
    John Harrison
    Oct 28, 2004
  3. Tiger
    Replies:
    5
    Views:
    953
    Dave Thompson
    May 1, 2006
  4. davidj411
    Replies:
    1
    Views:
    1,015
    Mike Driscoll
    May 27, 2008
  5. Replies:
    2
    Views:
    530
Loading...

Share This Page