Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Socket Problem.

Posted on 2006-11-09
4
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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