Witam,
piszę program, który za pomocą serializacji zapisuje wszystkie dane do pliku. Ogólnie serializacja działa dobrze, wszystko zapisuje i wczytuje. Muszę do tego dodać obsługę serwera, który będzie odbierał i wysyłał dany plik tak, by program mógł czytać dane pobierając plik z serwera i zapisywać do pliku, który wyśle na serwer.
Wszystko póki co działa ale jednorazowo tzn. serwer ładnie stoi, uruchamiam program i mogę dodać nowe dane i zapisać do serwera, ale żeby wczytać czy jeszcze raz zapisać muszę na nowo uruchomić "klienta".
serwer
public class Serwer {
public static void main(String[] args){
new Nasluchiwanie().start();
}
}
-----------------------------------------
import java.io.*;
import java.net.*;
public class Nasluchiwanie extends Thread {
final static int port = 9101;
public void run() {
ServerSocket serwerSocket = null;
InetSocketAddress adres = null;
Socket klientSocket = null;
try {
serwerSocket = new ServerSocket();
adres = new InetSocketAddress("127.0.0.1", port);
serwerSocket.bind(adres);
while(true){
try{
klientSocket = serwerSocket.accept();
System.out.println("Połączono z " + klientSocket.getInetAddress());
new Odbieranie(klientSocket).start();
new Wysylanie(klientSocket).start();
new Nasluchiwanie().start();
}catch(IOException e){
System.err.println("Błąd połączenia z klientem");
}
}
} catch (IOException e) {
System.err.println("Błąd bind");
}
}
}
-----------------------------------
import java.io.*;
import java.net.*;
public class Odbieranie extends Thread{
final static String dane = "E:\\dane.dat";
final static int size = 6022386;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Socket socket = null;
int bytesread;
int current = 0;
public Odbieranie(Socket klient){
this.socket = klient;
}
public void run(){
try{
byte[] byteodebrane = new byte[size];
InputStream is = socket.getInputStream();
fos = new FileOutputStream(dane);
bos = new BufferedOutputStream(fos);
bytesread = is.read(byteodebrane, 0, byteodebrane.length);
current = bytesread;
do {
bytesread = is.read(byteodebrane, current, (byteodebrane.length - current));
if (bytesread >= 0) {
current += bytesread;
}
} while (bytesread > -1);
bos.write(byteodebrane, 0, current);
bos.flush();
System.out.println("Odebrałem " + dane + " (" + current + "bytes)");
}catch(IOException e){
}finally{
try{
if (fos != null) fos.close();
if (bos != null) bos.close();
if (socket != null) socket.close();
}catch(IOException e){
}
}
}
}
----------------------------------------------
import java.io.*;
import java.net.*;
public class Wysylanie extends Thread {
final static String dane = "E:\\dane.dat";
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
Socket socket = null;
public Wysylanie(Socket klient) {
this.socket = klient;
}
public void run() {
try {
File plik = new File(dane);
byte[] bytwyslane = new byte[(int) plik.length()];
fis = new FileInputStream(plik);
bis = new BufferedInputStream(fis);
bis.read(bytwyslane, 0, bytwyslane.length);
os = socket.getOutputStream();
os.write(bytwyslane, 0, bytwyslane.length);
os.flush();
System.out.println("Wysłałem " + dane + "(" + bytwyslane.length + "bytes)");
} catch (IOException e) {
System.err.println("Błąd wysyłania");
}finally{
try{
if (bis != null) bis.close();
if (fis != null) fis.close();
if (os != null) os.close();
if (socket != null) socket.close();
}catch(IOException e){
}
}
}
}
akcja przycisku klienta
private void WczytajButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Wczytuje plik z serwera dane.dat, który przy pomocy serializacji
// był wcześniej na serwer zapisany
String dane = "dane.dat";
int size = 6022386;
int bytesread;
int current = 0;
try {
SocketAddress adres = new InetSocketAddress("127.0.0.1", port);
socket.connect(adres);
try {
byte[] byteodebrane = new byte[size];
InputStream is = socket.getInputStream();
fos = new FileOutputStream(dane);
bos = new BufferedOutputStream(fos);
bytesread = is.read(byteodebrane, 0, byteodebrane.length);
current = bytesread;
do {
bytesread = is.read(byteodebrane, current, (byteodebrane.length - current));
if (bytesread >= 0) {
current += bytesread;
}
} while (bytesread > -1);
bos.write(byteodebrane, 0, current);
bos.flush();
System.out.println("Plik " + dane + " pobrałem (" + current + "bytes)");
} finally {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (socket != null) socket.close();
}
} catch (IOException e) {
}
try {
ObjectInputStream wejscie = new ObjectInputStream(new FileInputStream("dane.dat"));
hotel = (InterfejsHotel) wejscie.readObject();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Nie znaleziono pliku!");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Błąd wczytywania pliku!");
} catch (ClassNotFoundException e) {
Logger.getLogger(RezerwacjeUI.class.getName()).log(Level.SEVERE, null, e);
}
displayPersonel();
displayPokoje();
displayKlienci();
------------------------------------------
private void ZapiszButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Za pomocą serializacji, zapisuje dane do pliku dane.dat
// wysyłam dany plik na serwer gdzie będzie przechowywany
try {
FileOutputStream wF = new FileOutputStream("dane.dat");
ObjectOutputStream wO = new ObjectOutputStream(wF);
wO.writeObject(hotel);
wO.close();
wF.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Blad wczytywania pliku!");
}
String dane1 = "dane.dat";
File plik = new File(dane1);
byte[] bytearray = new byte[(int) plik.length()];
try {
SocketAddress adres = new InetSocketAddress("127.0.0.1", port);
socket.connect(adres);
try {
fis = new FileInputStream(plik);
bis = new BufferedInputStream(fis);
bis.read(bytearray, 0, bytearray.length);
os = socket.getOutputStream();
System.out.println("Wysyłam " + dane1 + "(" + bytearray.length + "bytes)");
os.write(bytearray, 0, bytearray.length);
os.flush();
System.out.println("Wysłane");
} catch (IOException e) {
}finally{
if (bis != null) bis.close();
if (fis != null) fis.close();
if (os != null) os.close();
if (socket != null) socket.close();
}
} catch (IOException e) {
System.err.println("Błąd socketu");
}
}