Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem using DataInputStream

Status
Not open for further replies.

cRODEEkrank

Technical User
Dec 5, 2001
41
US
Hello,

I'm building a client/server application in which the client sends the contents of a text file containing student grades over to the server. The server is reading with a DataInputStream object using the readInt() method. The problem is, the integers its reading in are different from the grades from the file. Here's part of the code:

DataInputStream din = new DataInputStream
(sock.getInputStream());

while (din.readInt() != -1)
{
System.out.println(din.readInt());
}

Any help would be appreciated!
 
hi!

it looks like you are sending a text file, in order to read a text file you should use a method which read text even if the text is numbers(int) to readInt you should send int.

hope this helps, goodluck!
alon.
 
hi

if u do want to use numbers, then read from the text file, comvert it to integer. something like this

String s;
in s,read the number from text file, then

int a = new Integer(s).intValue();

then try sending it. of as the previous tip suggests, use some method like readUTF() to read it as string, then convert to integer on the server side.

luv
Karthik.

LOL A ship is safe in the harbour, but that's not what it is meant for!!! LOL
 
It's still not working. I would like to use a StreamTokenizer to have the ability to distinguish between text and numbers in the file. However, the data returned from "nextToken()" is garbage and doesn't match the file contents at all.

Here's an example of my Server Code:

ServerSocket port = new ServerSocket(1111);
Socket s = port.accept();
String value;

StreamTokenizer sT = new StreamTokenizer(new BufferedReader(new InputStreamReader(s.getInputStream())));

while (sT.nextToken() != sT.TT_EOF)
{
if (sT.nextToken() == StreamTokenizer.TT_WORD)
{
value = sT.sval;
System.out.println("value is: " + value);
}
}

The value it is returning are numbers which aren't even close to the numbers in the file. Any help would be greatly appreciated!!!

Crod
 
Sounds like you are having encoding issues.

In Java IO there are two fundamental IO types
[1] Input and Output Streams - these are raw byte data
[2] Readers and Writers - these are bridges between byte data and character data via an encoding

I'd recommend using a Reader to decode a byte stream into the character text you want.

Here's how to generally approach (off the top of my head - excuse any typo's that don't compile :) )

InputStreamReader ins;
BufferedReader breader;
try
{
ins = new InputStream (socket.getInputStream()); // use default encoding
breader = new BufferedReader(ins, 128); //128 byte buffer size
String line;
while ((line = breader.readLine()) != null)
{
//Perform StringTokenizer operations here on the 'line' string
StringTokenizer st = new StringTokenizer (line);
// etc...
} catch (IOException ioex)
{
// complain !
}

finally
{
breader.close();
ins.close();
}



RjB.
 
Sorry : told you there would be typos :

ins = new InputStream (socket.getInputStream()); // use default encoding
should be
ins = new InputStreamReader (socket.getInputStream()); // use default encoding
RjB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top