package cn.itcast.tcp; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; //下载图片的客户端 public class ImageClient { public static void main(String[] args) throws Exception{ //建立tcp的服务 Socket socket = new Socket(InetAddress.getLocalHost(),9090); //获取socket的输入流对象 InputStream inputStream = socket.getInputStream(); //获取文件的输出流对象 FileOutputStream fileOutputStream = new FileOutputStream("F:\\3.jpg"); //边读边写 byte[] buf = new byte[1024]; int length = 0 ; while((length = inputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } //关闭资源 fileOutputStream.close(); socket.close(); } }
package cn.itcast.tcp; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.HashSet; /* 1. 编写一个服务端可以给多个客户端发送图片。 (多线程) */ public class ImageServer extends Thread { Socket socket ; //使用该集合是用于存储ip地址的。 static HashSet<String> ips = new HashSet<String>(); public ImageServer(Socket socket) { this.socket = socket; } @Override public void run() { try { //获取到socket输出流对象 OutputStream outputStream = socket.getOutputStream(); //获取图片的输入流对象 FileInputStream fileInputStream = new FileInputStream("F:\\美女\\3.jpg"); //读取图片数据,把数据写出 byte[] buf = new byte[1024]; int length = 0 ; while((length = fileInputStream.read(buf))!=-1){ outputStream.write(buf,0,length); } String ip = socket.getInetAddress().getHostAddress(); // socket.getInetAddress() 获取对方的IP地址 if(ips.add(ip)){ System.out.println("恭喜"+ip+"同学成功下载,当前下载的人数是:"+ ips.size()); } //关闭资源 fileInputStream.close(); socket.close(); }catch (IOException e) { } } public static void main(String[] args) throws IOException { //建立tcp的服务 ,并且要监听一个端口 ServerSocket serverSocket = new ServerSocket(9090); while(true){ //接受用户的链接。 Socket socket = serverSocket.accept(); new ImageServer(socket).start(); } } }
package cn.itcast.tcp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.InetAddress; import java.net.Socket; /* 2. 实现登陆与注册 功能。 客户端与服务端连接的时候,就要提示客户端请选择功能。 客户端注册的时候,用户名与密码都是发送给服务端 的,服务端需要把数据保存到服务端的文件上。 登陆: 登陆的时候客户端输入用户名与密码发送给服务端,服务端需要校验,返回结果给客户端。 */ public class LoginClinet { public static void main(String[] args) throws IOException { Socket socket = new Socket(InetAddress.getLocalHost(),9090); //获取socket的输出流对象 OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream()); //获取到socket的输入流对象 BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); //获取到键盘的输入流对象 BufferedReader keyReader = new BufferedReader(new InputStreamReader(System.in)); while(true){ System.out.println("请选择功能: A(登陆) B(注册)"); String option = keyReader.readLine(); if("a".equalsIgnoreCase(option)){ getInfo(socketOut, keyReader, option); //读取服务器反馈的信息 String line = socketReader.readLine(); System.out.println(line); }else if("b".equalsIgnoreCase(option)){ getInfo(socketOut, keyReader, option); //读取服务器反馈的信息 String line = socketReader.readLine(); System.out.println(line); } } } public static void getInfo(OutputStreamWriter socketOut,BufferedReader keyReader, String option) throws IOException { System.out.println("请输入用户名:"); String userName = keyReader.readLine(); System.out.println("请输入密码:"); String password = keyReader.readLine(); String info = option +" "+userName+" "+password+"\r\n"; socketOut.write(info); socketOut.flush(); } }
package cn.itcast.tcp; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Properties; public class LoginServer extends Thread { Socket socket; static File file = new File("F:\\users.properties"); public LoginServer(Socket socket) { this.socket = socket; } static { try { if (!file.exists()) { file.createNewFile(); } } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { while(true){ try { // 获取socket的输入流对象 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(socket.getInputStream())); // 获取socket的输出流对象 OutputStreamWriter socketOut = new OutputStreamWriter(socket.getOutputStream()); // 读取客户端输入的信息 String info = bufferedReader.readLine(); String[] datas = info.split(" "); // 获取到用户 的选择功能 String option = datas[0]; // 注册 String userName = datas[1]; String password = datas[2]; if ("a".equalsIgnoreCase(option)) { // 登陆 Properties properties = new Properties(); // 加载配置文件 properties.load(new FileReader(file)); if (properties.containsKey(userName)) { String tempPass = properties.getProperty(userName); if (password.equals(tempPass)) { socketOut.write("欢迎" + userName + "登陆成功\r\n"); } else { socketOut.write("密码错误\r\n"); } } else { socketOut.write("用户名不存在,请重新输入...\r\n"); } socketOut.flush(); } else if ("b".equalsIgnoreCase(option)) { // 创建一个配置文件类 Properties properties = new Properties(); //加载原来的配置文件 properties.load(new FileReader(file)); if (!properties.containsKey(userName)) { // 不存在该用户名 properties.setProperty(userName, password); // 生成一个配置文件 properties.store(new FileWriter(file), "users"); socketOut.write("注册成功..\r\n"); } else { // 存在用户名 socketOut.write("用户名已经被注册,请重新输入\r\n"); } socketOut.flush(); } } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(9090); while (true) { Socket socket = serverSocket.accept(); new LoginServer(socket).start(); } } }
时间: 2024-10-24 17:41:05