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.println( e );
return;
}
System.out.println("Indexing Server is running...");
while ( true ) {
try {
System.out.println("Waiting for clients");
Socket sock = sSock.accept();
System.out.println("Client connection accepted");
ObjectInputStream ois = new ObjectInputStream(
sock.getInputStream() );
ObjectOutputStream oos = new ObjectOutputStream(
sock.getOutputStream() );
DataOutputStream dos = new DataOutputStream(
sock.getOutputStream() );
// Read from Client
System.out.println("About to read command from client");
String command = (String)ois.readObject();
System.out.println("Server received client command " + command);
if(command.equals("get(all)"))
{
File file = new File(directory);
if(file.isDirectory())
{
String[] files = file.list();
int numOfFiles = 0;
numOfFiles = files.length;
System.out.println("File 1 is " + files[0]);
dos.writeInt(numOfFiles);
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.println("Share directory not present");
System.exit(1);
}
}
ois.close();
oos.close();
dos.close();
sock.close();
} catch( Exception e ) {
e.getStackTrace();}
// System.err.println( e ); }
}
}
}
Client
-------------------------
import java.net.*;
import java.io.*;
public class Client1 {
public static void main( String[ ] args ) {
if ( args.length < 2 ) {
System.err.println( "Client1 <IP address of server>
<command....>" );
return;
}
// String command = args[1];
try {
Socket sock = new Socket( args[ 0 ], 8951 );
ObjectInputStream ois = new ObjectInputStream(
sock.getInputStream() );
ObjectInputStream ois2 = new ObjectInputStream(
sock.getInputStream() );
ObjectOutputStream oos = new ObjectOutputStream(
sock.getOutputStream() );
DataInputStream dis = new DataInputStream(
sock.getInputStream() );
//Write to Server
String command = args[1];
System.out.println("Sending Command....") ;
oos.writeObject(command);
oos.flush();
// Read from Server
int numOfFiles = 0;
numOfFiles = dis.readInt();
System.out.println("The number of files are " + numOfFiles);
/*
String files[] = new String[numOfFiles];
files[0] = (String)ois.readObject();
System.out.println("Ist file is: " + files[0]);
*/
String[] files = new String[numOfFiles];
int i = 0;
while( i < numOfFiles)
{
files[i] = (String)ois.readObject();
System.out.println("Client has received the following files in
share directory : " + files[i]);
i++;
}
/*
for(i = 0; i < numOfFiles; i++)
{
files[i] = (String)ois.readObject();
System.out.println("Client has received the following files in share
directory : " + files[i]);
}
*/
ois.close();
oos.close();
dis.close();
sock.close();
}
catch( Exception e ) {
e.printStackTrace();}
// System.err.println( e ); }
}
}
"Ryan Stewart" <za****@no.texas.spam.net> wrote in message
news:FN********************@texas.net...
"Al Wilkerson" <ac***@comcast.net> wrote in message
news:bX3Ob.83634$xy6.143558@attbi_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?