Hello guys,
I want to know if its possible to use the same socket to transfer two things at the same time.

// set up input stream for objects
          input = new BufferedReader(new InputStreamReader(clientsoc.getInputStream()));
          // set up output stream for objects
          output = new PrintWriter(new OutputStreamWriter(clientsoc.getOutputStream()));
          output.println(nickName  + " joined the chat.");
          output.flush();// flush output buffer to send header information

I was developing a chat, now I want to implement the FTP function in it, that is, unable a user to both chat and send a file. What must I do, i have already implemented the chat part, but I am not sure , whether to create a new socket, or new streams or just use the same streams as above.

Thanks for your help.

Recommended Answers

All 20 Replies

Using the same socket wouldn't your chat and file data get interpersed with the application having no way to know whats what ?

If you use ObjectOutputStream and ObjectInputStream you can send anything you want - Strings, complex Objects, whatever (as long as they are serialisable).

Hello,

Yeppp .. you are right .. but I wanted to append something when sending files.

Example:

If the user clicks on the button SEND FILE : append something like $^File^$

and if SEND MESSAGE button

append nothing


What do you suggest me to do. I want my program to very efficient, I want my program to be a bit like MSN, where the user can chat and send files if needed. Thanks for the answer.

Using the same socket wouldn't your chat and file data get interpersed with the application having no way to know whats what ?

Yes. You'ld have to do something like:

out.writeUTF("Chat");
out.writeUTF(message);
out.writeUTF("Data");
out.writeObject(myData);
...
String kind =  in.readUTF();
if (kind.equals("Chat")) {
   message = in.readUTF();
} else { 
   if (kind.equals("Data")) {
      MyDataClass d = (MyDataClass) in.readObject();
   }
}

Couldn't you also use getClass or instanceOf to tell which type of Object it was?

Hello guys,
I am a bit confused, what should I do, use the same socket or create a different, which will be more easier and more efficient.

Btw, what the difference between ObjectInputStream and DataInputStream?

I was using OBJECT before, and a friend of mine told me its better to use DATA, so I just change everything from OBJECT to DATA. Do you think its good what I have done.


@ BJSJC: Its the first I heard of "instanceOf" what does that do?

Thanks for your answers.

I think it would be easier, and more efficient, to use 1 socket.
Data streams transmit the Java primitive types (boolean, int etc).
Object stream transmit any kind of object or primitives, and are thus more general.
You can query the type of object via getClass or instanceOf (Google it), but you're going to declare a variable to refer to it, then do something with it, so you'll probably want to know the object type when you write the code - use different "kind" headers for each type of Object you want to share.

Yep ,thanks for the answer but James, you mean that I should use the OBJECT then?

And how to change the headers, thanks for the answer.

Yes, he means to use the Object instead, because not only can you send and receive primitive types, but you can send and receive Objects. With the Data one, you can only send and receive primitive types.

edit: And I'm not sure what you mean by "how to change the headers"

Yes, I would use an Object stream to mix different data types on one socket.
As for the "headers", my previous post suggested that you should send a keyword at the start of each transmission to identify what kind of content was going to follow - a message, a file or whatever. That's the variable I called "kind". It's up to you how many you have and what they are - it depends on how many kinds of stuff you want to be able to send.

James , I read something about BufferedReader and PrintWriter thats its good to use when dealing with transfer of information as the data is buffered(takes time to travel over network). I don't really know how its works but I wanted to know if I wont have problem later on, when I am chatting with people on the other part of the planet, you see.

Thanks for the answer ... reading on instanceOf, which is very interesting.

Hello ,
I just think of something... If I used your method James

out.writeUTF("Chat");
out.writeUTF(message);
out.writeUTF("Data");
out.writeObject(myData);
...
String kind =  in.readUTF();
if (kind.equals("Chat")) {
   message = in.readUTF();
} else { 
   if (kind.equals("Data")) {
      MyDataClass d = (MyDataClass) in.readObject();
   }
}

Means , when a user will send a big file ... the user will have to wait for the transfer of the file to be completed for him to be able to chat again , as I am using only one stream. Maybe I am wrong!!

Please let me know, before I change everything.

Thanks a lot.

You are correct. . the user will have to wait for the file transfer. You might want to consider putting this on a separate thread.

It will change the whole structure of my program. I don't want to touch the chat again.... its working fine. I just want to implement the FTP part smoothly. Can I use different streams on one socket ? Do you think it will work ?

Like:

output = new ObjectOutputStream( clientsoc.getOutputStream() );
output2 = new ObjectOutputStream( clientsoc.getOutputStream() );

I have not tested it yet, but I think ... both will do the same thing , isn't it? Do you think MSN or any other chat program use only one socket .. or different socket.

(Disclaimer: I'm a bit rusty on Sockets, you may want to wait until James or someone else concurs)

If you use the same Socket, the second thing you are sending will have to wait until the first thing is received. (In other words, only one thing can go over the connection at a time). But even if you use two different Sockets, due to what you mentioned earlier (about waiting for the file transfer), you would still need to use another thread.

PS

I just ran across this java tutorial related to a question you asked in another thread about encryption. . if you have some free time you might want to check it out.
http://java.sun.com/docs/books/tutorial/security/index.html

Yep , I've got your point BJSJC. I am a bit confused, you see, I want to find a good solution, before starting, thanks for the answer dude.

James , I read something about BufferedReader and PrintWriter thats its good to use when dealing with transfer of information as the data is buffered(takes time to travel over network). I don't really know how its works but I wanted to know if I wont have problem later on, when I am chatting with people on the other part of the planet, you see.

Trust me, not relevant in this case. Performance will be limited by the network speed, not by your code.

(Disclaimer: I'm a bit rusty on Sockets, you may want to wait until James or someone else concurs)

If you use the same Socket, the second thing you are sending will have to wait until the first thing is received. (In other words, only one thing can go over the connection at a time). But even if you use two different Sockets, due to what you mentioned earlier (about waiting for the file transfer), you would still need to use another thread.

Concur. Plus you'll still be limited by the network speed.

OK ... I will go for OBJECT then. As you said, I will have to differentiate between the two headers and launch one in another thread... I am working on it.

I will let you know if i am having some problems .

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.