[ range @ 18.06.2002. 11:26 ] @
Imam klijenta koji salje objekat Konto serveru, koji ga deserijalizuje i
smesta u bazu. Klijent ima metode listenSocket(), kojim otvara Socket na
nekom portu, i metod sendKonto koji za parametre ima socket preko koga se
salje i objekat Konto koji se salje. Server ima isto metod listenSocket()
koji ima while(true) petlju u kojoj osluskuje klijente, deserijalizuje
objekte, i smesta podatke u bazu.Problem je u sledecem: samo jednaput ja
mogu da unesem podatke u bazu, posle tog jednog puta server pada uz
exception java.net.SocketException: Socket is closed, a ja stvarno nigde ne
zatvaram socket.Jel zna neko sta bi to moglo da bude?
Evo koda:
klijent :

public void listenSocket(){
ObjectOutputStream outputKonto = null;


socket = new Socket("HOST-NAME", 4444);
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
outputKonto = new ObjectOutputStream(socket.getOutputStream());
System.out.println("Connected!!!!!!! ");
}
catch (UnknownHostException e) {
System.out.println("Unknown host: ");
System.exit(1);
}
catch (IOException e) {
System.out.println("No I/O");
e.printStackTrace();
System.exit(1);
}
}
protected void sendKontoToServer(){
theKonto = this.getKonto1();
try{
socket = this.getSocket();
sendKonto(socket, theKonto);
}
catch (Exception e) {
System.out.println("Unknown host: ");
e.printStackTrace();
System.exit(1);
}

}

protected void sendKonto(Socket socket, Konto theKonto){
ObjectOutputStream outputToServer = null;
try{
outputToServer = new ObjectOutputStream(socket.getOutputStream());
outputToServer.writeObject(theKonto);
outputToServer.flush();
outputToServer.close();

}
catch (IOException e){
e.printStackTrace();
}
}
server:
public void listenSocket(){
ObjectInputStream inputKonto = null;
Konto aKonto = null;
try{
server = new ServerSocket(4444);
client = server.accept();
System.out.println("Opened socket!");
}
catch(IOException ioE){
System.out.println("Could not listen on port 4444");
ioE.printStackTrace();
System.exit(-1);
}
try{
System.out.println("3");
inputKonto = new ObjectInputStream(client.getInputStream());
out = new PrintWriter(client.getOutputStream(), true);
}
catch(IOException ioE){
System.out.println("Accepting connection on port 4444failed!2");
ioE.printStackTrace();
System.exit(-1);
}
while(true){
try{
inputKonto = new ObjectInputStream(client.getInputStream());
out = new PrintWriter(client.getOutputStream(), true);
aKonto = (Konto)inputKonto.readObject();
inputKonto.close();
access.insertKonto(aKonto);


}
catch(Exception E){
System.out.println("Reading failed!" + E.getMessage());
E.printStackTrace();
System.exit(-1);
}
}
}




[ weB_KiLeR @ 18.06.2002. 18:01 ] @
Pazi postoji dosta problema zasto bi pucala veza na serveru...
Probaj da uradis u odvojenim thread-ovima komuniciranje client-server ako client ima user interface moras da mu ubacis thread-ove jer ako to nemas onda sve ode u k...
Tj ili pukne server ili client skroz usmercuje ...
Baci mi source celog progyja pa cu da probam da ga nateram da radi...
E da evo ti jedan kod koji bi mogao da ti pomogne...
Citat:

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


public class Server {

Vector knjiga;
ServerSocket s;
Socket cSocket;
File database;
int port;

public static void main(String[] args) throws IOException
{
Server myServer = new Server(1234);
while (true) {
myServer.getClientConnection();
}
}

private void saveDB() throws IOException
{
PrintWriter DBWriter = new PrintWriter(new BufferedWriter(new FileWriter(database)));
for (Enumeration e = AddressBook.elements(); e.hasMoreElements();) {
DBWriter.println((String)e.nextElement());
}
DBWriter.close();
}

private String processCommand (String commandLine) throws IOException
{
String command = null;
String argument = null;
String record;
String output="";
int space;

//client's command parsing
commandLine = commandLine.trim();
commandLine = commandLine.toUpperCase();
if ((space = commandLine.indexOf(' '))==-1)
command=commandLine;
else {
command = commandLine.substring (0,space);
argument = commandLine.substring (space, commandLine.length()).trim();
}

if (command.equals("LIST")) {
output = "\nListing records:\n";
for (Enumeration e = knjiga.elements(); e.hasMoreElements();)
output = output + (String)e.nextElement() + '\n';
output = output + "\n\t"+ knjiga.size() + " element(s) found\n";
}
else if (command.equals("SEARCH")) {
int count = 0;
output = "\nSearching for \"" + argument + "\".\n";
for (Enumeration e = knjiga.elements(); e.hasMoreElements();) {
record = (String)e.nextElement();
if (record.toUpperCase().indexOf(argument)!=-1) {
output = output + record + '\n';
count++;
}
}
output = output + "\n\t"+ count + " element(s) found\n";
}
else if (command.equals("STORE")) {
knjiga.addElement(argument);
output = "\nRecord added: " + argument + "\n";
}
else if (command.equals("SAVE")) {
saveDB();
output = "\nAddress Book saved\n";
}
else output = "\nUnknown command\n";
return output;
}

private void getClientConnection ()
{
String command = null;
PrintWriter out = null;
BufferedReader in = null;

try {
// Waits for clients' connection requests
cSocket = sSocket.accept();

// Gets input/output streams
out = new PrintWriter(cSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));

// Hello client
out.println ("\nWelcome to AddressBook network application.");

// Waits for client's command
while ((command = in.readLine()) != null) {

//if client says "Bye" the connection is closed
if (command.equals("bye")) {
out.println ("Have a nice time.\n");
// Closes the connection
out.close();
in.close();
cSocket.close();
return;
}

// Answers to client command
out.println (processCommand (command));
}

} catch (IOException e) {
System.err.println("Client connection closed.");
return;
}

}

public Server (int port)
{
AddressBook = new Vector(50,25);
sSocket = null;
cSocket = null;
this.port=port;
DBFile = new File ("abook.dat");

// Reads records form file add fills in AddressBook
// If no database file esists creates a new one
try {
if (DBFile.exists()) {
BufferedReader fileReader = null;
String record = null;
fileReader = new BufferedReader(new FileReader(DBFile));
while ((record = fileReader.readLine()) != null)
AddressBook.addElement(record);
fileReader.close();
} else {
PrintWriter newDBFile = new PrintWriter(new BufferedWriter(new FileWriter(DBFile)));
newDBFile.close();
}
} catch (FileNotFoundException e) {
System.exit(1);
} catch (IOException e) {
System.err.println ("Errore nell'apertura del file archivio");
System.exit(1);
}

// Initialise ServerSocket
try {
sSocket = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Cannot listen on port: " + String.valueOf(port));
System.exit(1);
}

}
}


O thread-ovima mozes naci na adresi
http://www.javaworld.com/javaw...-05-2002/jw-0503-java101.html?
Ovde imas jedan mali tutorial o tome ako jos imas problema mogu ti dati moje projekte gde to imas...
[ range @ 18.06.2002. 21:28 ] @
Hvala ti na odgovoru, i bio si totalno upravu. Samo sto sam ja to otkrio koji sat pre tvog posta, ali u svakom slucaju hvala ti. Za pocetak sam hteo da mi ova aplikacija radi za single usera, pa i nisam hteo da mi server extends Thread ili implements Runnable. Medjutim cim sam uterao Server u thread stvar je proradila. Stvar je u tome sto threadovi su mi stvar sama po sebi za izbegavanje i to u sirokom luku, izmedju ostalog zato sto jedini projekat koji nisam zavrsio bio u vezi sa njima (chat koji je trebao da podrzi ICQ, Messangera i Jabbera). Ako te interesuje mogu da ti posaljem kod da vidis kako izgleda sada.
pozdrav Sreten
ps. Ovo je ona aplikacija za racunovodstvo i videcemo koliko su sve neverne tome ovde bili u pravu kada su pricali da je java nezgodna za to
[ Ivan Tanasic @ 18.06.2002. 21:42 ] @
Hmm, da se nadovezem na ovaj poslednju post... cinimi se da niko nije sporio da java moze sasvim lepo da odradi posao.. tj vecina je sporila samo GUI koji je zaista spor... Java je veoma dobar i 'lep' :P jezik i samim tim, ovo se u javi moze veoma lepo uraditi, ali i dalje ostaje ono parcenjce koje strci.. gui ;))
[ weB_KiLeR @ 21.06.2002. 22:46 ] @
Lepo sam ti rekao bez thread-ova nema nista najbolje ti je da napravis jednu klasu koja obavlja konekciju i koja je je nabudzena thread-ovima i onda samo pozivas funkcije iz te class-e :)