Solved

Socket Problem.

Posted on 2006-11-09
4
254 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
Comment Utility
>>byte bytes[] = null;

should be

byte bytes[] = new byte[256];


or some such
0
 

Author Comment

by:prain
Comment Utility
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
Comment Utility
Ok. I found why. Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 47
array6 challenfge 6 62
Updating Java 9 85
json format text only 4 64
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now