• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sending Text over a Socket.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}


}
}

 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic