Solved

Socket Problem.

Posted on 2006-11-09
4
284 Views
Last Modified: 2010-03-31
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);  
   }
  }
 
}


0
Comment
Question by:prain
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17908686
>>byte bytes[] = null;

should be

byte bytes[] = new byte[256];


or some such
0
 

Author Comment

by:prain
ID: 17908737
Ok. That error is gone. But  still get the count on the ServersSide as -1.
Why is that the bytes are not tranferred?.
0
 

Author Comment

by:prain
ID: 17908769
Ok. I found why. Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17908779
:-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question