Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Requesting Java Help (Highly Confused)  (Read 1991 times)

sdmike1

  • Admiral
  • *****
  • Posts: 820
  • Dyslexics of the world, untie!
    • View Profile
Requesting Java Help (Highly Confused)
« on: April 25, 2013, 10:03:53 AM »

I am in a group trying to write a program to that tells you the amount of money you have left in your lunch account.  I am writing the networking code for the program and have run into a strange issue, both ports are connected the int value is sent but the float value is never sent, has anyone run into this before?

In the coarse of debugging it today I found that the server thread appeared to be going back and performing step 1 strangely, it would say:
Code
True: 1
True: 2
(it would print the username here which in my case was 102090)
True: 1
True: 3
True: 4
(if another client connected)
true: 1
true: 2
Server: 102090
true: 3
true: 4
true: 1
(the pattern holds true for a third client as well.)
true: 1
true: 2
Server: 102090
true: 3
true: 4
true: 1
(and a forth)
true: 1
true: 2
Server: 102090
true: 3
true: 4
true: 1


It may help to know that yesterday we ran in to the error, “Software caused connection abort: socket write error” Google searches for this error have given few results. Any and all help is appreciated, i will post the code below.


The client class, this gets the person's student id, sends it to the server and receives the balance as a float.
Code
import java.io.*;
import java.net.*;
import java.util.*;

public class Client
{
public static void main(String[] args)
{
//declares the DataOutputStream
DataOutputStream out;
//declares the ServerSocket
ServerSocket serverSocket = null;
//gets the username
String Username = System.getProperty("user.name");
//parses the username into an int
int StudetIDNumber = Integer.parseInt(Username);
//declares the socket
Socket sock = null;
//dummy data for the client to desplay if the server doesn't send the float
float Balance = 404.0f;

try
{
//open a network socket
sock = new Socket("localhost", 4444);
//create write stream to send information
out=new DataOutputStream(sock.getOutputStream());
//writes the Int to the network stream
out.writeInt(StudetIDNumber);
//for debuging
System.out.println(sock.isConnected() + ": 1"); }
catch (IOException e)
{
//Bail out
e.printStackTrace();
System.out.println("Client: 2");
}
System.out.println(StudetIDNumber);
DataInputStream in;
try
{
//opens a socket
sock = new Socket("localhost", 4444);
//for debuging
System.out.println(sock.isConnected() + ": 2");
//gets the InputStream
in = new DataInputStream(sock.getInputStream());
//For debuging
System.out.println(sock.isConnected() + ": 3");
//Reads the float, the client gets stuck here.
Balance = in.readFloat();
//for debuging
System.out.println(sock.isConnected() + ": 4");
}
catch (IOException e)
{
//She's going to blow *dives for cover*
e.printStackTrace();
System.out.println("Client: 2");
}
System.out.println(Balance);


}
}


This is the server class, it is desgined to accept the connection from the client and spin off a septet thread which then handles the client.
Code
import java.net.*;
import java.io.*;
import java.nio.channels.*;

public class Server
{
    public static void main(String[] args) throws IOException
    {
//begins to declare the ServerSocket object
        ServerSocket serverSocket = null;
        //Is the server lisnting
        boolean listening = true;

        try
       {
//creates the ServerSocket
            serverSocket = new ServerSocket(4444);
        }
        catch (IOException e)
        {
            System.err.println("Could not listen on port: 4444.");
            System.exit(-1);
        }
//the ServerThread runs in this loop while the server is listening
        while (listening)
        new ServerThread(serverSocket.accept()).start();

        serverSocket.close();
    }
}

This is the class which contains the thread which the server will spin off, it takes the connection from the server, receives the information, and sends dummy test information back to the client, in the future it will call a class that will search a .csv file for the appropriate users balance and return that to the thread. The problem is probably in this class.
Code
import java.net.*;
import java.io.*;
import java.util.*;

public class ServerThread extends Thread
{
//Begins to declare the socket
    private Socket socket = null;
//Holds the account balance
float AccoutnBalance = 12.34f;
//Used to hold the ID number
int StudentIdNumber;

    public ServerThread(Socket socket)
    {
     super("ServerThread");
     this.socket = socket;
    }

    public void run()
    {
DataInputStream in;
     try
     {
//opens the DataInputStream
in =new DataInputStream(socket.getInputStream());
//For debuging
System.out.println(socket.isConnected() + ": 1");
//reads the int that the client sends
StudentIdNumber = in.readInt();
//For debuging
System.out.println(socket.isConnected() + ": 2");
//desplays the student id number
System.out.println("Server: " + StudentIdNumber);

     }
     catch (IOException e)
     {
        e.printStackTrace();
        System.out.println("Server: 1");

     }
     DataOutputStream out;
     try
     {
//opens the DataInputStream
out = new DataOutputStream(socket.getOutputStream());
//for debuging
System.out.println(socket.isConnected() + ": 3");
//Sends the float
out.writeFloat(12.34f);
//for debuging
System.out.println(socket.isConnected() + ": 4");

}
catch(IOException e)
{
e.printStackTrace();
System.out.println("Server: 2");
}

}
}

As i said before any and all help is greatly appreciated :)

TJJ

  • Admiral
  • *****
  • Posts: 1905
    • View Profile
Re: Requesting Java Help (Highly Confused)
« Reply #1 on: April 25, 2013, 11:51:10 AM »

Client.java, line 43: is what's causing your problem:

Quote
sock = new Socket("localhost", 4444);

You've already opened the socket further up (line 25); opening up a 2nd is causing a 2nd ServerThread to be spawned and consequently your code isn't doing what you intend it to do.

This is a perfectly example why you should declare local variables as final, so you don't accidentally re-assign them later.
Logged

sdmike1

  • Admiral
  • *****
  • Posts: 820
  • Dyslexics of the world, untie!
    • View Profile
Re: Requesting Java Help (Highly Confused)
« Reply #2 on: April 26, 2013, 08:02:43 AM »

Thanks!, i will report with the results next period when i am in java class :)

sdmike1

  • Admiral
  • *****
  • Posts: 820
  • Dyslexics of the world, untie!
    • View Profile
Re: Requesting Java Help (Highly Confused)
« Reply #3 on: April 26, 2013, 08:53:14 AM »

Thanks! Worked great!