1

I'm writing a client server application in Java. At one point I need my PC client to create a directory on my PC server via socket, only I could not make it happen. Can someone help me?

1

2 Answers 2

2

You need to design a communication protocol, this is a type of dialect in which your server and your client speak.

i have designed the simplest communication protocol ever for you, that only has 2 commands.

Consist in 2 simple commands and its meaning:

mkdir - create a directory in the remote server

exit - close the session

this means that you have to write 2 programs (server and client) that implement your great communication protocol.

you can start from this code that actually sends/receives 2 commands and pretend executing the command and close the session.

Server side:

    import java.net.*;
import java.io.*;

public class Server
{
    static ServerSocket   serverSocket;
    static Socket         socket;
    static BufferedWriter writer;
    static BufferedReader reader;

    public static void main(String args[])
    {
        try
        {
            System.out.println("Hola mundo");
            serverSocket = new ServerSocket(12345);
            socket = serverSocket.accept();
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            while(true)
            {
                String command = reader.readLine();
                if(command.contains("mkdir"))
                {
                    System.out.println("Creating a new directory " + command);
                    // put yoour code to create a directory with the params
                }
                else if(command.equals("exit"))
                {
                    System.out.println("Exiting the program");
                    break;
                }
            }
        }
        catch(Exception err)
        {

        }
    }
}

Client side :

import java.net.*;
import java.io.*;

public class Client
{
    static Socket socket;
    static BufferedWriter writer;
    static BufferedReader reader;
    public static void main(String args[])
    {
        try
        {
            System.out.println("Connecting to the server");
            InetSocketAddress address = new InetSocketAddress("127.0.0.1", 12345);
            socket = new Socket();
            socket.connect(address);
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            // creating folder
            System.out.println("Creating remote folder");
            writer.write("mkdir newFolder\r\n");
            writer.flush();


            // closing server
            System.out.println("Sending exit command");
            writer.write("exit\r\n");
            writer.flush();

            socket.close();
        }
        catch(IOException err)
        {
            System.out.println(err.getMessage());
        }
    }
}

Now your challenge is to make really the folder is created when the mkdir command reaches the server.

Compilation:

javac Client.java

javac Server.java
0
0

On the server, you can use Files#createDirectory() to create a directory on the filesystem. You might use data passed by the client to determine the name of the directory.

Here's a link to the Javadoc: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#createDirectory-java.nio.file.Path-java.nio.file.attribute.FileAttribute...-

Here's a link in Oracle's Java Tutorials that includes an example: https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#create

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.