File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Sending Text over a Socket. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending Text over a Socket. " Watch "Sending Text over a Socket. " New topic
Author

Sending Text over a Socket.

herman hope
Greenhorn

Joined: Apr 09, 2009
Posts: 1
Hi there! I have been struggling for several days now to resolve an issue I am having with a game I am designing.

Basically I am using sockets to make my game run over the internet, and despite it working, I am have issues with the ReadLine() on my client.

If the Game returns 1 line of text to the client, it is fine. If I try and return multiple lines of text FROM the server, such as..

out.println("Hello there!" + "\n how are you?";


it will only read the first line (Hello There). I have tried looping the Realine() method until it reaches null, and this works. However, when I send the next command to the server, the ReadLine method won't return anything, almost as though its "stopped". I have tried resetting it, looping it, but it just isn't working. Im not 100% sure if this is the right section, as the problem could be to do with my sockets.

Not asking anyone to write this for me, just asking if anyone could help point me in the right direction or explain what's going wrong. Any help is greatly appreciated!


Below is my Client

import java.io.*;
import java.net.*;

public class Client {
public static void main(String[] args) throws IOException {

Socket mySocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
mySocket = new Socket("86.7.165.118",4450);
out = new PrintWriter(mySocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Can't find the Host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the IP");
System.exit(1);
}

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;

while ((userInput = stdIn.readLine()) != null) {
out.println(userInput); //Send data to server
System.out.println(in.readLine()); //Print data received from server
}


}
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

herman hope wrote:it will only read the first line (Hello There).


I'd expect that. You aren't sending a line termination character, so BufferedReader is sitting there waiting for it.

herman hope wrote:
I have tried looping the Realine() method until it reaches null, and this works. However, when I send the next command to the server, the ReadLine method won't return anything, almost as though its "stopped".


Which it has. I believe the only way you'll get null from a BufferedReader is on EOF, which one gets when one closes a socket. You'd have to open the socket connection again to send/receive more data.

Using BufferedReader is a Bad Idea for socket communication because EOL characters are platform dependent.
If you'd rather concentrate on the details of your game rather than developing your own socket-level protocol, I suggest you look at using Remote Method Invocation (RMI) instead. It wraps up all the nasty details of dealing with sockets with a simple method syntax.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
Consider Paul's rocket mass heater.
 
subject: Sending Text over a Socket.
 
Similar Threads
what is the prob !!!
Guys, need help on tcp socket (Urgent)
making Thread of methods
I am getting java.net.UnknownHostException
not able to connect...