简体   繁体   中英

not able to send byte array from server to client,able to send from client to server

I have a task to do this. Create a client and server socket interaction which accepts byte data and converts the byte data data received at server in the String and send back the response with the confirmation of the data conversation with success/unsuccess as the data passed will be with fix data length format so the validation should be done at server end.

As for eg

there are fields which ur sending to server like,

field 1 - number field 2 - String field 3 as Floating number ie 108.50

After conversion from byte to String : 152|any msg|108.50

In Byte it will be something like this,

10101|1001010010000000011000000000|1110111011

I have tried the following programs to do this

Server.java

public class Server extends Thread
{
    private ServerSocket serverSocket;

    public Server(int port) throws IOException
    {
        serverSocket = new ServerSocket(port);
        //serverSocket.setSoTimeout(100000);
    }

    public void run()
    {
        while(true)
        {
            try
            {
                Socket server = serverSocket.accept();
                byte Message[]=null;
                DataInputStream in =
                        new DataInputStream(server.getInputStream());

                ByteArrayOutputStream buffer = new ByteArrayOutputStream();

                int nRead;
                byte[] data = new byte[16384];
                while ((nRead = in.read(data, 0, data.length)) != -1) {
                    buffer.write(data, 0, nRead);
                }
                                System.out.println("On this line"); //This doesnt get printed
                buffer.flush();

                data= buffer.toByteArray();

                System.out.println(data);
                String convertmsg = new String(data);
                System.out.println("Msg converted "+convertmsg);
                DataOutputStream out =
                        new DataOutputStream(server.getOutputStream());
                System.out.println("below dataoutputstream");
                out.write("Success".getBytes());
                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;
        try
        {
            Thread t = new Server(port);
            t.start();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

client

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());


                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);


                System.out.println(data);//This line doesnt get printed
                //System.out.println("Server says " + in.readUTF());
                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);
        }
    }

}

If i send data from client to server in bytes,it reads it and prints it.Also then the line "Enter userinput " gets printed and if the user enters '1' the program continues. But the problem is this program given above. If i try to send data from server stating "success"(meaning the data has been converted from bytes to String successfully) then the program stucks and the cursor doesnt go below the line which are in comments "This line doesnt get printed".There is no error printed and none of the program terminates.I am new to socket programming and dont understand much about networking. Any help will be truly appreciated.

You're reading the input until end of stream, but the peer isn't closing the connection, so end of stream never arrives.

I suggest you read and write lines, or use writeUTF() and readUTF().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM