Download
- // Client.java
- /*
- * In this client/server example, the client sends a file to the server.
- *
- */
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.net.*;
- import java.io.*;
- /* Create and display the client form */
- Client clientForm = new Client();
- clientForm.Display();
- }
- public void Display(){
- frame.setTitle("Client");
- btnTransfer.addActionListener(this);
- mainPanel.setLayout(layout);
- mainPanel.add(lblFile);
- mainPanel.add(txtFile);
- mainPanel.add(btnTransfer);
- frame.getContentPane().add(mainPanel);
- frame.pack();
- frame.setVisible(true);
- }
- /* File Open Dialog box allows the user to select a file */
- fileDlg.showOpenDialog(this);
- txtFile.setText(filename);
- try{
- /* Try to connect to the server on localhost, port 5555 */
- /* Send filename to server */
- outputStream.write(fileDlg.getSelectedFile().getName() + "\n");
- outputStream.flush();
- /* Get reponse from server */
- /* If server is ready, send the file */
- if ( serverStatus.equals("READY") ){
- byte[] buffer = new byte[sk.getSendBufferSize()];
- int bytesRead = 0;
- while((bytesRead = file.read(buffer))>0)
- {
- output.write(buffer,0,bytesRead);
- }
- output.close();
- file.close();
- sk.close();
- }
- }
- /* Catch any errors */
- }
- }
- }
- // Server.java
- /*
- * Server waits for a connection to be established by client
- *
- */
- import java.io.*;
- import java.net.*;
- class Server
- {
- {
- /* Listen on port 5555 */
- /* Accept the sk */
- /* Read the filename */
- if ( !filename.equals("") ){
- /* Reply back to client with READY status */
- outReader.write("READY\n");
- outReader.flush();
- }
- /* Create a new file in the tmp directory using the filename */
- byte[] buffer = new byte[sk.getReceiveBufferSize()];
- int bytesReceived = 0;
- while((bytesReceived = input.read(buffer))>0)
- {
- /* Write to the file */
- wr.write(buffer,0,bytesReceived);
- }
- }
- }
4 comments:
got error file cannot find
Hi Vams, sorry for the late reply, can you provide more details, is the file not found on the client or on the server?
Arjun Goswami: Create a file in c drive with tmp as name
thank u man
Post a Comment