示例一:多客户端登录(使用线程)
客户端:
1 package cn.kgc.demo6; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.io.ObjectOutputStream; 9 import java.io.OutputStream; 10 import java.io.OutputStreamWriter; 11 import java.net.Socket; 12 import java.net.UnknownHostException; 13 import java.util.Scanner; 14 15 //客户端 16 public class Client { 17 public static void main(String[] args) { 18 Scanner input = new Scanner(System.in); 19 try { 20 //在new对象的时候,就会建立连接 21 Socket server =new Socket("192.168.31.40",10000); 22 23 //接收服务器的数据 24 InputStream in = server.getInputStream(); 25 26 //向服务器发送数据 27 OutputStream out = server.getOutputStream(); 28 29 //登录: 30 System.out.println("请输入用户名"); 31 String username = input.nextLine(); 32 System.out.println("请输入密码"); 33 String password = input.nextLine(); 34 User user = new User(username,password); 35 //创建对象输出流 36 ObjectOutputStream oos = new ObjectOutputStream(out); 37 oos.writeObject(user); 38 39 byte[] bytes=new byte[1024]; 40 int len = in.read(bytes); 41 System.out.println("服务器响应:" + new String(bytes,0,len)); 42 43 oos.close(); 44 45 } catch (UnknownHostException e) { 46 e.printStackTrace(); 47 } catch (IOException e) { 48 e.printStackTrace(); 49 } 50 } 51 }
服务端:
1 package cn.kgc.demo6; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.ObjectInputStream; 6 import java.io.OutputStream; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 10 11 public class Server { 12 public static void main(String[] args) { 13 ServerSocket server=null; 14 try { 15 System.out.println("服务器启动..."); 16 server = new ServerSocket(10000); 17 18 while(true){ 19 //一个socket就是一个客户 20 Socket socket = server.accept(); 21 //找一个员工来服务客户 22 new SockerHandler(socket).start(); 23 } 24 25 } catch (IOException e) { 26 e.printStackTrace(); 27 }finally{ 28 try { 29 server.close(); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 } 34 } 35 } 36 37 class SockerHandler extends Thread{ 38 private Socket socket; 39 40 public SockerHandler(Socket socket) { 41 super(); 42 this.socket = socket; 43 } 44 45 @Override 46 public void run() { 47 System.out.println("in..."); 48 try { 49 InputStream in = socket.getInputStream(); 50 ObjectInputStream ois = new ObjectInputStream(in); 51 User user = (User) ois.readObject(); 52 String result = ""; 53 if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) { 54 //登录成功 55 result = "登录成功"; 56 } else { 57 //登录失败 58 result = "用户名或密码不正确"; 59 } 60 System.out.println("招待"+socket +"完毕"); 61 //向客户端返回信息 62 OutputStream out = socket.getOutputStream(); 63 out.write(result.getBytes()); 64 out.flush(); 65 out.close(); 66 67 } catch (ClassNotFoundException e) { 68 e.printStackTrace(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 finally{ 73 try { 74 socket.close(); 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 } 80 81 }
对象:
1 package cn.kgc.demo6; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private String username; 7 private String password; 8 9 public User() { 10 } 11 12 public User(String username, String password) { 13 super(); 14 this.username = username; 15 this.password = password; 16 } 17 public String getUsername() { 18 return username; 19 } 20 public void setUsername(String username) { 21 this.username = username; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 30 31 }
示例二:上传文件
客户端:
1 package cn.kgc.demo9; 2 3 import java.io.DataOutputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.IOException; 7 import java.io.OutputStream; 8 import java.net.Socket; 9 import java.util.Scanner; 10 11 //上传文件的客户端 12 public class Client { 13 public static void main(String[] args) throws Exception { 14 Scanner input = new Scanner(System.in); 15 //和接收文件的服务器建立连接 16 Socket server = new Socket("192.168.31.40",9000); 17 OutputStream out = server.getOutputStream(); 18 19 DataOutputStream dos = new DataOutputStream(out); 20 String command; 21 while (true) { 22 System.out.println("请输入要操作的指令:"); 23 command = input.nextLine(); 24 //发送指令到服务器 25 dos.writeUTF(command); 26 if("upload".equals(command)){ 27 System.out.println("请输入要上传文件的路径:"); 28 String filePath=input.nextLine(); 29 uploadFile(filePath,dos); 30 }else if("quit".equals(command)){ 31 System.out.println("退出文件上传系统"); 32 break; 33 } 34 } 35 36 37 38 } 39 40 public static void uploadFile(String filename,DataOutputStream out) throws IOException{ 41 File file = new File(filename); 42 //如果文件不存在,结束 43 if (!file.exists()) { 44 return ; 45 } 46 String name = file.getName(); 47 System.out.println(name); 48 long length = file.length(); 49 //发送数据 50 out.writeUTF(name); 51 out.writeLong(length); 52 53 FileInputStream in = new FileInputStream(file); 54 byte[] bytes=new byte[1024]; 55 int len; 56 while( (len=in.read(bytes)) != -1 ){ 57 out.write(bytes, 0, len); 58 } 59 in.close(); 60 61 } 62 } 63 64 // 请输入要操作的指令:upload 65 // 请输入要上传文件的路径:C:/xxxx.xx 66 // 向服务器发送文件名 67 // 向服务器发送文件大小 68 // 向服务器发送具体的文件内容 69 70 // 如果输入的是: quit 退出
服务端:
1 package cn.kgc.demo9; 2 3 import java.net.ServerSocket; 4 import java.net.Socket; 5 6 public class Server { 7 public static void main(String[] args) throws Exception { 8 //做为服务器要创建的是ServerSocket 9 System.out.println("服务器启动..."); 10 ServerSocket server = new ServerSocket(9000); 11 Socket socket; 12 while(true){ 13 socket = server.accept(); 14 System.out.println(socket+" 连接到服务器"); 15 new Thread(new UploadFileTask(socket)).start(); 16 } 17 } 18 }
1 package cn.kgc.demo9; 2 3 import java.io.DataInputStream; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.net.Socket; 9 10 public class UploadFileTask implements Runnable { 11 12 private Socket socket; 13 private InputStream in; 14 15 public UploadFileTask(Socket socket) { 16 this.socket = socket; 17 try { 18 this.in = socket.getInputStream(); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 } 23 24 @Override 25 public void run() { 26 DataInputStream dis = new DataInputStream(in); 27 try { 28 String command; 29 while(true){ 30 command = dis.readUTF(); 31 32 System.out.println("命令:"+command); 33 if("upload".equals(command)){ 34 //接收文件 35 reciveFile(dis); 36 37 }else if("quit".equals(command)){ 38 System.out.println(socket + " 断开连接"); 39 socket.close(); 40 break; 41 } 42 } 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } 46 47 } 48 49 private void reciveFile(DataInputStream ins) throws IOException { 50 String filename = ins.readUTF(); 51 System.out.println("接收的文件名:" + filename); 52 long length = ins.readLong(); 53 System.out.println("接收的文件大小:" + length); 54 byte[] buf = new byte[1024]; 55 File saveFile = new File("D:/",filename); 56 FileOutputStream fos = new FileOutputStream(saveFile); 57 //5000 58 //1024 1024 1024 1024 904 59 int count = (int)(length / 1024); //4 60 for(int i=0;i<count ;i++){ 61 ins.read(buf); 62 fos.write(buf); 63 } 64 int lastLength = (int) (length % 1024); 65 ins.read(buf, 0, lastLength); 66 fos.write(buf,0,lastLength); 67 fos.close(); 68 System.out.println("保存"+ socket+"的文件:" + filename+"成功"); 69 70 71 } 72 73 }