Results 1 to 2 of 2
  1. #1
    HermanHope is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default Receiving Text over a Socket (ReadLine() problem)

    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.

    I the Game returns 1 line of text to the client, it is fine. If I try and return multiple lines of text, 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.

    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
    }


    }
    }

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    I am not 100% sure, without running and testing all the code myself, but I think your trouble is in the in.readLine(), because if I am correct, as soon as readLine() hits the \n, it stops reading...
    Who Cares... As Long As It Works...

Similar Threads

  1. [SOLVED] problem in socket connection
    By neeraj.singh in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 02:33 PM
  2. problem in socket connection in sending images
    By vibhor in forum Networking
    Replies: 2
    Last Post: 02-20-2009, 06:39 AM
  3. Replies: 10
    Last Post: 01-17-2009, 09:30 PM
  4. problem with console.readLine()
    By thatguy in forum New To Java
    Replies: 1
    Last Post: 12-14-2008, 08:40 PM
  5. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 07:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •