Image

Imagesugarplumkitty wrote in Imagejava_dev 😡frustrated

I'm so frustrated I want to throw my computer across the room.

I'm taking Advanced Java. The assignment I'm battling is to create a ServerSocket and Socket and simulate a change machine.

Following instructions and using the code bits we've been taught and I can find in my Java book, I've designed and created my programs. My Socket program times out when I try to create the connection.

I started off creating the socket and OutputDataStream before the user prompting program ran. When it kept timing out, I thought it might be because I didn't have any data to put into OutputStream at the creation point, so I've changed things around to create/connect the Socket when I've got data to send. moneyInserted is float.


if( keepGoing ) {

System.out.println( "MySocket calling getMoney()" );
moneyInserted = changeMachine.getMoney();
try {

System.out.println("My Socket attempting to create socket");
MyClient = new Socket("192.168.101.1", 1024);
System.out.println("Socket created");

output = new DataOutputStream( MyClient.getOutputStream() );

}
catch (IOException e) {
System.out.println(e);
output = null;
}


I get the same message I got before:

Runtime dialogue
--------------------
MySocket calling getMoney()

Type 1 for Five Dollar bill
Type 2 for One Dollar
Type 3 for Quarter
Type 4 for Dime
Enter money to change: 4

Accepted Dime
My Socket attempting to create socket
java.net.ConnectException: Connection timed out: connect
java.lang.NullPointerException
at MySocket.main(MySocket.java:78)
Exception in thread "main"
Process completed.



My ServerSocket is alive and waiting at the "Trying to accept()" point. It never gets to the debug statement I've got immediately after:

System.out.println( "ServerSocket 1024 created" );
Socket serverSocket = null;
try {
System.out.println( "trying to accept()" );
serverSocket = MyServer.accept();

System.out.println( "ServerSocket 1024 accepted" );


Runtime dialogue
--------------------
ServerSocket 1024 created
trying to accept()


I can find bits and pieces of programs that create these things, but nowhere can I find a complete program that I can use as a model. Does anyone have any hints for me? Is anyone willing to let me send my code to them for analysis and clues?

It's too late to drop this course and the instructor isn't available until Monday afternoon.


edit: Thanks to everyone who offered suggestions. This hurdle has been conquered. On to the next!