Using Socket Programming (TCP/IP)

Using Socket Programming (TCP/IP) 

This is a fundamental approach that gives you a lot of control over the transfer process. You would typically create a client and a server application in Java. 


How it works: 


Server-side: The server application listens on a specific port for incoming connections from clients. When a client connects, the server accepts the connection and sets up input and output streams to communicate with the client.


Client-side: The client application initiates a connection to the server's IP address and port. Once the connection is established, the client can send requests to the server (e.g., to upload or download a file) and receive responses.


File Transfer: For transferring a file, the client reads the file's content and sends it to the server through the output stream. The server reads the data from its input stream and writes it to a file on its system. The process is reversed for downloading a file.    


Example: 


Server:

import java.net.*; 
import java.io.*; 
 
public class FileServer { 
    public static void main(String[] args) throws IOException { 
        ServerSocket serverSocket = new ServerSocket(12345); 
        System.out.println("Server started. Waiting for client..."); 
        Socket clientSocket = serverSocket.accept(); 
        System.out.println("Client connected: " + clientSocket.getInetAddress()); 
 
        // Get input stream from client 
        InputStream inputStream = clientSocket.getInputStream(); 
        // Create file output stream to save the received file 
        FileOutputStream fileOutputStream = new FileOutputStream("received_file.txt"); 
 
        byte[] buffer = new byte[4096]; 
        int bytesRead; 
        while ((bytesRead = inputStream.read(buffer)) != -1) { 
            fileOutputStream.write(buffer, 0, bytesRead); 
        } 
 
        System.out.println("File received."); 
 
        fileOutputStream.close(); 
        inputStream.close(); 
        clientSocket.close(); 
        serverSocket.close(); 
    } 
} 
 

Client: 


import java.net.*; 
import java.io.*; 
 
public class FileClient { 
    public static void main(String[] args) throws IOException { 
        String serverAddress = "127.0.0.1"; // Replace with the actual server IP 
        int serverPort = 12345; 
 
        Socket socket = new Socket(serverAddressserverPort); 
        System.out.println("Connected to server: " + serverAddress + ":" + serverPort); 
 
        // Get output stream to send data to the server 
        OutputStream outputStream = socket.getOutputStream(); 
        // Create file input stream to read the file to be sent 
        FileInputStream fileInputStream = new FileInputStream("file_to_send.txt"); 
 
        byte[] buffer = new byte[4096]; 
        int bytesRead; 
        while ((bytesRead = fileInputStream.read(buffer)) != -1) { 
            outputStream.write(buffer, 0, bytesRead); 
        } 
 
        System.out.println("File sent."); 
 
        fileInputStream.close(); 
        outputStream.close(); 
        socket.close(); 
    } 
} 
 

Pros: 

  • Provides fine-grained control over the transfer process. 
  • Suitable for custom protocols and specific requirements. 

Cons: 

  • Requires more coding and handling of low-level details (e.g., error handling, multi-threading for concurrent transfers). 
  • You need to implement the file transfer logic yourself. 


Click on link to view more details: Transfer files over the internet using Java



#IT-Buzz, #Socket, #Java, #File transfer

Post a Comment

0 Comments