Answer the question
In order to leave comments, you need to log in
What could be wrong in the code for receiving / transmitting a file (Socket)?
I'm trying to send files from the server to the client on request 1 or 2
telnet accepts the text of the files any number of times without problems.
The client, in turn, makes a request, but cannot wait for the file
Server code
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws Exception {
try (ServerSocket serverSocket = new ServerSocket(9001)) {
while (true) {
System.out.println("Ожидание клиента");
Socket socket = serverSocket.accept();
System.out.println("Клиент подключен");
new Thread(new MyServer(socket)).start();
}
}
}
}
class MyServer implements Runnable {
private Socket socket;
public MyServer(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try (Scanner scanner = new Scanner(socket.getInputStream())) {
OutputStream os = socket.getOutputStream();
while (scanner.hasNextLine()) {
String i = scanner.nextLine();
if (!socket.isConnected()) {
break;
} else {
if (i.equals("1")) {
new SendFile(os, "F:\\save.ser");
System.out.println("Файл 1 отправлен");
} else if (i.equals("2")) {
new SendFile(os, "F:\\arxiv.txt");
System.out.println("Файл 2 отправлен");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class SendFile {
public SendFile(OutputStream outputStream,String name) throws Exception {
File myFile = new File(name);
byte[] buffer = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream in = new BufferedInputStream(fis);
in.read(buffer, 0, buffer.length);
outputStream.write(buffer, 0, buffer.length);
outputStream.flush();
in.close();
fis.close();
// outputStream.close(); // Если он открыт, то на один клиентский поток, может
// отправиться лишь один файл
}
}
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 9001);
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
// Создаем объект для чтения строк с клавиатуры
Scanner scan = new Scanner(System.in, "UTF-8");
InputStream is = socket.getInputStream();
while (true) {
System.out.print("[Запрос]: ");
String line = scan.nextLine();
// Отправляем строку серверу
out.println(line);
out.flush();
new ReceiveFile(is,"arxiv.ser");
System.out.println("Файл получен");
}
} catch (Exception x) {
System.out.println("Ошибка ввода/вывода");
x.printStackTrace();
}
}
}
import java.io.*;
public class ReceiveFile {
public ReceiveFile(InputStream is,String name) throws Exception{
int byteread;
File test = new File(name);
FileOutputStream fos = new FileOutputStream(test);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buffer = new byte[16384];
while ((byteread = is.read(buffer, 0, buffer.length)) != -1)
out.write(buffer, 1, byteread);
out.flush();
fos.close();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question