Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Server {
- private static final int PORT = 5000;
- private static final String FILE_NAME = "out.txt";
- private static final int BUFFER_SIZE = 1024;
- public static void main(String[] args) {
- try {
- ServerSocket serverSocket = new ServerSocket(PORT);
- report("Waiting for connection..");
- Socket connection = serverSocket.accept();
- report("Received connection from " + connection.getInetAddress().getHostName());
- FileOutputStream output = new FileOutputStream(new File(FILE_NAME));
- BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
- byte[] buffer = new byte[BUFFER_SIZE];
- int bytesRead = -1;
- while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) {
- output.write(buffer, 0, bytesRead);
- }
- report("Write finished");
- output.close();
- input.close();
- connection.close();
- serverSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void report(String message) {
- System.out.println(message);
- }
- }
Add Comment
Please, Sign In to add comment