synchronization of database

My assessment is to write a Client - Server application that use SQL database. A copy of database should be on both Client and Server side. While there are many Clients there is only one Server. Clients database entries change many times a day and every Client is about to synchronize with the database on the server. Synchronization is in the following form: 2 tables (with orders and orders details) are sent from Client do Server. 5 tables (with informations about products, prices, clients etc.) are sent from Server to Client.

I have already created the whole local side of the program (Client Side) but as I am new to Sockets I have a great problem with synchronization.

I implemented synchronization in the following way:

Client Size:

[code]

SocketConnection soc = (SocketConnection) Connector.open("socket://127.0.0.1:9988");

OutputStream osZ = soc.openOutputStream();

Vector worekZ = dbm.serializeZamowienia();

//serialized whole table from database

for(int i = 0; i < worekZ.size(); i++){

osZ.write((byte[]) worekZ.elementAt(i));

osZ.write(128);//rows separator

}

osZ.close();

soc = (SocketConnection) Connector.open("socket://127.0.0.1:9988");

OutputStream osZS = soc.openOutputStream();

Vector worekZS = dbm.serializeZamSzczegoly();

for(int i = 0; i < worekZS.size(); i++){

osZS.write((byte[]) worekZS.elementAt(i));

osZS.write(128);

}

osZS.close();

//so on with other tables that are about to send...

soc.close();

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SocketConnection soc = (SocketConnection) Connector.open("socket://127.0.0.1:9988");

InputStream isC = soc.openInputStream();

int ch = 0;

int i = 0;

byte[] temp = new byte[1000];

Vector worek = new Vector();

while(true){

ch = isC.read();

if(ch == -1){

// -1 end of table (and also end of InputStream as each table is in separate IS)

break;

}

if(ch == 128){ // separator detected - go to next row

worek.addElement(temp);

String str = new String();

}

temp = (byte) ch;

i++;

}

dbm.deserializeCennik(worek); worek.removeAllElements();

isC.close();

soc.close();

soc = (SocketConnection) Connector.open("socket://127.0.0.1:9988");

InputStream isCK = soc.openInputStream();

while(true){

ch = isCK.read();

if(ch == -1){

break;

}

if(ch == 128){

worek.addElement(temp);

}

temp = (byte) ch;

i++;

}

dbm.deserializeCennikKlienci(worek);worek.removeAllElements();

isCK.close();

soc.close();

//and so on with other tables that are about to be received from server

[2912 byte] By [staneqa] at [2007-11-26 17:48:10]
# 1

On the server side there is something like this. Note that number of send and received (serialized / deserialized) tables matches.

My problem is - please help me correct it to make it running.

Is the idea ok? Code compiles but it does not synchronize the database. It does nothing - difficult to diagnose.

public class ServerMobile {

private static Vector worek = new Vector();

private static SManager sm = new SManager();

private Socket clientSocket;

public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;

try {

serverSocket = new ServerSocket(9988);

} catch (IOException e) {

e.printStackTrace();

e.getMessage();

System.exit(1);

}

Socket clientSocket = null;

try {

clientSocket = serverSocket.accept();

} catch (IOException e) {

e.printStackTrace();

e.getMessage();

System.exit(1);

}

int ch = 0;

int i = 0;

byte[] temp = new byte[5000];

InputStream isZ = clientSocket.getInputStream();

while(true){

ch = isZ.read();

if(ch == -1){ // -1 koniec tabeli (Vectora)

break;

}

if(ch == 128){

worek.addElement(temp);

}

temp[i] = (byte) ch;

i++;

}

sm.deserializeZamowienia(worek);

worek.removeAllElements();

isZ.close();

clientSocket = serverSocket.accept();

ch = 0;

i = 0;

InputStream isZS = clientSocket.getInputStream();

while(true){

ch = isZS.read();

if(ch == -1){

break;

}

if(ch == 128){

worek.addElement(temp);

}

temp[i] = (byte) ch;

i++;

}

sm.deserializeZamSzczegoly(worek);

worek.removeAllElements();

isZS.close();

////////////////////////// EXPORT //////////////////////////////////

clientSocket = serverSocket.accept();

OutputStream osC = clientSocket.getOutputStream();

worek = sm.serializeCennik();

for(i = 0; i < worek.size(); i++){

osC.write((byte[]) worek.elementAt(i));

osC.write(128);

}

worek.removeAllElements();

osC.close();

clientSocket = serverSocket.accept();

OutputStream osCK = clientSocket.getOutputStream();

worek = sm.serializeCennikKlienci();

for(i = 0; i < worek.size(); i++){

osCK.write((byte[]) worek.elementAt(i));

osCK.write(128);

}

worek.removeAllElements();

osCK.close();

clientSocket = serverSocket.accept();

OutputStream osK = clientSocket.getOutputStream();

worek = sm.serializeKlienci();

for(i = 0; i < worek.size(); i++){

osK.write((byte[]) worek.elementAt(i));

osK.write(128);

}

worek.removeAllElements();

osK.close();

clientSocket = serverSocket.accept();

OutputStream osT = clientSocket.getOutputStream();

worek = sm.serializeTowary();

for(i = 0; i < worek.size(); i++){

osT.write((byte[]) worek.elementAt(i));

osT.write(128);

}

worek.removeAllElements();

osT.close();

clientSocket = serverSocket.accept();

OutputStream osU = clientSocket.getOutputStream();

worek = sm.serializeUsers();

for(i = 0; i < worek.size(); i++){

osU.write((byte[]) worek.elementAt(i));

osU.write(128);

}

worek.removeAllElements();

osU.close();

clientSocket.close();

serverSocket.close();

staneqa at 2007-7-9 5:00:30 > top of Java-index,Core,Core APIs...