网络编程



服务(service)
Tcp 编程

服务器
 a.Ip地址(域名)
 b.不同的端口SeverSocket,=<65535,
  <1000,计算机留作自用,一般不推荐使用
 c.每个端口可以建立多个Socket实例,每个实例代表一个对客户的服务
客户端
 a.使用域名(IP)找到指定服务器
 b.使用服务器的端口号找到服务
 c.建立Socket对象
 d.使用流与服务器通信

服务器端:

package Net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class SocketDemo {
	public static void main(String[] args) throws Exception {
		SocketDemo service =new SocketDemo();
		service.listen(8888);
	}

	private void listen(int i) throws Exception {
		@SuppressWarnings("resource")
		ServerSocket ss=new ServerSocket(i);
		while(true){
			Socket s=ss.accept();
			new MyThread(s).start();;
		}
	}
	class MyThread extends Thread{
		Socket s;
		public MyThread(Socket s) {
			this.s=s;
		}
		@Override
		public void run() {
			try {
				BufferedReader In=new BufferedReader(new InputStreamReader(s.getInputStream()));
				PrintWriter Out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
				Out.println("吃点什么?");
				Out.flush();
				System.out.println(In.readLine());
				Scanner sc=new Scanner(System.in);
				String str=sc.nextLine();
				Out.println(str);
				Out.flush();
				sc.close();
				s.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

客户端

package Net;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ClientDemo {

	public static void main(String[] args) throws Exception {
		Socket socket =new Socket("localhost", 8888);
		BufferedReader In=new BufferedReader(new InputStreamReader(socket.getInputStream()));
		PrintWriter Out=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
		System.out.println(In.readLine());
		Scanner sc =new Scanner(System.in);
		String str=sc.nextLine();
		Out.println(str);
		Out.flush();
		System.out.println(In.readLine());
		sc.close();
		socket.close();
	}

}


简单TFP

package Net;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class FtpClient {
	public static void main(String[] args) throws IOException{
		Link(8888);
	}

	private static void Link(int i) throws IOException {
		@SuppressWarnings("resource")
		Socket socket =new Socket("localhost",8866);
		BufferedReader In=new BufferedReader(new InputStreamReader(socket.getInputStream()));
		PrintWriter Out=new PrintWriter(socket.getOutputStream(),true);
		new FromFtpServer(In).start();
		new ToFtpServer(Out).start();
	}
	
}
class FromFtpServer extends Thread{
	BufferedReader In;
	String serverStr;
	public FromFtpServer(BufferedReader In) {
		this.In=In;
	}
	@Override
	public void run() {
		try {
			String head;
			int length;
			while((serverStr=In.readLine())!=null){
				head=serverStr.split(",")[0];
				length=Integer.parseInt(serverStr.split(",")[1]);
				if(head.equals("T")|head.equals("L")|head.equals("?")){
					for (int i = 0; i < length; i++) {
						System.out.println(In.readLine());
					}
				}else if(head.equals("f")){
					String fileName=serverStr.split(",")[2];
					System.out.println(fileName);
					String Name=fileName.substring(0,fileName.indexOf("."))+"1"+fileName.substring(fileName.indexOf("."));
					System.out.println();
					BufferedOutputStream bs=new BufferedOutputStream(new FileOutputStream(Name));
					for (int i = 0; i < length; i++) {
						bs.write(In.read());
						System.out.println(i+" "+length);
					}
					bs.close();
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class ToFtpServer extends Thread{
	PrintWriter Out;
	String ToFtpServerStr;
	public ToFtpServer(PrintWriter Out) {
		this.Out=Out;
	}
	@Override
	public void run() {
		@SuppressWarnings("resource")
		Scanner scanner=new Scanner(System.in);
		while(true){
			ToFtpServerStr=scanner.nextLine();
			Out.println(ToFtpServerStr);
		}
	}
}
package Net;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class FtpServer {

	public static void main(String[] args) throws IOException {
		@SuppressWarnings("unused")
		FtpServer server = new FtpServer();
		Listen(8866);
	}

	private static void Listen(int i) throws IOException {
		@SuppressWarnings("resource")
		ServerSocket ss = new ServerSocket(i);
		int num=1;
		while (true) {
			System.out.println("等待客户端"+num+"连接");
			Socket socket = ss.accept();
			System.out.println("客户端"+(num++)+"已连接");
			new ServerThread(socket).start();
		}
	}
}
class ServerThread extends Thread {
	Socket socket = new Socket();
	public ServerThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		try {
			String InStr = null;
			BufferedReader In = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			PrintWriter Out = new PrintWriter(socket.getOutputStream(),
					true);// 自动调用Out.flush()方法,仅对Out.Println()适用.
			Out.println("T,2");
			Out.println("欢迎使用FtpDemo");
			Out.println("使用方法见命令\'?\'");
			while (true) {
				InStr = In.readLine();
				if (InStr.trim().equals("?")) {
					Out.println("?,1");
					Out.println("\'?\',\'ls\',\'getXX\',\'exit\'");
				} else if (InStr.trim().toLowerCase().equals("ls")) {
					File file=new File(".");
					String[] fileList =file.list(new ListFileFilter());
					Out.println("L,"+fileList.length);
					for (int i = 0; i < fileList.length; i++) {
						Out.println(fileList[i]);
					}
				}else if (InStr.trim().split("\\s+")[0].toLowerCase().equals("get")) {
					String fileName=InStr.split("\\s+")[1];
//					System.out.println(fileName);
					File file =new File(fileName);
					if(!file.exists()){
						System.out.println(fileName+"文件不存在!");
						return;
					}
					Out.println("f,"+file.length()+","+fileName);
					InputStream br=new BufferedInputStream(new FileInputStream(file));
					int b;
					int i=0;
					while((b=br.read())!=-1){
						Out.write(b);
						Out.flush();
						System.out.println(i++);
					}
					System.out.println("传送完成");
					br.close();
				}else{
					Out.println("T,1");
					Out.println("无此命令");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

class ListFileFilter implements FilenameFilter{
	@Override
	public boolean accept(File arg0, String arg1) {
		if(arg0.isDirectory()){
			return true;
		}
		return false;
	}
}



你可能感兴趣的:(JAVA基础)