【发布时间】:2014-05-19 12:15:57
【问题描述】:
我是 Java 新手,在将变量从一个类传递到另一个主类时遇到了一些问题。
关于该计划的一些信息 -
我有一个名为“Server.java”的主类和另一个名为“Client.java”的主类
这是一个用java编写的简单的TCP Server-client程序。服务器类首先执行,因此它可以接受来自客户端的连接,然后执行。
一旦客户端连接到服务器,客户端通过键入例如“alice.txt”来指定它希望从服务器接收的文件的名称,然后服务器将其目录中具有该名称的文件发送到客户。
我被困在哪里 -
如果我首先在服务器中硬编码文件名,我只能在客户端接收文件(检查下面的代码)。我希望从客户端获取文件名并传递给 Server 类,以便代码适用于所有文件,而不仅仅是一个硬编码的文件。
任何帮助表示赞赏:)
Server.java
import java.io.*;
import java.net.*;
class Server
{
public static void main(String argv[]) throws Exception
{
//beginning of the try method
try
{
//create a new serversocket object with port no 6789
ServerSocket welcomeSocket = new ServerSocket(6789);
//while loop
while(true)
{
//create a new socket object and accept the connection and it waits for any connection from client
Socket connectionSocket = welcomeSocket.accept();
//display confirmation to the user
System.out.println("Connection accepted!");
System.out.println("File request recevied!");
//specify the file the server wants to send
File myFile = new File("alice.txt");
//THIS IS WHERE THE FILE FROM THE CLIENT IS HARD-CODED. I AM TRYING TO REPLACE THE FILE NAME WITH A VARIABLE THAT WAS PASSED FROM THE CLIENT SIDE
//get the byte array length of the file
byte [] bArray = new byte [(int)myFile.length()];
//open a new file object
FileInputStream f = new FileInputStream(myFile);
//new buffered input stream object
BufferedInputStream bs = new BufferedInputStream(f);
//read function of the inputput stream
bs.read(bArray, 0, bArray.length);
//declare new output strea object
OutputStream os = connectionSocket.getOutputStream();
//display messages to the users
System.out.println("Okay, sending the file now.");
//write the file
os.write(bArray, 0, bArray.length);
//flush the file
os.flush();
//close the connection
connectionSocket.close();
//display confirmation message to the user
System.out.println("File was successfully sent!");
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
Client.java
import java.io.*;
import java.net.*;
import java.util.*;
class Client
{
public static void main(String argv[]) throws Exception
{
try
{
//declare scanner object
Scanner s = new Scanner(System.in);
//display a message to the user
System.out.println("Enter the file name you wish to request");
//read the user input
String textFileName = s.nextLine();
//declare a new Socket object and specify the host name and the port number
Socket clientSocket = new Socket("localhost", 6789);
//make a byte array in which the transmitted file will be broken down into and sent
byte [] bArray = new byte[10000000];
//create new inputstream object and set it to the input stream from the client
InputStream is = clientSocket.getInputStream();
//open new fileinput object
FileOutputStream fos = new FileOutputStream(textFileName);
//get the value from the fileoutputstream to bufferedoutput stream
BufferedOutputStream bos = new BufferedOutputStream(fos);
//read function of the inputsteam object
int readFile = is.read(bArray,0,bArray.length);
//assign readfile to endile
int endFile = readFile;
do
{
readFile = is.read(bArray, endFile, (bArray.length-endFile));
if(readFile >= 0)
{
endFile = endFile + readFile;
}
}while(readFile > -1);
//write file
bos.write(bArray, 0, endFile);
//show the message to the user
System.out.println("File " + textFileName + " was successfully received!");
//flush the file
bos.flush();
//close the file
bos.close();
//close the socket
clientSocket.close();
///
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
【问题讨论】:
-
如果您从命令行调用此程序,只需将其作为参数传递,然后使用 argv[0] 或 argv[1] 等从参数中获取它
-
@Fallso 我这样做了,但是在将这些参数传递给 Server.java 时卡住了。还查看了 getter 和 setter,但据我所知,它们不适用于 Main 类。
标签: java networking tcp