8 Replies - 7016 Views - Last Post: 20 April 2013 - 09:11 PM Rate Topic: -----

#1 moh_tmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

File Transfer in A simple Chat program with Client/Server

Posted 19 November 2012 - 12:03 PM

I use the code in
http://www.dreaminco...r-gui-optional/
and divide it into tow projects

project 1 contain
  • ChatMessage
  • Client
  • ClientGUI

project 2 contain
  • ChatMessage
  • Server
  • ServerGUI


then add button to client
private JButton login, logout, whoIsIn, SendFile;

then add it to the client frame

I want when hit this button the client browse for some file in the computer the send it
It is not matter to send it to single user or all user
just want the file to be received correctle
Sorry for length
Sorry for bad english

Thanks to everybody

Is This A Good Question/Topic? 0
  • +

Replies To: File Transfer in A simple Chat program with Client/Server

#2 moh_tmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

Re: File Transfer in A simple Chat program with Client/Server

Posted 19 November 2012 - 12:43 PM

I use the code in
A simple Chat program with Client/Server

I split the code into tow projects
project 1 contain
  • ChatMessage
  • Server
  • ServerGUI


Project 2 Contain
  • ChatMessage
  • Client
  • ClientGUI


Then add a new button to lient
private JButton SendFile;
SendFile = new JButton("Send File");
...
southPanel.add(SendFile);



Now i want when hit this button the client browse for some file then send it to others
it is not matter if the same client receive the file
just want it to be received correctle



Sorry for length
Sorry for bad English
Thanks to everybody
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: File Transfer in A simple Chat program with Client/Server

Posted 19 November 2012 - 01:04 PM

So modify the ChatMessage class to be

public class ChatMessage implements Serializable {

	protected static final long serialVersionUID = 1112122200L;

	// The different types of message sent by the Client
	// WHOISIN to receive the list of the users connected
	// MESSAGE an ordinary message
	// LOGOUT to disconnect from the Server
	// SENDING_FILE to send a file
	static final int WHOISIN = 0, MESSAGE = 1, LOGOUT = 2;
	static final int SENDING_FILE = 3;  // <-----------------
        
	private int type;
	private String message;
        private byte[] file;                // <-----------------
	
	// constructor
	ChatMessage(int type, String message) {
		this.type = type;
		this.message = message;
	}
	
        // constructor for file transfer <--------------------
        ChatMessage(byte[] buffer) {
            file = buffer;  
            type = SENDING_FILE;
        }


	// getters
	byte[] getFile() {
		return file;
	}
}


Now, call the ChatMessage constructor with the bytes of your file and send that message over the wire.
At the other end the Client/Server who already switch on the type will have to add the case SENDING_FILE: in the switch()
Was This Post Helpful? 1
  • +
  • -

#4 moh_tmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

Re: File Transfer in A simple Chat program with Client/Server

Posted 19 November 2012 - 02:21 PM

Here what i do
Modify the ChatMessage class

In the ClientGUI class
Add the following
if(o == SendFile){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File myFile = new File( chooser.getSelectedFile().getAbsolutePath() );
byte[] mybytearray = new byte[(int) myFile.length()];
client.sendMessage(new ChatMessage(mybytearray));
}
return;
}



In the server side
Add the following
case ChatMessage.SENDING_FILE:
byte[] myFile = cm.getFile();
FileInputStream fis = null;
BufferedInputStream bis = new BufferedInputStream(fis);
for(int i = al.size(); --i >= 0;) {
try {
bis.read(myFile, 0, myFile.length);
ct.write(myFile, 0, myFile.length);
ct.flush();
ct.close();
ct.close();
return;
} catch (IOException ex) {
}
}
} // End For



And now I think the ClientGUI class should have similar code to the server
But I don't know where to put it

Thank you very much
Sorry for bad English
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: File Transfer in A simple Chat program with Client/Server

Posted 19 November 2012 - 02:29 PM

This
byte[] mybytearray = new byte[(int) myFile.length()];

just create an array to hold the file, you still have to copy the file into it
unless it is a very big file, in that case you will have to send it by chunks
Was This Post Helpful? 0
  • +
  • -

#6 moh_tmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

Re: File Transfer in A simple Chat program with Client/Server

Posted 19 November 2012 - 03:03 PM

This what I do

In ClientGUI class
try {
FileInputStream fileInputStream = new FileInputStream(myFile);
fileInputStream.read(mybytearray);
for (int i = 0; i < mybytearray.length; i++) {
System.out.print((char)mybytearray[i]);
}
} catch (Exception en) {


}


I think this copy the file to the array

In the server side
I want to send the bytes array and let the client know it is a file and save it with the same name and extension
What do you mean by very big what is the size
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 moh_tmp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-November 12

Re: File Transfer in A simple Chat program with Client/Server

Posted 24 November 2012 - 08:58 AM

Can any body help me please?
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2684
  • View blog
  • Posts: 11,340
  • Joined: 20-September 08

Re: File Transfer in A simple Chat program with Client/Server

Posted 24 November 2012 - 10:12 AM

You'll make your life easier by using a separate Socket for file sending. Send a header first containing the length and name then send the file (if it's of acceptable size)
Was This Post Helpful? 0
  • +
  • -

#9 Arkar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-April 13

Re: File Transfer in A simple Chat program with Client/Server

Posted 20 April 2013 - 09:11 PM

View Postpbl, on 19 November 2012 - 02:29 PM, said:

This
byte[] mybytearray = new byte[(int) myFile.length()];

just create an array to hold the file, you still have to copy the file into it
unless it is a very big file, in that case you will have to send it by chunks



Hi Paul,

Thanks for your contributions in this forum!

I am a drop-out from uni and coding is my focus since then. I just wonder how a integrated chat program (with chat and file transfer functions) created by you will look like!

Could you please show me a glimpse of it if you don't mind!

With Thanks,
ARkar
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1