Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default Reading inputstream on socket

    I'm having a problem with reading data that is sent to me. I have a wifi module that is reading inverted ASCII character data from a serial device, and then sending that data to my PC. I'm very new to using the socket and serversocket classes, and after some investigation, set the following program up. I can successfully connect to the wifi module, but I want to be able to print out the data that is being sent to my PC from the module. I tried doing this at first with

    in = new BufferedReader(new inputStreamReader(skt.getInputStream()));

    and then System.out.print(in), but this returns undecipherable results, like:

    19821f

    I then tried casting to a string and a character array (as seen below), but then the program would get stuck and not continue after connecting.

    I suspect that it is some sort of simple character formatting problem. Any ideas?

    Here's the code:

    import java.lang.*;
    import java.io.*;
    import java.net.*;
    class TCPServer {
    public static void main(String args[]) {
    String data = "Toobie ornaught toobie";
    BufferedReader in = null;

    try {
    InetAddress ia = InetAddress.getByName("192.168.1.2");
    ServerSocket srvr = new ServerSocket(5000, 50, ia);

    Socket skt = srvr.accept();
    System.out.print("Server has connected!\n");
    PrintWriter out = new PrintWriter(skt.getOutputStream(), true);


    //String uart_data = inputstream.
    in = new BufferedReader(new InputStreamReader(skt.getInputStream()));



    // one attempt that didn't work
    /*
    char[] cbuf = null;
    in.read(cbuf);
    System.out.print(cbuf);
    */


    System.out.print(in + "\n");

    System.out.print("Sending string: '" + data + "'\n");
    out.print(data);
    out.close();
    skt.close();
    srvr.close();
    srvr = null;

    }
    catch(Exception e) {
    System.out.print("Whoops! It didn't work!\n");
    }
    }

    }

  2. #2
    Debbie is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    Try looping on the readLine() method of the bufferedReader.
    E.g.


    Java Code:
    String nextLine;
    
    while((nextLine = in.readLine()) != null) {
     System.out.println(nextLine);
    
    }

Similar Threads

  1. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 07:49 PM
  2. Reading Web Pages with Socket Channels
    By Java Tip in forum java.net
    Replies: 0
    Last Post: 04-07-2008, 09:00 PM
  3. Replies: 0
    Last Post: 11-27-2007, 11:23 AM
  4. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 08:51 PM
  5. Un expected behaviour when reading from inputstream
    By adoorsarath in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2007, 06:02 PM

Tags for this Thread

Posting Permissions

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