gpt4 book ai didi

java - 如何从多个客户端接收文件?

转载 作者:行者123 更新时间:2023-12-01 12:46:06 26 4
gpt4 key购买 nike

我创建了一个程序,它将向服务器或客户端发送文件

我的问题是我有 2 个客户端,他们都需要向服务器发送文件发生的情况是服务器只能从 1 个客户端(第一个发送文件的客户端)接收文件

如何解决这个问题?

这是我的代码:

服务器

private void sendFile(File file)throws IOException
{
Socket socket = null;
String host = "127.0.0.1";
String receiver=txtReceiver.getSelectedItem().toString();
int port=0;
if(receiver=="Client1")
{
host="127.0.0.2";
port=4441;
}
else if(receiver=="Client2")
{
port=4442;
host="127.0.0.3";
}
else if(receiver=="Server")
{
port=4440;
host="127.0.0.1";
}

socket = new Socket(host, port);

//File file = new File("Client.txt");
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large.");
}
byte[] bytes = new byte[(int) length];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

int count;

while ((count = bis.read(bytes)) > 0) {
out.write(bytes, 0, count);
}

out.flush();
out.close();
fis.close();
bis.close();
socket.close();
}

public static void main(String args[]) throws IOException {
ServerSocket serverSocket = null;

try
{
serverSocket = new ServerSocket(4440);
} catch (IOException ex)
{
System.out.println("Can't setup server on this port number. ");
}

Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int bufferSize = 0;

try
{
socket = serverSocket.accept();
} catch (IOException ex)
{
System.out.println("Can't accept client connection. ");
}

try
{
is = socket.getInputStream();
bufferSize = socket.getReceiveBufferSize();
System.out.println("Buffer size: " + bufferSize);
} catch (IOException ex)
{
System.out.println("Can't get socket input stream. ");
}

try
{
fos = new FileOutputStream("C:\\Users\\Jake_PC\\Documents\\NetBeansProjects\\OJT2\\ServerReceivables\\file.txt");
bos = new BufferedOutputStream(fos);

} catch (FileNotFoundException ex)
{
System.out.println("File not found. ");
}

byte[] bytes = new byte[bufferSize];

int count;

while ((count = is.read(bytes)) > 0)
{
bos.write(bytes, 0, count);
}

bos.flush();
bos.close();
is.close();
socket.close();
serverSocket.close();

客户端

public static void main(String args[])throws IOException {
ServerSocket serverSocket = null;

try
{
serverSocket = new ServerSocket(4441);
} catch (IOException ex)
{
System.out.println("Can't setup server on this port number. ");
}

Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int bufferSize = 0;

try
{
socket = serverSocket.accept();
} catch (IOException ex)
{
System.out.println("Can't accept client connection. ");
}

try
{
is = socket.getInputStream();
bufferSize = socket.getReceiveBufferSize();
System.out.println("Buffer size: " + bufferSize);
} catch (IOException ex)
{
System.out.println("Can't get socket input stream. ");
}
//C:\Users\Jake_PC\Documents\NetBeansProjects\OJT2
try
{
fos = new FileOutputStream("C:\\Users\\Jake_PC\\Documents\\NetBeansProjects\\OJT2\\Client1Receivables\\file.txt");
bos = new BufferedOutputStream(fos);

} catch (FileNotFoundException ex)
{
System.out.println("File not found. ");
}

byte[] bytes = new byte[bufferSize];

int count;

while ((count = is.read(bytes)) > 0)
{
bos.write(bytes, 0, count);
}

bos.flush();
bos.close();
is.close();
socket.close();
serverSocket.close();
}

private void sendFile(File file)throws IOException
{
Socket socket = null;
String host = "127.0.0.1";
String receiver=txtReceiver.getSelectedItem().toString();
int port=0;
if(receiver=="Client1")
{
port=4441;
}
else if(receiver=="Client2")
{
port=4442;
}
else if(receiver=="Server")
{
port=4440;
}

socket = new Socket(host, port);

//File file = new File("Client.txt");
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File is too large.");
}
byte[] bytes = new byte[(int) length];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());

int count;

while ((count = bis.read(bytes)) > 0) {
out.write(bytes, 0, count);
}

out.flush();
out.close();
fis.close();
bis.close();
socket.close();
}

最佳答案

您需要启动一个新线程来处理每个接受的套接字。例子比比皆是。例如,请参阅 Java 教程中的自定义网络跟踪。

关于java - 如何从多个客户端接收文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24690093/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com