prain
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("Counte rServer 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("Machin e 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.ge tInputStre am());
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("Counte rServer 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,(by te)1,(byte )4};
Socket t = new Socket("localhost", 5000);
DataOutputStream out = new DataOutputStream(t.getOutp utStream() );
out.write(myBytes, myBytes.length, 0);
t.close();
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
}
}
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("Counte
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("Machin
", 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.ge
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
} System.out.println("Counte
}}
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,(
Socket t = new Socket("localhost", 5000);
DataOutputStream out = new DataOutputStream(t.getOutp
out.write(myBytes, myBytes.length, 0);
t.close();
}
catch(Exception e)
{
System.out.println("Error:
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. I found why. Thanks
:-)
ASKER
Why is that the bytes are not tranferred?.