*
The moose likes Sockets and Internet Protocols and the fly likes Unable to send data. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Unable to send data." Watch "Unable to send data." New topic
Author

Unable to send data.

Ann Joyce
Greenhorn

Joined: Jun 10, 2011
Posts: 4
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();


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Welcome to the Ranch!

How are you reading this data on the server?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ann Joyce
Greenhorn

Joined: Jun 10, 2011
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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

Joined: Jun 10, 2011
Posts: 4
thank you, this post can be closed.
 
 
subject: Unable to send data.
 
Similar Threads
About PrintWriter with println and write
simple socket programming
sending an integer to a client machine
Sockets
socket data compression problem