Problem with socket connection
One of my assignments was to create a serial I/O device simulator. I've found bits and pieces of code that should work correctly, but for some reason I'm getting a connection refused exception. Here is the while error:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:179)
at Driver.connect(Driver.java:20)
at SocketUser.run(SocketUser.java:33)
Line 20 of Driver.java is:
socket =new Socket("localhost", 54321);
Line 33 of SocketUser.java is:
connect();
I could post the entire code, but its 6 files big. I tried using a different port, but I still get the error.
Don't call connect() on the socket connection already established by calling a proper Socket constructor:socket = new Socket("localhost", 54321); // 'connect' automatically done
hiwaa at 2007-7-11 22:20:53 >

Alright. That fixed that error. Now I've got this error (I got this error last time as well):
Exception in thread "Thread-2" java.lang.NullPointerException
at Communication.SocketWriter.<init>(SocketWriter.java:26)
at SocketUser.initialize(SocketUser.java:62)
at SocketUser.run(SocketUser.java:35)
Line 26 of SocketWriter.java:
this.out = new ObjectOutputStream(socket.getOutputStream());
Line 62 of SocketUser.java:
writer = new SocketWriter(socket);
Line 35 of SocketUser.jaja:
initialize();
May be 'socket' is null at that code point.Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html
hiwaa at 2007-7-11 22:20:53 >

http://www.plastikhosting.net/uploads/tristanlee85/Sockets.zipThat zip file contains the .java files located in my /src/ directory.I wasn't sure what all code I could give you and still have it compile.
> http://www.plastikhosting.net/uploads/tristanlee85/Soc
> kets.zip
>
> That zip file contains the .java files located in my
> /src/ directory.
>
> I wasn't sure what all code I could give you and
> still have it compile.
Please read my previous reply and take my guess seriously.
If your 'socket' is instantiated and initialized at code point A, A must have been called before actual use of the 'socket'. I firmly believe when you start the thread, the connect() method in which the 'socket' is initialized is never called. Why don't you browse your own code by yourself?
And I'd like to say: stop those brittle design of class hierarchy!
hiwaa at 2007-7-11 22:20:53 >

Here is a sample application, you can use to start checking your code. The code you have provided is something like (Kill a fly with cannon). Try to make it simple, here is a working sample code.
/**
* @author Shahzad Masud
* @date 09-Apr-07
*/
import java.net.*;
import java.io.*;
public class TestSocket {
public static void main(String[] args) throws Exception {
Socket ClientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
char [] buf = new char[1024*6] ;
try {
// -- My Server --
ClientSocket = new Socket("192.168.0.115", 9002);
out = new PrintWriter(ClientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(ClientSocket
.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host:" + e);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: "
+ e);
System.exit(1);
}
String userInput = "Testing";
System.out.println("Sending message:" + userInput);
// -- Sending Response to the server --
out.print(userInput);
out.flush();
// -- Receiving Response from the server --
try {
int len = in.read(buf);
String abc = new String(buf);
abc = abc.substring(0, len);
System.out.println("Response from Server : " + abc);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("->End Time: " + new java.util.Date());
out.close();
in.close();
ClientSocket.close();
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
Please read my previous reply and take my guess seriously. If your 'socket' is instantiated and initialized at code point A, A must have been called before actual use of the 'socket'. I firmly believe when you start the thread, the connect() method in which the 'socket' is initialized is never called. Why don't you browse your own code by yourself?
And I'd like to say: stop those brittle design of class hierarchy!
Well first, I've been looking through this code for a while now. All i know is socket is returning null which is why I'm getting the NullPointerException. Now, for what reason socket is null I do not know. My only guess would be because it isn't creating a connection, but my connection refused error went away so I would assume it's connecting.
Your first error is calling Socket.connect() on already connected Socket. Here's from your first post:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:179)
at Driver.connect(Driver.java:20)
at SocketUser.run(SocketUser.java:33)
Then, your second error, which is a NPE, does arise from the fact that you don't call the code point where the class field 'socket' is initialized. The poor code point is SimpleRobot.connect() method, not Socket.connect(). Don't use misleading name for user class members.
SimpleRobot simplePrinter = new SimpleRobot();
simplePrinter.start();
/* run() method calls initialize() method, which in turn
calls writer = new SocketWriter(socket);
But alas, 'socket' is still null at this code point!
*/
Let me repeat: stop the brittle class hierarchy design.
hiwaa at 2007-7-11 22:20:53 >

