• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to send data.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am sending data over a tcp connection but do not see it getting to the server.
I can pull data off the server and dont see any issues.

I have tried 3 or 4 ways of writing this ..

// BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// wr.write("test");
// wr.flush();



// PrintStream printStream = new PrintStream(socket.getOutputStream());
// printStream.print("test");
// printStream.flush( );

// PrintWriter out = new PrintWriter(socket.getOutputStream());
// out.println("test");
// out.flush();
// out.close();
//

// DataOutputStream output= new DataOutputStream(socket.getOutputStream()) ;
// output.writeChars("test");
// output.flush();


// DataOutputStream output = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
// output.writeUTF("test");
// output.flush();


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

How are you reading this data on the server?
 
Ann Joyce
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your reply..

I am receiving it as
while (true) {
if(in.readLine() != null)

{
System.out.println(in.readLine()); // Read one line and output it
}
}

Does it send any special character when its sends it accross i,e new line ?etc
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be able to use readLine(), the sending code must send a line break. Only the third attempt (with out.println) does that. But you shouldn't use println; the line break symbol on the client and server may be different. Instead, use an explicit \n or \r\n. Although with readLine() it doesn't really matter as it can handle both.

Another thing: readLine() consumes the line it reads. You'll need to store that in a variable. Modifying your little code snippet you get this:
Another way of doing this is one you will see often in reading code - the read-assign-compare way:
And can you please UseCodeTags in the future? That'll make your code look a lot better. Just take a look at my code snippets.
 
Ann Joyce
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you, this post can be closed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic