D
derek
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;
}
}
}
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;
}
}
}