J'ai mis en œuvre les classes simples de serveur TCP et de client TCP qui peuvent envoyer le message du client au serveur et le message sera converti en majuscules du côté du serveur, mais comment puis-je réaliser le transfert de fichiers du serveur au client et le téléchargement de fichiers du client au serveur. les codes suivants sont ce que j'ai obtenu.
TCPClient.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
class TCPClient {
public static void main(String args[]) throws Exception {
int filesize=6022386;
int bytesRead;
int current = 0;
String ipAdd="";
int portNum=0;
boolean goes=false;
if(goes==false){
System.out.println("please input the ip address of the file server");
Scanner scan=new Scanner(System.in);
ipAdd=scan.nextLine();
System.out.println("please input the port number of the file server");
Scanner scan1=new Scanner(System.in);
portNum=scan1.nextInt();
goes=true;
}
System.out.println("input done");
int timeCount=1;
while(goes==true){
//System.out.println("connection establishing");
String sentence="";
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(
System.in));
Socket clientSocket = new Socket(ipAdd, portNum);
//System.out.println("connecting");
//System.out.println(timeCount);
if(timeCount==1){
sentence="set";
//System.out.println(sentence);
}
if(timeCount!=1)
sentence = inFromUser.readLine();
if(sentence.equals("close"))
clientSocket.close();
if(sentence.equals("download"))
{
byte [] mybytearray = new byte [filesize];
InputStream is = clientSocket.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\users\\cguo\\kk.lsp");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
//System.out.println(end-start);
bos.close();
clientSocket.close();
}
// if(sentence.equals("send"))
// clientSocket.
timeCount--;
//System.out.println("connecting1");
DataOutputStream outToServer = new DataOutputStream(clientSocket
.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
//System.out.println("connecting2");
//System.out.println(sentence);
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 {
Socket s = null;
int firsttime=1;
while (true) {
String clientSentence;
String capitalizedSentence="";
ServerSocket welcomeSocket = new ServerSocket(3248);
Socket connectionSocket = welcomeSocket.accept();
//Socket sock = 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("download"))
{
File myFile = new File ("C:\\Users\\cguo\\11.lsp");
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = connectionSocket.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
connectionSocket.close();
}
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");
}
}
}
Ainsi, le TCPServer.java sera exécuté en premier, puis le TCPClient.java, et j'essaie d'utiliser la clause if dans le TCPServer.java pour tester l'entrée de l'utilisateur, maintenant je veux vraiment mettre en œuvre la façon de transférer des fichiers des deux côtés (download et upload).