Ok, to be more specific below is the code for my Server and Client for this
simple Indexing File name Server program.
I don't understand why no messages come out when the client connects.
Here is how I run the program : java IndexingServer (start the server) and
java Client1 localhost get(all).
If I comment out the code on each side after reading in "numOfFiles " I get
messages printed out.
But when uncomment the code to read and write the file names no messages are
printed out, thus no files name are being transmitted.
Server
------
import java.net.*;
import java.io.*;
import java.util.*;
public class IndexingServer {
private static final String directory = "share";
public static void main( String[ ] args ) {
ServerSocket sSock = null;
try {
sSock = new ServerSocket( 8951 );
}
catch( IOException e ) {
System.err.prin tln( e );
return;
}
System.out.prin tln("Indexing Server is running...");
while ( true ) {
try {
System.out.prin tln("Waiting for clients");
Socket sock = sSock.accept();
System.out.prin tln("Client connection accepted");
ObjectInputStre am ois = new ObjectInputStre am(
sock.getInputSt ream() );
ObjectOutputStr eam oos = new ObjectOutputStr eam(
sock.getOutputS tream() );
DataOutputStrea m dos = new DataOutputStrea m(
sock.getOutputS tream() );
// Read from Client
System.out.prin tln("About to read command from client");
String command = (String)ois.rea dObject();
System.out.prin tln("Server received client command " + command);
if(command.equa ls("get(all)") )
{
File file = new File(directory) ;
if(file.isDirec tory())
{
String[] files = file.list();
int numOfFiles = 0;
numOfFiles = files.length;
System.out.prin tln("File 1 is " + files[0]);
dos.writeInt(nu mOfFiles);
dos.flush();
int i = 0;
while(i < numOfFiles)
{
oos.writeObject (files[i]);
oos.flush();
i++;
}
/*
for (i=0; i < files.length; i++) {
oos.writeObject (files[i]);
oos.flush();
}
*/
}
else
{
System.err.prin tln("Share directory not present");
System.exit(1);
}
}
ois.close();
oos.close();
dos.close();
sock.close();
} catch( Exception e ) {
e.getStackTrace ();}
// System.err.prin tln( e ); }
}
}
}
Client
-------------------------
import java.net.*;
import java.io.*;
public class Client1 {
public static void main( String[ ] args ) {
if ( args.length < 2 ) {
System.err.prin tln( "Client1 <IP address of server>
<command....> " );
return;
}
// String command = args[1];
try {
Socket sock = new Socket( args[ 0 ], 8951 );
ObjectInputStre am ois = new ObjectInputStre am(
sock.getInputSt ream() );
ObjectInputStre am ois2 = new ObjectInputStre am(
sock.getInputSt ream() );
ObjectOutputStr eam oos = new ObjectOutputStr eam(
sock.getOutputS tream() );
DataInputStream dis = new DataInputStream (
sock.getInputSt ream() );
//Write to Server
String command = args[1];
System.out.prin tln("Sending Command....") ;
oos.writeObject (command);
oos.flush();
// Read from Server
int numOfFiles = 0;
numOfFiles = dis.readInt();
System.out.prin tln("The number of files are " + numOfFiles);
/*
String files[] = new String[numOfFiles];
files[0] = (String)ois.rea dObject();
System.out.prin tln("Ist file is: " + files[0]);
*/
String[] files = new String[numOfFiles];
int i = 0;
while( i < numOfFiles)
{
files[i] = (String)ois.rea dObject();
System.out.prin tln("Client has received the following files in
share directory : " + files[i]);
i++;
}
/*
for(i = 0; i < numOfFiles; i++)
{
files[i] = (String)ois.rea dObject();
System.out.prin tln("Client has received the following files in share
directory : " + files[i]);
}
*/
ois.close();
oos.close();
dis.close();
sock.close();
}
catch( Exception e ) {
e.printStackTra ce();}
// System.err.prin tln( e ); }
}
}
"Ryan Stewart" <za****@no.texa s.spam.net> wrote in message
news:FN******** ************@te xas.net...
"Al Wilkerson" <ac***@comcast. net> wrote in message
news:bX3Ob.8363 4$xy6.143558@at tbi_s02... Is anyone aware of any special things to remember when sending Objects
back in forth between Client and Sever, other than every readObject() should
have a writeObject() and vice versa ?
Does the ordering of statements before the read and write matter?
Thanks,
--
Al
Well, it's generally recommended that the server be listening when the
client tries to connect. Further, it's a good idea to make the connection
before trying to get the I/O streams.
Seriously, though, could you be a little more specific?