Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > TCP connection reply

Reply
Thread Tools

TCP connection reply

 
 
derek
Guest
Posts: n/a
 
      12-12-2004
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;
}
}
}
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      12-12-2004
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.

 
Reply With Quote
 
 
 
 
dereklai2k@yahoo.com.hk
Guest
Posts: n/a
 
      12-14-2004
Thanks!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reply....Reply All not in Browser Denny B Computer Support 3 04-24-2006 01:59 PM
Secure and compressed TCP/IP connection (for any TCP/IP services and applications) Pavel Aronovich Computer Security 0 02-22-2004 08:35 AM
can sombody tell me how to reply to sombody elses message and include the original message in the reply? Computer Support 3 08-24-2003 12:58 PM
Re: can sombody tell me how to reply to sombody elses message and include the original message in the reply? Computer Support 0 07-24-2003 09:21 PM



Advertisments