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...