By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Socket programming

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"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?
Jul 17 '05 #2

P: n/a
readObject and writeObject are used if you are going to use object
serialization for the protocol. Generally, there are four possible options
when developing a protocol, each with its own drawbacks.

Object Serialization.
- Java specific protocol, cannot communicate with other non-Java
applications.
Relatively cumbersome. Difficult to scale.

Raw Text Based Protocol.
- Very cumbersome. Very difficult to scale. This assumes that the protocol
is developed from scratch. If you use a protocol that has been agreed upon
by a governing body, then these statements may not always hold true.

RMI/JRMP
- Java specific. Relatively easy to scale.

RMI/IIOP
- Language independant. Relatively easy to scale.

This list is by no means, a finite list, but the most common strategies
used.

"Does the ordering of statements before the read and write matter?"
Yes, of course.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"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

Jul 17 '05 #3

P: n/a
"This list is by no means, a finite list"

change to

"This list is a finite list"

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"Tony Morris" <di******@optusnet.com.au> wrote in message
news:40**********************@news.optusnet.com.au ...
readObject and writeObject are used if you are going to use object
serialization for the protocol. Generally, there are four possible options when developing a protocol, each with its own drawbacks.

Object Serialization.
- Java specific protocol, cannot communicate with other non-Java
applications.
Relatively cumbersome. Difficult to scale.

Raw Text Based Protocol.
- Very cumbersome. Very difficult to scale. This assumes that the protocol
is developed from scratch. If you use a protocol that has been agreed upon
by a governing body, then these statements may not always hold true.

RMI/JRMP
- Java specific. Relatively easy to scale.

RMI/IIOP
- Language independant. Relatively easy to scale.

This list is by no means, a finite list, but the most common strategies
used.

"Does the ordering of statements before the read and write matter?"
Yes, of course.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"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


Jul 17 '05 #4

P: n/a
except the other way around.
</confusion>

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"Tony Morris" <di******@optusnet.com.au> wrote in message
news:40***********************@news.optusnet.com.a u...
"This list is by no means, a finite list"

change to

"This list is a finite list"

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"Tony Morris" <di******@optusnet.com.au> wrote in message
news:40**********************@news.optusnet.com.au ...
readObject and writeObject are used if you are going to use object
serialization for the protocol. Generally, there are four possible

options
when developing a protocol, each with its own drawbacks.

Object Serialization.
- Java specific protocol, cannot communicate with other non-Java
applications.
Relatively cumbersome. Difficult to scale.

Raw Text Based Protocol.
- Very cumbersome. Very difficult to scale. This assumes that the protocol is developed from scratch. If you use a protocol that has been agreed upon by a governing body, then these statements may not always hold true.

RMI/JRMP
- Java specific. Relatively easy to scale.

RMI/IIOP
- Language independant. Relatively easy to scale.

This list is by no means, a finite list, but the most common strategies
used.

"Does the ordering of statements before the read and write matter?"
Yes, of course.

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
"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



Jul 17 '05 #5

P: n/a
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?

Jul 17 '05 #6

P: n/a
"Al Wilkerson" <ac***@comcast.net> wrote in message
news:5odOb.73527$sv6.156398@attbi_s52...
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.


I ran into this exact problem just the other day. If you read the fine print
on your ObjectInputStream, you'll find that when you create an instance of
an ObjectInputStream by wrapping it around another InputStream, it blocks
until it receives a header from its corresponding OutputStream. So since you
create the ObjectInputStream first in both your client and server, they're
both waiting to hear from the other's ObjectOutputStream, which will never
be created. Therefore, the first order of business is to switch those
statements around to create your output streams before your input streams.
Next, why do you make two ObjectInputStreams in the client? You have ois and
ois2. I'm guessing you were just using it for debugging or something. If you
leave it there, though, the client is still not going to go anywhere,
because it will be looking for another header sent from an
ObjectOutputStream that doesn't exist. To have two inputs on the client
you'd need two outputs on the server. I can't see any reason to do that
though. At least not within the same thread. Other than that, it works fine
for me, assuming your share directory exists.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.