rickyjoepr rickyjoepr - 7 months ago 21
Java Question

How to send double from client to server and back to client using I/O

I cannot use scanners or printwriters

I have a client, and a server.

i run the server and the output is,

Creating Server Socket 5095 . . .
SUCCESS!!!
in while loop
Waiting for connection.


then i run the client and the output is,

Creating Client Socket
SUCCESS!!!
initializing variables
variables flushed
initializing input
java.io.EOFException
at java.io.DataInputStream.readFully(Unknown Source)
at java.io.DataInputStream.readLong(Unknown Source)
at java.io.DataInputStream.readDouble(Unknown Source)
at Client.runClient(Client.java:38)
at Client.main(Client.java:51)


I would like to initialize some doubles in the client, and send them to the server, where they will be parsed and manipulated, with the result then being sent back to the client.

The client fails when I attempt to write the double to the OutPutStream.
What Am I doing wrong?

import java.io.*;
import java.net.*;
import java.util.*;

public class Client {

static void pStr(String p) {
System.out.println(p);
}

static void runClient() {

Socket client;
DataOutputStream output;
DataInputStream input;

try {
pStr("Creating Client Socket ");
client = new Socket("127.0.0.1", 5095);
pStr("SUCCESS!!!");

input = new DataInputStream(client.getInputStream());
output = new DataOutputStream(client.getOutputStream());

System.out.println("initializing variables");
double arg1 = 5;
output.writeDouble(arg1);
output.flush();

double arg2 = 3;
output.writeDouble(arg2);
output.flush();

System.out.println("variables flushed");

System.out.println("initializing input");
// error occurs at next line
double ans = input.readDouble();
System.out.println("input has been read");
System.out.println(ans);

input.close();
output.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// this is second class file
public static void main(String args[]) {
Client.runClient();
}
}
public class SimpleServer {

static void pStr(String p) {
System.out.println(p);
}

static void runServer() {
ServerSocket server;

try {
// Create a ServerSocket.
pStr("Creating Server Socket " + 5095 + " . . . ");
server = new ServerSocket(5095);
pStr("SUCCESS!!!");

while (true) {
System.out.println("in while loop");
Socket connection;
DataOutputStream output = null;
DataInputStream input = null;

pStr("Waiting for connection.");
connection = server.accept();
pStr("Done");

System.out.println("entering try");

try {
// Get input and output streams.
input = (DataInputStream) connection.getInputStream();
output = (DataOutputStream) connection.getOutputStream();

String ansStr;

double arg1, arg2;
try {
System.out.println("int try block");
arg1 = input.readDouble();
arg2 = input.readDouble();

ansStr = "" + arg1 + "," + arg2;
System.out.println(ansStr);

output.writeDouble(arg1);
output.writeDouble(arg2);

} catch (Exception e) {
ansStr = e.getMessage();
}
output.flush();
} catch (Exception e) {
pStr("Error in Protocol: " + e.getMessage());
} finally {
try {
input.close();
} catch (Exception e) {
}
try {
output.close();
} catch (Exception e) {
}
try {
connection.close();
} catch (Exception e) {
}
}
}
} catch (Exception e) {
pStr("Error making ServerSocket");
}
}

public static void main(String args[]) {
runServer();
}
}

EJP EJP
Answer

Your server code is crashing with a ClassCastException which you have unaccountably failed to mention. The connection is therefore being closed without any data being sent, hence the client-side exception.

You cannot get a DataInputStream or DataOutputStream from a socket just by casting. You have to use the same process you're using in the client.

Comments