Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what streams to use for inputing and outputing an character array over a socket  RSS feed

 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

I was wondering if anyone had any ideas for my problem here.Im wondering what streams to use to read a character array and output a double array.

The context:
1:Client sends an array of characters to server
2:server proceses this array and based on its contents send back another array.
3:the array that the server sends back infact consists of double values not characters.

are there any streams to do such a thing?if this is unclear please feel free to ask for more details.
-thanks
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you are doing something funky. No matter what the value is going across them, sockets talk in bytes. You are interpreting the data as double values. Do you really need character arrays? Sending String objects would vastly simplify things.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey joe,

I am infact sending strings at the moment, was just wondering if it was possible to send character array so that i didnt have to do any parsing of the string on the receiving end.Because right now i am trying to send a "list" of offers to the server simply represented by just chracters.

Say i wanted to send an offer that included name, email, and address all i am doing is sending a string over to the server that would look like this
String offer="n,e,a";

now when the server gets that string im going to have to just use a comma delimeter to get the chracters from it and store them in an array.This is the step i wanted to eliminate...but oh well if it cant be done thats cool.

Same with sending back the double values(which by the way are just probabilities that the server will accept those offers).Right now i havn't done this becuase i was kind of waiting to see if i could send an array across the streams.

I've read some about byteArrayOutput/inputStreams but not really sure how i could do all that...meh
Anymore ideas would still be appreciated
-thanks
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Woah! I was way off base with what you were doing.
ByteArrayInputStream and it's corresponding output stream are used for writing to/from byte arrays. Not going to help you.
You should take some time and look at the Java Tutorial chapter on Input/Output. It will clear some things up as to how IO works in Java.
AFTER you read the tutorial, you should try using an java.io.ObjectInputStream. It and it's corresponding output stream serialize objects (including arrays) into and out of streams. Basically it does what you are doing with the Strings and the parsing for you.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey joe!

its funny i reloaded this page to write back saying that i was going to use ObjectInput/outputStreams instead.This way i can construct an Offer...like ill make a class for and offer that consists of an array among other things iv found i need to include...then just send that offer object.Im prety sure this will be much easier.

I actualy just started thinking of how to implement what im doing like as of yesterday when i first sent that post, so iv had more time to think about it today, and since im going to be sending many offers back and forth from client to server it only makes sense to make an offer class and send the offer objects with the Object streams.

Thanks for the input though, im only coding on paper right now just trying to organzie my thoughts before i dive into actual coding,so when i start that i may run into some problems.

One thing though that i was thinking about and was kinda sketchy to me(becuase im new at socket programing) was that i infact have to communicate with two servers, and i dont know if this is possible, im prety sure it is though as long as each of the servers is on its own(different) port number,
the client should be able to coonect to both--im hoping.

the scheme of the interaction kind of goes like this so far:

1:1st server sends initial offer to client
2:client sends that offer to 2nd server to get probabilities
3:2nd server sends back those probabilities
4:based on those probabilites client sends back an oposing offer to 1st server
5:1st Server does some calculations and sends back counter offer.
6:client recevies offer and *doesnt need to contact 2nd server* does some calculations sends back counter offer.
7:loop 5 and 6 until agreement offer is reached.

I hope i can communicate with both servers haha thats what this is kinda based on.I should be good to go though i imagine...i hope
whats your input?
-thanks, luc
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

as long as each of the servers is on its own(different) port number,
the client should be able to coonect to both


If by "server" you are talking about a piece of software listening to a port rather than a physical machine you are correct. Physical machines will differ in IP address as well.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by "server" you are talking about a piece of software listening to a port rather than a physical machine you are correct.


Yah sorry i didnt mention this...Im just making my on "servers" and running them from my own computer.Before i run my client program i was just going to start the server programs so that they are waiting to accept connections.

oh yah, do i need to implement the serializable interface in order to be able to send custom objects across the streams?(not sure if my terminology is right)
I think i do, and if i do are there any methods that i must include when i implement Serializable?

-thanks
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by luc comeau:

oh yah, do i need to implement the serializable interface in order to be able to send custom objects across the streams?(not sure if my terminology is right)

From the IO Tutorial I pointed you to:

An object is serializable only if its class implements the Serializable interface. . Making instances of your classes serializable is easy. You just add the implements Serializable clause to your class declaration. . . You don't have to write any methods.


I told you to read it first!

[ February 09, 2005: Message edited by: Joe Ess ]
[ February 09, 2005: Message edited by: Joe Ess ]
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
haha so sorry Mr.E ill be sure to do my home work next time , sometimes i ask questions that i know the answers to within five mintues after asking.I was just unsure at that second but my program compiled so i assumed it worked haha, i guess im lazy.Anyways thanks for the reassurance.All your time helping has been much appreciated!
-thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!