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:
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.
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.
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.
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