i have a client server and i send data perfectly to the client its when the client wants to send or update data which is the problem. what i am trying to do is check to see if the object that is passed to the server has a id and a name and if so then it will be a new "patient" so it needs to save it to the existing database all the data is passed to the server using:

public void newPatient(int Id,String Name,String Address,String TelNumber) throws IOException{
         ObjectOutputStream oos = null;
        try {
            Socket socket = new Socket("localhost", 1234);
             Patient patient = new Patient();
            oos = new ObjectOutputStream(socket.getOutputStream());
            patient.setId(Id);
            patient.setName(Name);
            patient.setAddress(Address);
            patient.setTelNumber(TelNumber);
            oos.writeObject(patient);
        } catch (IOException e) {            
        } finally {
            oos.close();
        }
     }

and this is the code for my server:

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Server extends Thread {

    private ServerSocket server = null;
    private int port = 1234;
    
    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    java.util.Date date = new java.util.Date();
    String datetime = dateFormat.format(date);

    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() {
        System.out.println("****************************************************");
        System.out.println("*                                                  *");
        System.out.println("*                Patient Information               *");
        System.out.println("*                       ©2010                      *");
        System.out.println("*                                                  *");
        System.out.println("*                   Luke Houlahan                  *");
        System.out.println("*                                                  *");
        System.out.println("* Server Online                                    *");
        System.out.println("* Listening On Port " + port + "                           *");
        System.out.println("*                                                  *");
        System.out.println("****************************************************");
        System.out.println("");
        while (true) {
            try {
                listenForConnections();
            } catch (IOException ex) {
            }
        }
    }

    private void listenForConnections() throws IOException {
        Socket socket = server.accept();        
            returnPatient(socket);
    }

    private void returnPatient(Socket socket) throws IOException {
        ObjectOutputStream out = null;
        ObjectInputStream in = null;
        Patient patient = null;
        try {

            in = new ObjectInputStream(socket.getInputStream());
            patient = (Patient) in.readObject();
            if (patient.getName() != null && patient.getId() != 0){
            System.out.println("- [" + datetime + "] Adding New Patient Id: "+patient.getId()+"Name: "+patient.getName());
            }
              else if (patient.getId() != 0) {
                System.out.println("- [" + datetime + "] Request Of ID: " + patient.getId());
            } else if(patient.getName() != null) {
                System.out.println("- [" + datetime + "] Request Of Name: " + patient.getName());
            }
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(readPersons(patient));
        } catch (IOException ex) {           
            System.out.println("- [" + datetime + "] Client Queried Invalid Entry");
        } catch (ClassNotFoundException ex) {
            System.out.println("- [" + datetime + "] Class Not Found Error!");
        } finally {
            in.close();
            out.close();
            socket.close();
        }
    }

    public Object readPersons(Patient patient) throws IOException {
        Patient result = null;
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream("newdata.dat"));
            if (patient.getName() != null && patient.getId() != 0){            
            saveNewPatient(patient.getId(),patient.getName(),patient.getAddress(),patient.getTelNumber());
            }
            if (patient.getId() != 0) {
                while (in != null) {
                    result = (Patient) in.readObject();
                    if (patient.getId() == result.getId()) {
                        return result;
                    }
                }
            }
            if (patient.getName() != null) {
                while (in != null) {
                    result = (Patient) in.readObject();
                    if (patient.getName().equalsIgnoreCase(result.getName())) {
                        return result;
                    }
                }
            }
            in.close();
        } catch (ClassNotFoundException ex) {
            System.out.println("- [" + datetime + "] Class Not Found Error!");
        }
        return result;
    }

    public void saveNewPatient(int Id, String Name, String Address, String TelNumber) throws IOException {
        Patient patient = new Patient();
        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("newdata.dat")));

        patient.setId(Id);
        patient.setName(Name);
        patient.setAddress(Address);
        patient.setTelNumber(TelNumber);

        out.writeObject(patient);
        out.flush();
        out.close();
    }
}

thanks in advanced,
houlahan

Recommended Answers

All 3 Replies

flush the stream, might do a lot of good :)

ok so i got it to save the new "patient" but now it just over rights everything that was in that file i want it to append the new patient to file :/

open the file in the correct mode, place file pointer correctly, and write :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.