Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

Socket Problem.

I have a small client server type program. On the server side I am implementing a simple
ServerSocket and I am trying to receive a buffer of bytes from the client. But I am receiving
a NullpointerException on the client side when I test this program. I cannot see what the
problem is....

PLEASE NOTE THAT IT IS MY REQUIREMENT THAT I MUST TRANSFER A byte[] FROM THE CLIENT...


Here is the Server.....
import java.io.*;
import java.net.*;

public class ComDataReceiverTest
{  
  public static void main(String[] args)
   {
     System.out.println("CounterServer started.");
 
     try {  
          ServerSocket aServerSocket = new ServerSocket(5000);
           
          for (;;)
          {
         
           Socket aSocket = aServerSocket.accept();

           InetAddress inetAdd = aSocket.getInetAddress();

           String remoteIP = inetAdd.getHostAddress();
           String remoteHostname = inetAdd.getHostName();

           int port = aSocket.getPort();

            System.out.println("Machine at IP: " + remoteIP +
                               ", with hostname: " +
                               remoteHostname + " called at port: " + port);
              //***NOTE I can see thos line is printed on the server side. So the
              //   connection is OK.
              
              
              
              //This is the area I am receiving data buffer from the Client
              DataInputStream in = new DataInputStream(aSocket.getInputStream());
        
              
              if (in== null)
               System.out.println("in Null");
              else
               System.out.println("in Not Null");
              
              //Note : I am getting "in Not Null" printed on the screen. This
              //       tells me that the DataInputStream object (in) is not null
              
              
               in.toString();
              
               byte bytes[] = null;
               int count  = in.read(bytes);
              
            aSocket.close();
           
             }
         } catch (Exception e) {System.out.println("Error: " + e);
           }  System.out.println("CounterServer stopped.");
   }}



and here is the client


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

public class TestClient
{  
  public static void main(String [] args)
  {  
 
   try
   {
     byte[] myBytes = {(byte)5,(byte)6,(byte)3,(byte)2,(byte)1,(byte)4};
     
     Socket t = new Socket("localhost", 5000);          
     DataOutputStream out = new DataOutputStream(t.getOutputStream());
     out.write(myBytes, myBytes.length, 0);
     t.close();
   }
   catch(Exception e)
   {
          System.out.println("Error: " + e);  
   }
  }
 
}


ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

Ok. That error is gone. But  still get the count on the ServersSide as -1.
Why is that the bytes are not tranferred?.
Avatar of prain

ASKER

Ok. I found why. Thanks
:-)