Skip to content

Instantly share code, notes, and snippets.

@Anass-ABEA
Created September 11, 2022 14:54
Show Gist options
  • Save Anass-ABEA/23a0ede78c25f66ff677364944c2cb4f to your computer and use it in GitHub Desktop.
Save Anass-ABEA/23a0ede78c25f66ff677364944c2cb4f to your computer and use it in GitHub Desktop.
Java Sockets #3 : Simplified Client/Server Application - Sending files from server to client
package myjava.sockets.filetransfert;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
private Scanner scanner;
public Client() {
try{
socket = new Socket("127.0.0.1",Server.PORT);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
scanner = new Scanner(System.in);
getFile();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getFile() throws IOException {
String filesLen = in.readUTF();
int maxFiles = Integer.parseInt(filesLen);
String menu = in.readUTF();
System.out.println(menu);
int userSelection = 1;
boolean isSelectionCorrect = false;
while (!isSelectionCorrect){
System.out.print("Select a file number: ");
userSelection = scanner.nextInt();
isSelectionCorrect = userSelection>0 && userSelection<= maxFiles;
}
out.writeUTF(""+userSelection);
String fileContent = in.readUTF();
System.out.println(" -- FILE START --");
System.out.println(fileContent);
System.out.println(" -- FILE END --");
}
public static void main(String[] args) {
new Client();
}
}
package myjava.sockets.filetransfert;
import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
import java.util.List;
public class ClientConnection {
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
public ClientConnection(Socket socket) {
this.socket = socket;
try{
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendFile() {
try{
sendMenu();
int index = getSelectedFileIndex();
sendSelectedFile(index);
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendSelectedFile(int index) throws IOException {
File[] fileList = new File(Server.FILES_PATH).listFiles();
File selectedFile = fileList[index];
List<String> fileLines = Files.readAllLines(selectedFile.toPath());
String fileContent = String.join("\n",fileLines);
out.writeUTF(fileContent);
}
private int getSelectedFileIndex() throws IOException {
String input = in.readUTF();
return Integer.parseInt(input)-1;
}
private void sendMenu() throws IOException {
String menu = "** Files **\n";
File[] fileList = new File(Server.FILES_PATH).listFiles();
out.writeUTF(""+fileList.length);
for (int i = 0; i < fileList.length; i++) {
menu += String.format("* %d - %s\n",i+1, fileList[i].getName());
}
out.writeUTF(menu);
}
public void close() {
try{
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package myjava.sockets.filetransfert;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static final String FILES_PATH = "./files/server";
private ServerSocket serverSocket;
public static final int PORT = 3030;
public Server() {
try{
serverSocket = new ServerSocket(PORT);
acceptConnections();
} catch (IOException e) {
e.printStackTrace();
}
}
private void acceptConnections() throws IOException {
while (true){
Socket clientSocket = serverSocket.accept();
if(clientSocket.isConnected())
new Thread( ()->{
ClientConnection client = new ClientConnection(clientSocket);
client.sendFile();
client.close();
} ).start();
}
}
public static void main(String[] args) {
new Server();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment