473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Socket programming

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
6 3812
"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
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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pyguy2 | last post by:
Issues of socket programming can be wierd, so I'm looking for some comments. In my python books I find exclusive use of socket.close(). From my other readings, I know about a "partial close...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
10
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
0
by: shonen | last post by:
I'm currently attempting to connect to a shoutcast server pull down the information from here and then I'll parse it. I got this working with the httplib, which was great, the problem is I want...
8
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.