다크호스 프로그래머네트워크 프로 그래 밍 - 코드 구현

5283 단어 serverclient
당신 과 교류 하 기 를 기대 합 니 다!
 
 
클 라 이언 트
package cn.itcast.b_upload;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
	public static void main(String[] args) throws IOException {
		// 1.                .
		File file = getFile();
		System.out.println(file);

		// 2.      Socket.
		Socket socket = new Socket("192.168.10.1", 5000);

		// 3.      socket        .
		InputStream in = socket.getInputStream();
		OutputStream out = socket.getOutputStream();

		// 4.                .
		out.write(file.getName().getBytes());

		// 5.            .
		byte[] buffer = new byte[1024];
		int len;
		len = in.read(buffer);
		String mess = new String(buffer, 0, len);

		// 6.         ,        .
		if ("        ".equals(mess)) {
			System.out.println(mess + ",       !!!");
			socket.close();
			return;
		} else {
			System.out.println(mess);
		}

		System.out.println("       !!!");
		// 7.            .         .
		FileInputStream fis = new FileInputStream(file);
		// 8.             .
		while ((len = fis.read(buffer)) != -1) {
			// 8.    socket         .
			out.write(buffer, 0, len);
		}

		// 9.       ,      .
		socket.shutdownOutput();

		// 10.               .
		len = in.read(buffer);
		System.out.println(new String(buffer, 0, len));

		// 11.      socket         .
		fis.close();
		socket.close();

	}

	public static File getFile() throws IOException {
		// 1.                 .
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		while (true) {
			System.out.println("          :");
			String filePath = br.readLine();

			File file = new File(filePath);
			// 2.     .
			if (!file.exists()) {
				System.out.println("     ,     . ");
				continue;
			}
			// 2.      .
			if (file.isDirectory()) {
				System.out.println("        ,     !!!");
				continue;
			}

			return file;
		}
	}
}

 서버
package cn.itcast.b_upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	public static void main(String[] args) throws IOException {
		// 1.      .
		System.out.println("tcp      .      ");
		ServerSocket server = new ServerSocket(5000);

		while (true) {
			// 2. accept          .
			final Socket socket = server.accept();

			new Thread(new Runnable() {

				@Override
				public void run() {

					String ip = socket.getInetAddress().getHostAddress();
					System.out.println("   :" + ip + "   ..");

					try {
						// 3.      socket        .
						InputStream in = socket.getInputStream();
						OutputStream out = socket.getOutputStream();

						byte[] buffer = new byte[1024 * 8];

						int len;
						// 4.          .
						len = in.read(buffer);
						String fileName = new String(buffer, 0, len);

						// 5.               ...
						File dir = new File("e:\\upload");
						//          .      ,    .
						if (!dir.exists()) {
							dir.mkdir();
						}

						// 6.            ,     File   .
						File file = new File(dir, fileName);

						// 7.    ,         .            .
						if (file.exists()) {
							out.write("        ".getBytes());
							socket.close();
							return;
						} else {
							out.write("         ".getBytes());
						}

						System.out.println("       ");
						// 8.              .  socket               .
						FileOutputStream fos = new FileOutputStream(file);
						// 9.        .
						while ((len = in.read(buffer)) != -1) {
							fos.write(buffer, 0, len);
						}

						System.out.println("       !!!");
						// 10.        ,           .
						out.write("         ".getBytes());

						// 11.               .
						fos.close();
					} catch (IOException e) {
						// e.printStackTrace();
					} finally {
						try {
							socket.close();
						} catch (IOException e) {
							// e.printStackTrace();
						}
						System.out.println("   :" + ip + "   ..");
					}

				}
			}).start();

		}

	}
}
 

좋은 웹페이지 즐겨찾기