Results 1 to 3 of 3
  1. #1
    Chrillz is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default TCP/IP client not working properly

    Hi! I have written code for a TCP/ip client that is supposed to connect to a server and receive some data. This works successfully when I connect to a TCP server in Java, but when I try to connect to a TCP server that consists of an embedded system I get the following message:

    "java.io.StreamCorruptedException: invalid stream header: 30313233"

    30 31 32 33 is the hexadecimal representation of the data that the server sends to connecting clients, so something is working, but why do I get this Exception?

    the code for the client is here:
    Java Code:
    import java.net.*;
    
    import java.io.*;
    
    public class TCPClientD {
    
        private String serverIP;
        private int serverPort;
        String fail;
    
        public TCPClientD(String serverIP, int serverPort) {
            this.serverIP = serverIP;
            this.serverPort = serverPort;
            Thread thread = new Thread(new ConnectAndListenToServer());
            thread.start();
        }
    
        private class ConnectAndListenToServer implements Runnable {
    
            public void run() {
                try {
                    Socket socket = new Socket(
                            InetAddress.getByName(serverIP),
                            serverPort);
    
                    ObjectInputStream input = new ObjectInputStream(
                            socket.getInputStream());
                    String message;
                 
                    while (true) {
    
                        message = input.readUTF();
                        System.out.println(message);
    
                    }
                } catch (IOException e) {
                    fail = e.getMessage();
                    System.out.println(e);
                    System.out.println(fail);
                }
            }
        }
    
        public static void main(String[] args) {
            new TCPClientD("192.168.0.115", 5555);
    
        }
    }
    Thankful for answers! / Christian

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default

    This is sort of a guess, but I may have run into the opposite problem recently. I was screwing around with my own InputStream extension (connected to a Java server) and I was getting some gibberish before my data. I soon discovered that the gibberish was actually the Byte Order Mark.

    Perhaps your InputStream is expecting a BOM before the data, and throwing an exception because it's not seeing one.
    Last edited by kjkrum; 07-20-2011 at 01:53 AM.
    Get in the habit of using standard Java naming conventions!

  3. #3
    Chrillz is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    I googled the message and found a thread describing the same problem, turns out the solution is to use the DataInputStream instead of ObjectInputStream.. Thanks for the reply!

Similar Threads

  1. Why this block is not working properly?
    By juhiswt in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 11:08 AM
  2. date is not working properly
    By newnewgen in forum New To Java
    Replies: 1
    Last Post: 10-12-2010, 09:04 AM
  3. event handler not working properly
    By H3rtaherta in forum Java 2D
    Replies: 3
    Last Post: 11-24-2008, 02:39 AM
  4. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 05:48 PM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 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
  •