Results 1 to 2 of 2
  1. #1
    Xx_Amy90 is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default Hey guys would love a little help with sending array via client/server (+Telnet)

    Hey Guys,

    Im having a little problem i have managed to send info from client to server etc... but i want to be able to do it though telnet also (Open it up and say go telnet 127.0.0.1 4444, and then put in like 1 2 3 and then it comes up in the server just like it would if sending via the client. At the moment im getting this error:

    Java Code:
    java.io.StreamCorruptedException: invalid stream header: 310D0A32
    	at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    	at java.io.ObjectInputStream.<init>(Unknown Source)
    	at ConnectionHandler.run(server1.java:73)
    	at java.lang.Thread.run(Unknown Source)
    Let me know if i'm doing anything wrong please:

    My main goal for this is to have it so i can enter say Username, ID and Name and then be able to recall them with a time, Like a very simple check in check out system. Would really love some help <3 :)

    Client:

    Java Code:
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class client1 {
     
        public static void main(String[] args) {
            try {
    // Create a connection to the server socket on the server application
                Socket socket = new Socket("localhost", 7777);
     
    // Send a message to the client application
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                //oos.writeObject("A B C");
                String data[]=new String[3];
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter details for the array");
                for(int x=0;x<3;x++){
                    System.out.print("Enter word number"+(x+1)+":");
                    data[x]=br.readLine();
                }
                oos.writeObject(data);
                System.out.println("Details sent to server...");
                oos.close();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    Server:
    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.lang.ClassNotFoundException;
    import java.lang.Runnable;
    import java.lang.Thread;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
     
    public class server1
    {
        private ServerSocket server;
        private int port = 4444;
     
        public server1()
        {
            try
            {
                server = new ServerSocket(port);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
     
        }
     
        public static void main(String[] args)
        {
            server1 example = new server1();
            example.handleConnection();
        }
     
        public void handleConnection() 
        {
            System.out.println("Waiting for client message got...");
     
    // The server do a loop here to accept all connection initiated by the
     
    // client application.
            while (true) 
            {
                try
                {
                    Socket socket = server.accept();
                    new ConnectionHandler(socket);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
     
    class ConnectionHandler implements Runnable 
    {
        private Socket socket;
        public ConnectionHandler(Socket socket)
        {
            this.socket = socket;
            Thread t = new Thread(this);
            t.start();
        }
        public void run()
        {
            try
            {
    // Read a message sent by client application
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                String message[] = (String[]) ois.readObject();
                //System.out.println("Message Received from client: " + message);
                //b(message);
                            printArray(message);    
                ois.close();
                socket.close();
                System.out.println("Waiting for client message is...");
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            } 
            catch (ClassNotFoundException e) 
            {
                e.printStackTrace();
            }
        }
        private void b(String message) {
             List<String> list = new ArrayList<String>();
                String[] arr = list.toArray(new String[0]);
                System.out.println("Array is " + Arrays.toString(arr));
             
        }
            private void printArray(String[] arr){
               for(String s:arr){
                  System.out.println(s);
                }
        }
    }
    Last edited by Xx_Amy90; 02-04-2013 at 11:19 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Hey guys would love a little help with sending array via client/server (+Telnet)

    java.io.StreamCorruptedException: invalid stream header: 310D0A32
    Why do you think Telnet sends data in Java's object stream format?

    If you look at the data in the error message as hex digits and convert it to a String: "1\r\n2"
    Looking at it as input from the keyboard: 1 (press Enter) 2
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2012, 09:24 AM
  2. Sending file from client to server
    By kaijeong in forum New To Java
    Replies: 50
    Last Post: 07-29-2010, 07:01 PM
  3. sending byte from server to C client
    By rnvrnv in forum Networking
    Replies: 11
    Last Post: 07-02-2010, 03:20 PM
  4. Sending Text File --- Server-To-Client
    By nigamsir in forum Networking
    Replies: 1
    Last Post: 03-08-2010, 03:45 PM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 05:12 AM

Posting Permissions

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