Hi,
I’m trying to receive sent data with a Palm. I’m programming with superWaba and I’m using the PalmOS Emulator.
I wrote a testing program in Java that sends Strings over and I’m receiving them in the emulator, at least the first cycle (I’m reading with readBytes in a byte[10]). In the next cycle however nothing is read anymore…
Does anybody know what I’m doing wrong?
Thanks,
******** code server side app:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
/**
-
SocketTest
-
User: bart.vanlommel
-
Date: Feb 6, 2003
-
Time: 11:03:34 AM
-
To change this template use Options | File Templates.
*/
public class SocketTest {private Socket sock = null;
private ServerSocket srv = null;
private BufferedWriter wrtr = null;private static final int PORT = 2525;
public SocketTest() {
try {
System.out.println( “**** begin SocketTest ****” );
srv = new ServerSocket( PORT );
sock = srv.accept();System.out.println( "**** Socket accepted ****" ); sendData(); } catch ( Exception e ) { System.out.println( "Exception occurred: " + e.toString() ); e.printStackTrace(); }
}
private void sendData() throws Exception {
System.out.println( “**** starting sendData ****” );wrtr = new BufferedWriter( new OutputStreamWriter( sock.getOutputStream() ) ); int getal = 1; String test = null; while ( true ) { test = "test" + getal; System.out.println( "test = " + test ); wrtr.write( test ); wrtr.flush(); getal++; Thread.sleep( 1000 ); }
}
public static void main( String args[] ) {
SocketTest test = new SocketTest();
}
}
******** code superwaba client app:
import waba.ui.;
import waba.fx.;
import java.lang.*;
import waba.io.Socket;
import superwaba.ext.xplat.io.DataStream;
public class ConnSocket extends MainWindow {
String col0[] = {"Options","Cut","Copy","Paste","Exit"};
String col1[] = {"About ConnectionTest","Info"};
MenuBar mymenu = new MenuBar(new String[][]{col0,col1});
Button startBtn = new Button( "Start" );
Button stopBtn = new Button( "Stop" );
Label text = new Label("xxxxxxxxxxxxxxxxxx");
Socket port = null;
DataStream ds = null;
byte[] buffer = new byte[10];
public ConnSocket() {
setDoubleBuffer(true);
setBorderStyle(TAB_ONLY_BORDER);
setTitle("ConnectionTest");
setMenuBar(mymenu);
//Now the real implementation
add( text, 20, 50 );
add( startBtn, LEFT, BOTTOM );
add( stopBtn, RIGHT, BOTTOM );
}
public void onStart() {
repaint();
}
public void onEvent(Event event) {
if ( event.type == ControlEvent.PRESSED
&& event.target == stopBtn ) {
text.setText("Kiekeboe");
repaint();
return;
}
if ( event.type == ControlEvent.PRESSED
&& event.target == startBtn ) {
text.setText("Kiekeboe");
repaint();
listenOnSocket( port );
return;
}
if ( event.type == ControlEvent.WINDOW_CLOSED
&& mymenu.getSelectedMenuItem() == 004){
this.exit(0);
}
}
public void onPaint(Graphics g) {
}
private void listenOnSocket( Socket _soc )
{
if ( _soc == null ) _soc = new Socket( "localhost", 2525 );
ds = new DataStream( _soc );
if ( !_soc.isOpen() )
{
throw new RuntimeException("failed opening port : " + _soc.lastError);
}
int count = -1;
String s = null;
byte b = 0;
byte[] buf = new byte[10];
byte[] buf2 = null;
while ( count == -1 )
{
int test = 0;
try
{
count = ds.readBytes(buf, test, buf.length);
test = test + 10;
if ( count != -1 ) {
buf2 = new byte[count];
for (int i=0; i<count; i++) buf2[i] = buf[i];
text.setText(new String(buf2)+"/#"+count);
}
repaint();
}
catch (Exception e)
{
throw new RuntimeException("Exception e (in while): " + test);
}
}
}
}