473,569 Members | 3,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3817
"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?
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.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

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******@optus net.com.au> wrote in message
news:40******** **************@ news.optusnet.c om.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.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


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******@optus net.com.au> wrote in message
news:40******** *************** @news.optusnet. com.au...
"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******@optus net.com.au> wrote in message
news:40******** **************@ news.optusnet.c om.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.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



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

Jul 17 '05 #6
"Al Wilkerson" <ac***@comcast. net> wrote in message
news:5odOb.7352 7$sv6.156398@at tbi_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 ObjectInputStre am, you'll find that when you create an instance of
an ObjectInputStre am by wrapping it around another InputStream, it blocks
until it receives a header from its corresponding OutputStream. So since you
create the ObjectInputStre am first in both your client and server, they're
both waiting to hear from the other's ObjectOutputStr eam, 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 ObjectInputStre ams 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
ObjectOutputStr eam 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
6330
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 operation". So, I figured it would be useful to post some code about how socket.close() has an implicit send in it and you can actually gain some...
5
3674
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 but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are...
1
3380
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 XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This...
5
11678
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 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine...
2
15312
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 now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with...
10
4014
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 anyone please help me with some sample codings and guidance? Can you also suggest some other news group available for socket programming in VB.Net?
11
8585
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 mechanisms. I use a non-blocking approach for this, using the call to 'poll' to support the async mechanism - rather than the 'begin' and 'end'...
0
1892
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 to use the select statement to do only do this periodically. (I'm trying to be a client, accepting data, that might be my first problem) Basically...
8
4666
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 page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data...
3
2759
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: #include "TCPEchoServer.h" /* TCP echo server includes */ #include <pthread.h /* for POSIX threads */
0
7619
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
950
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.