Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: client sometimes send data properly to server and sometimes doesnt send any data

  1. #1
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default client sometimes send data properly to server and sometimes doesnt send any data

    Well first of all let me explain what i want from the code.

    The client sends data to server in bytes which the client converts in String and displays

    Then the server sends data("Success message") to client in bytes which the client converts to String and displays.
    IF the user types '1' then this cycle happens once again.

    The issues i am getting is that the client sometimes sends the data properly to server while at other times it doesnt send any data to server.
    I am really bad at network programming and will truly appreciate if any one helps me with this issue

    Client

    Java Code:
    public class Client {
        public static void main(String args[]) throws IOException
        {
            int userinput =1;
            while(userinput==1)
            {
                String serverName = "192.168.0.8";
                int port = 4003;
                try
                {
                    System.out.println("Connecting to " + serverName
                            + " on port " + port);
                    Socket client = new Socket(serverName, port);
                    System.out.println("Just connected to "
                            + client.getRemoteSocketAddress());
                    OutputStream outToServer = client.getOutputStream();
                    DataOutputStream out =
                            new DataOutputStream(outToServer);
                    System.out.println("above out.wirte()");
                    out.write("any msg".getBytes(),0,"any msg".length());
                    out.flush();
                    outToServer.flush();
                    InputStream inFromServer = client.getInputStream();
                    DataInputStream in =
                            new DataInputStream(inFromServer);
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     
                    int nRead;
                    System.out.println("converting array "+in);
     
                    byte[] data = IOUtils.toByteArray(in);
                    String msgfromserver = new String(data);
                    System.out.println("mstg from server "+msgfromserver);
                    in.close();
                    out.close();
     
                    inFromServer.close();
                    outToServer.close();
     
     
                    client.close();
                }catch(IOException e)
                { 
                    e.printStackTrace();
                }
                System.out.println("Enter userinput ");
                DataInputStream dis = new DataInputStream(System.in);
                String s = dis.readLine();
                userinput = Integer.parseInt(s);
            }
        }
     
    }
    Server

    Java Code:
    public class Server extends Thread
    {
        private ServerSocket serverSocket;
     
        public Server(int port) throws IOException
        {
            serverSocket = new ServerSocket(port);
         }
     
        public void run()
        {
            while(true)
            {
                try
                {
                    System.out.println("Waiting for client on port " +
                            serverSocket.getLocalPort() + "...");
                    Socket server = serverSocket.accept();
                    System.out.println("Just connected to "
                            + server.getRemoteSocketAddress());
                    byte Message[]=null;
                    DataInputStream in =
                            new DataInputStream(server.getInputStream());
     
                    System.out.println(server.getInputStream());
                    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     
                    int nRead=0;
                    byte[] data = new byte[in.available()];
                    System.out.println("outside while loop");
                    in.read(data);
                    System.out.println("completed whie loop");
                    System.out.println(data);
                     
                    String convertmsg = new String(data);
                    System.out.println("Msg converted "+convertmsg);
                         System.out.println("above dataoutputstream");
                    DataOutputStream out =
                            new DataOutputStream(server.getOutputStream());
                    System.out.println("below dataoutputstream");
                    out.write("Success".getBytes(), 0, "Success".length());
                    server.getOutputStream().flush();
                    server.getInputStream().close();
         
                    in.close();
                    out.close();
                    server.close();
                }catch(SocketTimeoutException s)
                {
                    System.out.println("Socket timed out!");
                    break;
                }catch(IOException e)
                {
                    e.printStackTrace();
                    break;
                }
            }
        }
        public static void main(String [] args)
        {
            int port = 4003;// Integer.parseInt(args[0]);
            try
            {
                Thread t = new Server(port);
                t.start();
            }catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    Here is the output i am getting at the server side

    Java Code:
    Waiting for client on port 4003...
    Just connected to /192.168.0.53:50340
    java.net.SocketInputStream@205eb650
    outside while loop
    completed whie loop37
    [B@7e6bc5aa
    Msg converted any msg is very very very 1000 898big
    above dataoutputstream
    below dataoutputstream
    Waiting for client on port 4003...
    Just connected to /192.168.0.53:50382
    java.net.SocketInputStream@5d0769dd
    outside while loop
    completed whie loop0
    [B@11b75be2
    Msg converted 
    above dataoutputstream
    below dataoutputstream
    Waiting for client on port 4003...
    Just connected to /192.168.0.53:50383
    java.net.SocketInputStream@1cf15b84
    outside while loop
    completed whie loop0
    [B@29af45f4
    Msg converted 
    above dataoutputstream
    below dataoutputstream
    Waiting for client on port 4003...
    Just connected to /192.168.0.53:50384
    java.net.SocketInputStream@3219ab8d
    outside while loop
    completed whie loop37
    [B@334dcfad
    Msg converted any msg is very very very 1000 898big
    above dataoutputstream
    below dataoutputstream
    Waiting for client on port 4003...
    Just connected to /192.168.0.53:50385
    java.net.SocketInputStream@397d812b
    outside while loop
    completed whie loop0
    [B@5eab4b89
    Msg converted 
    above dataoutputstream
    below dataoutputstream
    See sometimes the message converted line shows the entire message that has been converted like "Msg converted any msg is very very very 1000 898big" while sometimes there is nothing printed after "Msg converted".
    Last edited by maddy14; 07-10-2014 at 12:52 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: client sometimes send data properly to server and sometimes doesnt send any data

    [B@334dcfad
    That's the String returned by the array's toString() method. Normally that is not too useful
    For debugging use the Arrays class's toString() method for printing arrays:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    gimbal2 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: client sometimes send data properly to server and sometimes doesnt send any data

    But in this case it seems to be a binary data array, so that wouldn't print anything human-readable either. Unless you can read the Matrix.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: client sometimes send data properly to server and sometimes doesnt send any data

    Quote Originally Posted by Norm View Post
    That's the String returned by the array's toString() method. Normally that is not too useful
    For debugging use the Arrays class's toString() method for printing arrays:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    Thanks for your reply.I even tried doing what you said.But the main question is the client is sometimes not sending any data to the server.So the data array (which is the array that stores what the client sends in bytes) prints something like
    data[] data length 0

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: client sometimes send data properly to server and sometimes doesnt send any data

    The code doesn't compile for testing because of missing classes.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    maddy14 is offline Member
    Join Date
    May 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: client sometimes send data properly to server and sometimes doesnt send any data

    Quote Originally Posted by Norm View Post
    The code doesn't compile for testing because of missing classes.
    Thanks for trying my code.THe code must be not compiling because you may not be having IOUtils class.Its a jar file and I have provided the link below
    Commons IO - Download Commons IO

    Please if possible download jar and try my code again.
    I added this if and while statements (in server code) after getting inputstream from client .These statements wait till the server gets data of length >0 from client

    in = new DataInputStream(server.getInputStream());
    if(in.available() ==0)
    {
    while(in.available()==0)
    {
    in = new DataInputStream(server.getInputStream());
    }
    }
    System.out.println("server inputstream available bytes "+server.getInputStream().available());
    This seems to work but that it just a temporary solution and i guess if i go with this solution i may get problems in future

Similar Threads

  1. Replies: 8
    Last Post: 09-24-2010, 04:34 AM
  2. send bytes from client to server
    By 0xHexaDecimal in forum Networking
    Replies: 41
    Last Post: 06-04-2010, 03:17 PM
  3. how to send mp3 file from server to client
    By Jigga008 in forum Networking
    Replies: 0
    Last Post: 12-01-2009, 01:02 PM
  4. send/read int in a client/server app
    By dim_ath in forum New To Java
    Replies: 2
    Last Post: 01-03-2008, 02:03 PM
  5. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 05:05 AM

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
  •