aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes sending objects over a connection. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "sending objects over a connection." Watch "sending objects over a connection." New topic
Author

sending objects over a connection.

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
here is my client side object initialization and write.





And here is my serverside code to test to see if a connection
is in fact, an Admin.




i dont know why this approach is not working... if i could get any help,
it would be greatly appreciated.

Thanks,


Justin Fox


You down with OOP? Yeah you know me!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41803
    
  62
First of all, this is a bad idea:

as is this:

How are you going to know about problems if you suppress exceptions?

If at the server you expect a serialized Admin object, then that's what you need to send on the client. You need to use an ObjectOutputStream, not a PrintWriter (which would send admin.toString() over the wire - definitely not what you want.)


Ping & DNS - my free Android networking tools app
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


i changed it to this, and nothing is happening, it prints the first

system.out and then the last nothing in between.


im lost,


Justin Fox
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
ok instead of nesting the two inner try statments, I made them separate like so:



and after it prints "something wrong with objectoutput or input streams"
it hangs up...

lol help me please,

Justin Fox
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
the reason I want to send an object is because i want the admin client side be secure.

I don't want the regular chat client to be able to send '#admin' in text, for example, and have administrative rights.

so when i log in on the adminClient application, it will automatically send an "Administrator' object and that should be pretty clear to the server that this client has more rights (right that i implement ofcourse), because you can't send an object throught typing in a textfield, they have to programmed.

Thanks for helping,

Justin Fox
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41803
    
  62
You're still throwing away the exception information, so you don't know what is going wrong, and in which line. Put an e.printStackTrace() in the catch block to find out.

My guess would be that the admin object is not serializable, since from the look of the constructor it contains a reference to a socket or connection.

You must also make sure that the JVMs on both sides implement the same Java version. If, say, one side is Java 1.4, and the other 5, serialization will most likely not work.

Instead of jumping through all these hoops for achieving a limited degree of security, can't you use a username/password scheme coupled with encryption? That would secure the data transfer, and would ensure that only properly authenticated users could send admin commands.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
the thing is, i have sockets(clients) that are currently connected, stored in a socket array on the server.

i want to retrieve these sockets when i log into the admin application, so im going to have to send objects one way or another.

yea i gave up on the sending an Admin object, just gonna send

"#admin username password" and query the database.

i know you can do Socket.toString(), but can you do the opposite?

Justin Fox
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41803
    
  62
i gave up on the sending an Admin object

There's no reason to give up on it; you'll just have to make it Serializable, which should be no big deal.

i know you can do Socket.toString(), but can you do the opposite?

Not sure what you're asking here. You can't convert a Socket to a string in any meaningful way, and neither can you do the reverse. What are you trying to accomplish?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I don't know, I mean, I've looked at a lot of code, and mine seems to look(syntax wise) the exact same.

you have an ObjectInputStream for reading objects from the sockets InputStream, and an ObjectOutputStream for writing Objects to the sockets
OutputStream.

I dont know what im doing wrong, and in the above code where it just pauses, even if i put an e.printStackTrace(), that wouldn't matter because the code hangs up, i.e never gets to the next try statement.

is it bad to do the following?



versus.



or is it exactly the same?

the problem seems to be when I try the following code.



because when I undo the commented System.out, it will print
'got to objectstream creation', but hang up and never print
'got to end of program'.

thats what im not understanding, its like the program hits an
infinite loop.

Thanks,

Justin Fox
Johnny Kewl
Greenhorn

Joined: Apr 16, 2007
Posts: 8
Hi Justin...
As ULF said... e.getMessage() is also a good thing to look at.

The other thing is for your own sanity separate the serialization from the sockets so you can test the one without the other... whew!

Essentially the socket must just send bytes.
The class you sending must be serializable

And "then" you can manipulate objects.
Why that is important is because OBJECTS are containers, not generic types, they hold stuff.

Inside that object it must be an array of bytes...
I'm betting if you test with this...
if(!obj.getClass().isArray()) return false;

It will be false..... get that right first then go back to sockets.
Long num = 1;
Object o = num;
Is not array of bytes, probably just a ref to an int...

I think your problem is there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

"Johnny Kewl", please check your private messages.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
oh ok, now I understand.

I didn't know why you made classes serializable lol, thanks for clearing that up. I'll do a little example to test it out. Then let you guys know how it came out.

Thanks again,

Justin
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i made my object serializable, and my class constructor for that class excepts only a character.

but i still get the hang up when trying to initialize the objectoutputstream?

help please,

Justin Fox








my class i made serializable and the code to write an object of that type to a sockets output stream.

Thanks again,

Justin Fox
[ November 13, 2007: Message edited by: Justin Fox ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41803
    
  62
What is the error message you're getting?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
im not getting a message, after the 'objectoutputstream' initialization it just hangs up, like its in a infinite loop or something.

Thanks,

Justin fox
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I don't understand what is going on, its hanging up when it hits this block of code.




because when i comment it out, it runs fine.

and even when i put a try block around it like so:



it still hangs up and doesnt print the stacktrace.

if I could get some help please do, I can only send what i know.
I know it may become frustrating, but I have no idea what to do.
it seems my initilization of the object streams is crashing

Justin Fox
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41803
    
  62
Which line causes the hang? Does the object make it to the server? Try flushing the output stream before opening th einput stream.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


ok this code writes to the objectoutputstream just fine, and then i flush(), but when i uncomment the iniitilization of the objectinputstream, it hangs up.

whats the dealio?

Thanks again,

Justin Fox
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
I figured out what was wrong.

the socket i was creating was connecting to the server, so only the server can getInputStream from the connection, not the method in my same class that is on the localhost.

Thanks for all the replies.

I kept seeing people doing ObjectOutput and input streams in the same class, but they were using fileInput and output streams and the parameters.
That's when it dawned on me that only the server to access the sockets inputstream.

But anyways, thanks for all the help, this site is by far the best
forum out on the web.

Justin Fox
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending objects over a connection.