send files...please,can any one help me

i do a simple java chatting project

i want help in sending files between client and server

this my client and server code

how can i change in this code to achieve this point

thanks a lot for caring

//Server Class

import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Server extends JFrame

{

private JTextField enter;

private JTextArea display;

ObjectOutputStream output;

ObjectInputStream input;

//String message= " ";

public Server()

{

super("Server");

Container c=getContentPane();

enter=new JTextField();

enter.setEnabled(false);

enter.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent e)

{

sendData(e.getActionCommand());

}

}

);

c.add(enter, BorderLayout.NORTH);

display=new JTextArea();

c.add(new JScrollPane(display),BorderLayout.CENTER);

setSize(300,150);

show();

}

public void runServer()

{

ServerSocket server;

Socket connection;

int counter = 1;

try {

server=new ServerSocket(5000,100);

while(true) {

display.setText("waiting for connection\n");

connection=server.accept();

//client=new Socket(InetAddress.getByName("127.0.0.1"),5000);

display.append("Connection "+counter+

"recieved from "+ connection.getInetAddress().getHostName());

output=new ObjectOutputStream(connection.getOutputStream());

output.flush();

input=new ObjectInputStream(connection.getInputStream());

display.append("\nGot I/O stream\n");

String message="server>>connection successfull";

output.writeObject(message);

output.flush();

enter.setEnabled(true);

do{

try {

message=(String)input.readObject();

display.append("\n"+message);

display.setCaretPosition(display.getText().length());

}

catch(ClassNotFoundException cnfex){

display.append("\nUnknown object type recieved");

}

} while(!message.equals("CLIENT >>> TERMINATE"));

display.append("User terminated connection\n");

enter.setEnabled(false);

output.close();

input.close();

connection.close();

++counter;

}

}

catch(EOFException eof){

System.out.println("client terminated connection");

}

catch(IOException io) {

io.printStackTrace();

}

}

private void sendData(String s)

{

try {

//message=s;

output.writeObject("server>>> "+s);

output.flush();

display.append("\n server>>> "+s);

}

catch(IOException cnfex) {

display.append("\nError writing object ");

}

}

public static void main(String arge[])

{

Server app=new Server();

app.addWindowListener( new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

app.runServer();

}

}

//Client Class

import java.io.*;

import java.net.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Client extends JFrame

{

private JTextField enter;

private JTextArea display;

ObjectOutputStream output;

ObjectInputStream input;

String message= " ";

public Client()

{

super("Client");

Container c=getContentPane();

enter=new JTextField();

enter.setEnabled(false);

enter.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent e)

{

sendData(e.getActionCommand());

}

}

);

c.add(enter, BorderLayout.NORTH);

display=new JTextArea();

c.add(new JScrollPane(display),BorderLayout.CENTER);

setSize(300,150);

show();

}

public void runClient()

{

Socket client;

try {

display.setText("Attempting connection\n");

client=new Socket(InetAddress.getByName("127.0.0.1"),5000);

display.append("Connected to "+client.getInetAddress().getHostName());

output=new ObjectOutputStream(client.getOutputStream());

output.flush();

input=new ObjectInputStream(client.getInputStream());

display.append("\nGot I/O stream\n");

enter.setEnabled(true);

do{

try {

message=(String)input.readObject();

display.append("\n"+message);

display.setCaretPosition(display.getText().length());

}

catch(ClassNotFoundException cnfex){

display.append("\nUnknown object type recieved");

}

} while(!message.equals("SERVER >>> TERMINATE"));

display.append("Closing connection\n");

input.close();

output.close();

client.close();

}

catch(EOFException eof){

System.out.println("Server terminated connection");

}

catch(IOException e) {

e.printStackTrace();

}

}

private void sendData(String s)

{

try {

message=s;

output.writeObject("Client>>> "+s);

output.flush();

display.append("\n client>>> "+s);

}

catch(IOException cnfex) {

display.append("\nError writing object ");

}

}

public static void main(String arge[])

{

Client app=new Client();

app.addWindowListener( new WindowAdapter(){

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

app.runClient();

}

}

[5725 byte] By [Roddicka] at [2007-11-27 9:44:33]
# 1
http://java.sun.com/docs/books/tutorial/networking/sockets/index.htmlStudyTryAsk (with "code" tag, please)
pbulgarellia at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 2

I don't want to be a pain, but next time you might try and:

1- use the "code" tag

2- not paste your whole entire code.

The reason I say that is because it saves other users time when they are trying to help you, plus no-one can steal the whole program!

(But keep in mind, this is up to you)

http://java.sun.com/docs/books/tutorial/networking/index.html

This will help you from start to finish. I would try going back to the basics for a bit. Make sure you understand everything.

Hope this helps!

~Matt

OriginalSup3rmana at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 3

I'm not gonna read your code, for the following reasons

1. It's long and I'm lazy

2. You don't have a problem with your code

3. If I did, I'd be making it for you

I think the best way to do this is get the file, extract its byte data, pack it into packets and send them to the recipient.

RedUnderTheBeda at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 4

I didn't read your code, I don't think anybody did, but I'll give you some hints, you figure the rest out.

first:

char j=0;

while((j =(char)in.read()) != '}')

{

OUTPUTFILENAME=OUTPUTFILENAME+j;

}

FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME);

BufferedOutputStream out = new BufferedOutputStream(fos);

that should help you on your server (receiver) side.

as for the client (sender) side:

final JFileChooser fc = new JFileChooser();

//here set up a JFileChooser

//and put the following method in some appropriate calss

public static boolean send( String filename,String fname ) {

try {

Socket skt = new Socket(HOST, PORT);

FileInputStream fis = new FileInputStream(filename);

BufferedInputStream in = new BufferedInputStream(fis);

BufferedOutputStream out = new BufferedOutputStream( skt.getOutputStream() );

fname=fname+"}";

byte[] name=fname.getBytes();

for(int i=0;i<name.length;i++)

out.write(name[i]);

int i;

while ((i = in.read()) != -1) {

out.write(i);

}

return true;

}

catch( Exception e )

{

System.out.print("Error! It didn't work! " + e + "\n");

return false;

}

note that this code isn't complete, you'll have to do many things, but, it should help you know where to start from.

Omar_Sayyeda at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 5
thanks pbulgarelli for your link and advice
Roddicka at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 6
thanks alot matt for your hint
Roddicka at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 7
and thanks RedUnderTheBed
Roddicka at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 8
thnk you Omar Sayed i think your simple code will stand me up..........but i dont know how to use codeto write my code?Message was edited by: Roddick
Roddicka at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...
# 9

This is the part of the Server side (receiver) code, it could be your client, but

you must make sure that this code is implemented on the RECEIVER side.

As for how to implement it, you simply must create a ServerSocket for connections and another Socket for I/O, the determination of Client/Server sides is up to you.

char j=0;

while((j =(char)in.read()) != '}')

{

OUTPUTFILENAME=OUTPUTFILENAME+j;

}

FileOutputStream fos = new FileOutputStream(OUTPUTFILENAME);

BufferedOutputStream out = new BufferedOutputStream(fos);

Here is the sender side code, it could be your client or server, like before you must create a socket for writing, (skt).

final JFileChooser fc = new JFileChooser();

//here set up a JFileChooser

//and put the following method in some appropriate calss

public static boolean send( String filename,String fname ) {

try {

Socket skt = new Socket(HOST, PORT);

FileInputStream fis = new FileInputStream(filename);

BufferedInputStream in = new BufferedInputStream(fis);

BufferedOutputStream out = new BufferedOutputStream( skt.getOutputStream() );

fname=fname+"}";

byte[] name=fname.getBytes();

for(int i=0;i<name.length;i++)

out.write(name[i]);

int i;

while ((i = in.read()) != -1) {

out.write(i);

}

return true;

}

catch( Exception e )

{

System.out.print("Error! It didn't work! " + e + "\n");

return false;

}

Omar_Sayyeda at 2007-7-12 23:51:33 > top of Java-index,Core,Core APIs...