Implemento el servidor TCP simple y las clases de cliente TCP que pueden enviar el mensaje del cliente al servidor y el mensaje se convertirá a mayúsculas en el servidor, pero ¿cómo puedo lograr transferir archivos de servidor a cliente y cargar archivos de cliente a servidor? los siguientes códigos son los que tengo.cómo implementar el servidor TCP y el cliente TCP en java para transferir archivos
TCPClient.java
:
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String args[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("127.0.0.1", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + "\n");
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
TCPServer.java
:
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String args[]) throws Exception {
int firsttime = 1;
while (true) {
String clientSentence;
String capitalizedSentence="";
ServerSocket welcomeSocket = new ServerSocket(3248);
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
//System.out.println(clientSentence);
if (clientSentence.equals("set")) {
outToClient.writeBytes("connection is ");
System.out.println("running here");
//welcomeSocket.close();
//outToClient.writeBytes(capitalizedSentence);
}
capitalizedSentence = clientSentence.toUpperCase() + "\n";
//if(!clientSentence.equals("quit"))
outToClient.writeBytes(capitalizedSentence+"enter the message or command: ");
System.out.println("passed");
//outToClient.writeBytes("enter the message or command: ");
welcomeSocket.close();
System.out.println("connection terminated");
}
}
}
Así, el TCPServer.java
se ejecutará en primer lugar, y luego ejecutar el TCPClient.java
, y trato de usar la cláusula if en el TCPServer.java
para poner a prueba cuál es la entrada del usuario, ahora realmente quiero implementar cómo transferir archivos desde ambos lados (descargar y subir) .Gracias.
http://stackoverflow.com/questions/4687615/how-to-achieve-transfer-file-between-client-and- server-using-java-socket –
Además de todas las respuestas, puede leer todos los bytes de cualquier archivo a la vez con [readAllBytes (...)] (http://docs.oracle.com/javase/8/docs/ api/java/nio/file/Files.html # readAllBytes-java.nio.file.Path-) y escríbalos en cualquier archivo w ith [write (...)] (http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#write-java.nio.file.Path-byte: A-java.nio.file.OpenOption ...-). – thanopi57