0

I am writing a simple socket program as shown below. I want to send a byte array to a server socket and readi it at the server end. This is the server

package com.java;

//File Name GreetingServer.java

import java.net.*;
import java.io.*;

public class GreetingsServer extends Thread
{
private ServerSocket serverSocket;

public GreetingsServer(int port) throws IOException
{
   serverSocket = new ServerSocket(port);
   serverSocket.setSoTimeout(10000);
}

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());
         DataInputStream in =
               new DataInputStream(server.getInputStream());
         System.out.println("--"+ server.getReceiveBufferSize());
         byte[] b1 = new byte[4] ;
         int i =in.read(b1);
         System.out.println("i = " + i  + "b1 = " +b1);
        /* DataOutputStream out =
              new DataOutputStream(server.getOutputStream());
         out.writeUTF("Thank you for connecting to "
           + server.getLocalSocketAddress() + "\nGoodbye!");*/
         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 = Integer.parseInt(args[0]);
   try
   {
      Thread t = new GreetingsServer(port);
      t.start();
   }catch(IOException e)
   {
      e.printStackTrace();
   }
}
}

This is the client

package com.java;

import java.net.*;
import java.io.*;

public class GreetingClient
{
   public static void main(String [] args)
   {
      String serverName = args[0];
      int port = Integer.parseInt(args[1]);
      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();
         byte[] b = {1,2,3,4};
         DataOutputStream out =
                       new DataOutputStream(outToServer);
         /*BufferedOutputStream out = new BufferedOutputStream(outToServer);*/
         System.out.println("**" + b);  
         out.write(b);
         /*out.writeUTF("Hello from "
                      + client.getLocalSocketAddress());*/

         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }

   }
}

The output I get is this for the server.

Waiting for client on port 6066...
Just connected to /127.0.0.1:52349
--65536
i = 4b1 = [B@48270664
Waiting for client on port 6066...
Socket timed out!

There two things wrong. The first is that the bytearray is not coming entirely to the server. And secondly the server is calling thread twice.

Any input will be helpful. Please let me know if anyone finds a problem!

6
  • 1
    if you change System.out.println("i = " + i + "b1 = " +b1); with System.out.println("i = " + i + "b1 = " +b1.toString()); you'll see your first problem doesn't exists Commented Jan 10, 2014 at 6:36
  • 1
    @wxyz. Yup. The fact that i is 4 is already a good sign... You may need something like Arrays.toString(b1) though. ... + b1) is already the same as ... + b1.toString()) Commented Jan 10, 2014 at 6:40
  • Stupid mistake!!! I am getting the bytearray. Any reason for the two threads executing? Commented Jan 10, 2014 at 6:44
  • 1
    @Mad Physicist, you're right, thanks for clarifying. Also, there are no 2 threads, but a while loop which awaits for connections and runs on timeout after a while...You should create a new thread for each client. Commented Jan 10, 2014 at 6:48
  • 1
    Note for future readers: there was a SocketTimeout due to the timeout on the ServerSocket, which caused the 'Socket timed out!' condition. Commented Jan 10, 2014 at 7:06

1 Answer 1

1

The problem isn't what is being sent/received but rather how you're trying to output it to the console.

Arrays don't override toString(). In your server you have:

System.out.println("i = " + i  + "b1 = " +b1);

The output of b1.toString() is the default from Object.toString() which as noted in the Javdoc for Object is:

a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

You need to use Arrays.toString(b1) to get a String that shows the actual bytes in that array as [ x, x, x ,x ] (where each x is the numeric value of the byte)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.