欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Socket网络通信】利用TCP/IP协议实现从服务端的文件中读取数据打印到客户端的控制台,服务端对客户端输入过来的数据做出响应...

程序员文章站 2022-06-06 16:07:49
...

四 .用TCP/IP协议写一个服务器。要求
   1. 客户端一连接上,则向客户端打印三句话
      第一句话:欢迎****(***是客户端的主机的名字),连接****(***服务器端主机的名字)的服务器
      第二句话:您的ip为 *******
      第三句话:继续操作请输入 Y,退出请输入“quit”(输入quit则服务结束)

   2. 如果客户端继续操作输入的是y, 则服务器端返回:
       1.查看dota英雄简介则按1  (然后读取硬盘上的 “dota英雄简介.txt”文件,并在控制台上显示)
       2. 查看dota秘籍请按2  (然后读取硬盘上的 “dota秘籍.txt”文件,并在控制台上显示)

   3.不管何时一旦客户端输入了quit,要求退出系统。

这题只需要你们把服务器写出来,然后在cmd下用telnet命令测试成功就行。

 

下面的是YangKai同学做的,用到了双向通信、内部类,客户端也写出来了,做的很不错,我把有些地方稍微改了(取消了内部类)下贴出来供大家参考。

服务端:

 

 

 

 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务端
 */
public class ServerCopy {
	private final int PORT = 8888;

	public void start() {
		try {
			ServerSocket ss = new ServerSocket(PORT);
			System.out.println("服务器启动···");
			Socket socket = null;
			while ((socket = ss.accept())!=null) {
				ClientWatcher cw = new ClientWatcher(socket);
				cw.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new ServerCopy().start();
	}
}

class ClientWatcher extends Thread{
	private Socket socket;
	private BufferedReader netBr;
	private PrintStream netPs;

	public ClientWatcher(Socket socket) {
		super();
		this.socket = socket;
		try {
			netBr = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			netPs = new PrintStream(socket.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//输出提示方法
	public void print(){
		netPs.println("1.查看dota英雄简介请按1");
		netPs.println("2.查看dota英雄秘籍请按2");
		netPs.println("3.返回上级菜单请按3");
		netPs.println("4.退出请输入quit");
	}

	public void run() {
		try {
			String clientName = socket.getInetAddress().getHostName();
			String clientIp = socket.getInetAddress().getHostAddress();
			netPs.println("欢迎" + clientName + "来到的dota资料服务器");
			netPs.println("您的地址是" + clientIp);
			netPs.println("继续操作请按y,退出请按quit");
			String line = null;
			while ((line = netBr.readLine())!= null) {
				System.out.println("客户端:"+line);
				if ("Y".equalsIgnoreCase(line)) {
					netPs.println("1.查看dota英雄简介则按1");
					netPs.println("2.查看dota英雄秘籍则按2");
					netPs.println("3.退出请按quit");
					while ((line = netBr.readLine())!= null) {
						System.out.println("客户端:"+line);
						if ("1".equalsIgnoreCase(line)) {
							String txt = readFile("e:/peixun/test/dota英雄简介.txt");
							System.out.println("读取英雄简介:"+txt);
							netPs.println(txt);
							print();  //输出提示
						} else if ("2".equalsIgnoreCase(line)) {
							String txt = readFile("e:/peixun/test/dota秘籍.txt");
							System.out.println("读取英雄秘籍:"+txt);
							netPs.println(txt);
							print();   //输出提示
						} else if ("3".equalsIgnoreCase(line)){
							netPs.println("继续操作请按y,退出请按quit");
							break;
						} else if("quit".equalsIgnoreCase(line)){
							System.out.println("与客户端"+clientName+"断开链接!");
							return;
						} else {
							netPs.println("你输入信息错误!");
							print();   //输出提示
						}
					}
				} else if ("quit".equalsIgnoreCase(line)) {
					System.out.println("与客户端"+clientName+"断开链接!");
					break;
				}else {
					netPs.println("你输入信息错误!继续操作请按y,退出请按quit");
				}
			}
			netPs.println("");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				netBr.close();
				netPs.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String readFile(String fileName) {
		StringBuffer sb = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileName));
			String line = "";
			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append("\r\n");
			}
			br.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}

 

 

 客户端:

 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 客户端
 */
public class ClientCopy {

	private final String SERVCE_ADDRESS = "localhost";
	private final int PORT = 8888;

	public void start() {
		try {
			Socket socket = new Socket(SERVCE_ADDRESS, PORT);
			String line = null;
			Scanner scan = new Scanner(System.in);
			PrintStream netPs = new PrintStream(socket.getOutputStream());
			new Watcher(socket).start();
			while ((line = scan.nextLine())!=null) {
				netPs.println(line);
				if("quit".equals(line)) {
					System.out.println("与服务器断开连接!");
					break;
				}
			}
			netPs.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new ClientCopy().start();
	}
}

class Watcher extends Thread {
	private Socket socket;
	private BufferedReader netBr;

	public Watcher(Socket socket) {
		super();
		this.socket = socket;
		try {
			netBr = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.setDaemon(true);
	}

	public void run() {
		try {
			String line;
			while ((line = netBr.readLine()) != null) {
				System.out.println(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				netBr.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}