Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class MultiTransferServerA
- {
- private static ServerSocket serverSocket;
- private static final int PORT = 1234;
- public static void main(String[] args)
- throws IOException
- {
- try
- {
- serverSocket = new ServerSocket(PORT);
- }
- catch (IOException ioEx)
- {
- System.out.println("\nUnable to set up port!");
- System.exit(1);
- }
- do
- {
- //Wait for client...
- Socket client = serverSocket.accept();
- System.out.println("\nNew client accepted.\n");
- //Create a thread to handle communication with
- //this client and pass the constructor for this
- //thread a reference to the relevant socket...
- ClientHandler handler =
- new ClientHandler(client);
- handler.start();//As usual, method calls run.
- }while (true);
- }
- }
- class ClientHandler extends Thread
- {
- Socket socket = null;
- InputStream input = null;
- OutputStream output = null;
- public ClientHandler(Socket socket) throws IOException
- {
- //Set up reference to associated socket...
- try {
- input = socket.getInputStream();
- } catch (IOException ex) {
- System.out.println("Can't get socket input stream. ");
- }
- try {
- output = new FileOutputStream("A:\\TerimaBdariA .png");
- } catch (FileNotFoundException ex) {
- System.out.println("File not found. ");
- }
- byte[] bytes = new byte[16*1024];
- int count;
- while ((count = input.read(bytes)) > 0) {
- output.write(bytes, 0, count);
- }
- System.out.println("File received.");
- output.close();
- input.close();
- }
- public void run()
- {
- String received;
- try
- {
- if (socket!=null)
- {
- System.out.println(
- "Closing down connection...");
- socket.close();
- }
- }
- catch(IOException ioEx)
- {
- System.out.println("Unable to disconnect!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement