473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why I am receiving null in my output?

108 100+
I want to send a message to my computer from my phone using TCP..My computer is the server and my phone is the client. I am able to send a message from my phone to my computer but in the output, I get null characters ..

I paste my codes below;;

Client ::

Expand|Select|Wrap|Line Numbers
  1. public void startApp() { try { // establish a socket connection with remote server streamConnection = (StreamConnection) Connector.open(connectString);
  2.  
  3.   // create DataOuputStream on top of the socket connection
  4.   outputStream = streamConnection.openOutputStream();
  5.   dataOutputStream = new DataOutputStream(outputStream);
  6.  
  7.   // send the HTTP request
  8.   dataOutputStream.writeChars("Hello");
  9.   dataOutputStream.flush();
  10.  
  11.   // create DataInputStream on top of the socket connection
  12.   inputStream = streamConnection.openInputStream();
  13.   dataInputStream = new DataInputStream(inputStream);
  14.  
  15.   // retrieve the contents of the requested page from Web server
  16.   String test="";
  17.   int inputChar;
  18.   System.out.println("Entering read...........");
  19.   while ( (inputChar = dataInputStream.read()) != -1) {
  20.      // test=test+((char)inputShar);
  21.     results.append((char) inputChar);
  22.   }
  23.   System.out.println("Leaving read...........");
  24.   // display the page contents on the phone screen
  25.   //System.out.println(" Result are "+results.toString());
  26.   System.out.println("   ");
  27.   resultField = new StringItem(null, results.toString());
  28.   System.out.println("Client says "+resultField);
  29.   resultScreen.append(resultField);
  30.   myDisplay.setCurrent(resultScreen);
  31.  
  32. } catch (IOException e) {
  33.   System.err.println("Exception caught:" + e);
  34. } finally {
  35.   // free up I/O streams and close the socket connection
  36.   try {
  37.     if (dataInputStream != null)
  38.       dataInputStream.close();
  39.   } catch (Exception ignored) {}
  40.   try {
  41.     if (dataOutputStream != null)
  42.       dataOutputStream.close();
  43.   } catch (Exception ignored) {}
  44.   try {
  45.     if (outputStream != null)
  46.       outputStream.close();
  47.   } catch (Exception ignored) {}
  48.   try {
  49.     if (inputStream != null)
  50.       inputStream.close();
  51.   } catch (Exception ignored) {}
  52.   try {
  53.     if (streamConnection != null)
  54.       streamConnection.close();
  55.   } catch (Exception ignored) {}
  56. }
  57.  
  58. }
  59.  
My server :

Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3. /**
  4.  * @param args the command line arguments
  5.  */
  6. public static void main(String[] args)  {
  7.     // TODO code application logic here
  8.   try{
  9.       ServerSocket sck=new ServerSocket(880);
  10.       Socket client=sck.accept();
  11.       InputStream inp= client.getInputStream();
  12.       int  i;
  13.       OutputStream out=client.getOutputStream();
  14.       out.write("Testing ".getBytes());
  15.       System.out.println("Server has responded          ");
  16.       String str="";
  17.       while((i=inp.read())!=-1){
  18.  
  19.           str=str+((char) i);
  20.            System.out.println("USer says "+ str);
  21.       }
  22.  
  23.   }
  24.   catch(Exception e){
  25.       System.out.println("Error "+e);
  26.   }
  27.  
  28. }
  29.  
  30. }
My output for the server ;;

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
Dec 3 '10 #1
2 2294
Have you tried buffers? It's possible your data is being dropped. Also, you could consider switching to the Writer objects if all your sending is Strings.
Dec 6 '10 #2
phpuser123
108 100+
Thanks I already sorted, I used the available() and read(byte[],offset,len) and this solved my problem
Dec 6 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sandie Towers | last post by:
We use a number of similar databases and frequently create a new database using a backup restore of another similar database. We try to keep changes between databases in _Additional tables - like...
7
by: Stewart Gordon | last post by:
Using g++ 3.1, Mac OS X. I'm doing something trying to involve a null output stream, i.e. a stream that just discards what goes into it. The idea is that it gives a simple way of producing...
4
by: Ken Durden | last post by:
I'm looking for a way to simulate the following kind of C code: --- C++ --- void F( double * array, double * fMean, double * fMedian, double * fMax, double * fMin ); --- C# --- void F(...
4
by: Peter Hemmingsen | last post by:
Hi, I have a dotnet object (implemented in mc++ and used in c#) which have a property called "Info". The Info property is also a dotnet object (implemented in mc++). In the constructor of the...
0
by: George Durzi | last post by:
cross posted in datagrid group. Inside <columns/> in my datagrid, I have the following template column <asp:templatecolumn HeaderText="To Be Completed By"> <itemtemplate> <asp:Label...
4
by: Tifer | last post by:
Hello, I'm still new to the whole .Net thing and I'm having a problem with something that should be so simple -- executing a query and returning an output parameter. It's a standard "Add"...
7
by: darthghandi | last post by:
I am having mixed results with asynchronous socket receives. Sometimes I get the right information back from the buffer, other times I get some of the data that should be in the buffer printed out...
6
by: KDawg44 | last post by:
Hi, My responseXML is always null on my AJAX call. When I browse directly to the PHP script I am calling, the XML file shows up just fine. I have read that if a returned XML file is not...
2
by: gabosom | last post by:
Hi! I've been breaking my head trying to get the output variables from my Stored Procedure. This is my SP code CREATE PROCEDURE GetKitchenOrderDetail( @idService int, --outPut Variables ...
0
by: levisvv | last post by:
I am creating an interface program to connect to a Panasonic PBX. I am using VB6 with the Winsock control. I can connect to the PBX's IP address/port OK and it accepts my connection. I can...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.