I paste my codes below;;
Client ::
Expand|Select|Wrap|Line Numbers
- public void startApp() { try { // establish a socket connection with remote server streamConnection = (StreamConnection) Connector.open(connectString);
- // create DataOuputStream on top of the socket connection
- outputStream = streamConnection.openOutputStream();
- dataOutputStream = new DataOutputStream(outputStream);
- // send the HTTP request
- dataOutputStream.writeChars("Hello");
- dataOutputStream.flush();
- // create DataInputStream on top of the socket connection
- inputStream = streamConnection.openInputStream();
- dataInputStream = new DataInputStream(inputStream);
- // retrieve the contents of the requested page from Web server
- String test="";
- int inputChar;
- System.out.println("Entering read...........");
- while ( (inputChar = dataInputStream.read()) != -1) {
- // test=test+((char)inputShar);
- results.append((char) inputChar);
- }
- System.out.println("Leaving read...........");
- // display the page contents on the phone screen
- //System.out.println(" Result are "+results.toString());
- System.out.println(" ");
- resultField = new StringItem(null, results.toString());
- System.out.println("Client says "+resultField);
- resultScreen.append(resultField);
- myDisplay.setCurrent(resultScreen);
- } catch (IOException e) {
- System.err.println("Exception caught:" + e);
- } finally {
- // free up I/O streams and close the socket connection
- try {
- if (dataInputStream != null)
- dataInputStream.close();
- } catch (Exception ignored) {}
- try {
- if (dataOutputStream != null)
- dataOutputStream.close();
- } catch (Exception ignored) {}
- try {
- if (outputStream != null)
- outputStream.close();
- } catch (Exception ignored) {}
- try {
- if (inputStream != null)
- inputStream.close();
- } catch (Exception ignored) {}
- try {
- if (streamConnection != null)
- streamConnection.close();
- } catch (Exception ignored) {}
- }
- }
Expand|Select|Wrap|Line Numbers
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- try{
- ServerSocket sck=new ServerSocket(880);
- Socket client=sck.accept();
- InputStream inp= client.getInputStream();
- int i;
- OutputStream out=client.getOutputStream();
- out.write("Testing ".getBytes());
- System.out.println("Server has responded ");
- String str="";
- while((i=inp.read())!=-1){
- str=str+((char) i);
- System.out.println("USer says "+ str);
- }
- }
- catch(Exception e){
- System.out.println("Error "+e);
- }
- }
- }
Server has responded
USer says null H User says null H null User says null H null e etc etc
I am not supposed to get this null character,why I am getting it?? Another thing, my server is writing to the stream but the client is not able to receive that,why is that?Do I need to use a separate thread for that?
Thanks in adv