when i run the server and the client i get the following errors:
Client:
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2570)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1314)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
at Client.main(Client.java:29)
Java Result: 1
not really a error but it should receive the patients name?:
waiting for connection...
Message Recieved:null
waiting for connection...
so heres my code:
Server.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server extends Thread {
private ServerSocket server = null;
private int port = 1234;
public static void main(String[] args) {
Server server = new Server();
server.start();
}
public Server() {
try {
server = new ServerSocket(port);
} catch (IOException ex) {
}
}
public void run() {
while (true) {
try {
listenForConnections();
} catch (IOException ex) {
}
}
}
private void listenForConnections() throws IOException {
System.out.println("waiting for connection...");
Socket socket = server.accept();
returnPatient(socket);
}
private void returnPatient(Socket socket) throws IOException {
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
in = new ObjectInputStream(socket.getInputStream());
Patient patient = (Patient) in.readObject();
System.out.println("Message Recieved:" + patient.getName());
out = new ObjectOutputStream(socket.getOutputStream());
Patient patient1 = new Patient();
patient1.setID(1);
patient1.setName("John");
Patient patient2 = new Patient();
patient2.setID(2);
patient2.setName("Luke");
if ("John".equalsIgnoreCase(patient.getName())) {
out.writeObject(patient1);
} else if ("Luke".equalsIgnoreCase(patient.getName())) {
out.writeObject(patient2);
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
} finally{
in.close();
out.close();
socket.close();
}
}
}
Patient.java
import java.io.Serializable;
public class Patient implements Serializable {
int ID;
String name;
public int getID() {
return this.ID;
}
void setID(int ID) {
ID = this.ID;
}
public String getName() {
return this.name;
}
void setName(String string) {
string = this.name;
}
}
Client.java
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Client {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
Socket socket = new Socket("localhost", 1234);
Patient patient = new Patient();
patient.setName("Luke");
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(patient);
ois = new ObjectInputStream(socket.getInputStream());
Patient result = (Patient) ois.readObject();
System.out.println("ID= " + result.getID() + "\nName= " + result.getName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} finally {
ois.close();
oos.close();
}
}
}
i have the patient.java both client and server side and there both exactly the same.
thanks in advance