Question
Can someone help me add/implement a simple file transfer the following way: File transfer One person can send a file to another during the conversation
Can someone help me add/implement a simple file transfer the following way:
File transfer One person can send a file to another during the conversation It's up to you how the user initiates it; how the file is identified; whether the receiver gets the appropriate filename, or has to guess at one, or if received files all have the same name e.g. one user might type /send somefile.txt and perhaps the user simply gets download.dat
the code that I have done so far is below I just need the ability to be able to send a file anytime I want
Client.java
package sockets;
import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner;
public class Client {
private final String host; private final int port; private String nickname;
public static void main(String[] args) throws UnknownHostException, IOException { new Client("localhost", 12345).run(); }
public Client(String host, int port) { this.host = host; this.port = port; }
public void run() throws UnknownHostException, IOException { // connect client to server Socket client = new Socket(host, port); System.out.println("Client successfully connected to server!");
// create a new thread for server messages handling new Thread(new ReceivedMessagesHandler(client.getInputStream())).start();
// ask for a nickname Scanner sc = new Scanner(System.in); System.out.print("Enter a nickname: "); nickname = sc.nextLine();
// read messages from keyboard and send to server System.out.println("Send messages: "); PrintStream output = new PrintStream(client.getOutputStream());
while (sc.hasNextLine()) { output.println(nickname + ": " + sc.nextLine()); }
output.close(); sc.close(); client.close(); } }
class ReceivedMessagesHandler implements Runnable {
private InputStream server;
public ReceivedMessagesHandler(InputStream server) { this.server = server; }
@Override public void run() { try ( // receive server messages and print out to screen Scanner s = new Scanner(server)) { while (s.hasNextLine()) { System.out.println(s.nextLine()); } } } }
*******************************************************************************
Server.java
package sockets;
import java.io.IOException; import java.io.InputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; import java.util.Scanner;
public class Server {
private final int port; private final List clients; private ServerSocket server;
public static void main(String[] args) throws IOException { new Server(12345).run(); }
public Server(int port) { this.port = port; this.clients = new ArrayList<>(); }
public void run() throws IOException { server = new ServerSocket(port) { protected void finalize() throws IOException, Throwable { try { this.close(); } finally { super.finalize(); } } }; System.out.println("Port 12345 is now open.");
while (true) { // accepts a new client Socket client = server.accept(); System.out.println("Connection established with client: " + client.getInetAddress().getHostAddress());
// add client message to list this.clients.add(new PrintStream(client.getOutputStream()));
// create a new thread for client handling new Thread(new ClientHandler(this, client.getInputStream())).start(); } }
void broadcastMessages(String msg) { for (PrintStream client : this.clients) { client.println(msg); } } }
class ClientHandler implements Runnable {
private Server server; private InputStream client;
public ClientHandler(Server server, InputStream client) { this.server = server; this.client = client; }
@Override public void run() { String message = null;
// when there is a new message, broadcast to all Scanner sc = new Scanner(this.client); while (sc.hasNextLine()) { message = sc.nextLine(); server.broadcastMessages(message); } sc.close(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started