Solved

Socket Problem.

Posted on 2006-11-09
4
273 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
recursion example 16 126
Cipher Configuration on Apache HTTPD 4 62
split string containing \r\n in Java 46 43
Groovy problem when using SOAPUI : DispatchException occurred 7 44
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

770 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