标签:ack span accept bsp reply str buffere 阻塞 zab
1 import java.io.Serializable; 2 3 /** 4 * 用户类 5 * */ 6 public class User implements Serializable{ 7 private String loginName; //用户名 8 private String pwd; //用户密码 9 10 public User() { 11 } 12 public User(String loginName, String pwd) { 13 super(); 14 this.loginName = loginName; 15 this.pwd = pwd; 16 } 17 18 public String getLoginName() { 19 return loginName; 20 } 21 public void setLoginName(String loginName) { 22 this.loginName = loginName; 23 } 24 public String getPwd() { 25 return pwd; 26 } 27 public void setPwd(String pwd) { 28 this.pwd = pwd; 29 } 30 31 }
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.ObjectInputStream; 6 import java.io.OutputStream; 7 import java.io.PrintWriter; 8 import java.net.ServerSocket; 9 import java.net.Socket; 10 11 import pb.socket.entity.User; 12 13 /** 14 * 服务器端 15 * */ 16 public class LoginServer { 17 public static void main(String[] args) { 18 try { 19 //1.建立一个服务器Socket(ServerSocket)绑定指定端口并开始监听 20 ServerSocket serverSocket=new ServerSocket(8800); 21 //2.使用accept()方法阻塞等待监听,获得新的连接 22 Socket socket=serverSocket.accept(); 23 //3.获得输入流 24 InputStream is=socket.getInputStream(); 25 //获得流:可以对对象进行反序列化 26 ObjectInputStream ois=new ObjectInputStream(is); 27 //获得输出流 28 OutputStream os=socket.getOutputStream(); 29 PrintWriter pw=new PrintWriter(os); 30 //4.读取用户输入信息 31 User user=(User)ois.readObject(); 32 System.out.println("用户信息:"+user.getLoginName()+"---"+user.getPwd()); 33 //给客户一个响应 34 String reply="welcome!"; 35 pw.write(reply); 36 pw.flush(); 37 //5.关闭资源 38 pw.close(); 39 os.close(); 40 ois.close(); 41 is.close(); 42 socket.close(); 43 serverSocket.close(); 44 } catch (IOException e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } catch (ClassNotFoundException e) { 48 // TODO Auto-generated catch block 49 e.printStackTrace(); 50 } 51 52 } 53 }
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.io.ObjectOutputStream; 6 import java.io.OutputStream; 7 import java.io.PrintWriter; 8 import java.net.Socket; 9 import java.net.UnknownHostException; 10 11 import pb.socket.entity.User; 12 13 /** 14 * 客户端 15 * */ 16 public class LoginClient { 17 public static void main(String[] args) { 18 try { 19 //1.建立客户端Socket连接,指定服务器的位置以及端口 20 Socket socket=new Socket("localhost",8800); 21 //2.得到Socket的读写流 22 OutputStream os=socket.getOutputStream(); 23 //对象序列化流 24 ObjectOutputStream oos=new ObjectOutputStream(os); 25 //输入流 26 InputStream is=socket.getInputStream(); 27 BufferedReader br=new BufferedReader(new InputStreamReader(is)); 28 //3.利用流按照一定的协议对Socket进行读/写操作 29 User user=new User(); 30 user.setLoginName("Tom"); 31 user.setPwd("123456"); 32 oos.writeObject(user); 33 34 socket.shutdownOutput(); 35 //接受服务器的响应并打印显示 36 String reply=null; 37 while(!((reply=br.readLine())==null)){ 38 System.out.println("我是客户端,服务器的响应为:"+reply); 39 } 40 //4.关闭资源 41 br.close(); 42 is.close(); 43 oos.close(); 44 os.close(); 45 socket.close(); 46 } catch (UnknownHostException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } catch (IOException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 54 } 55 }
标签:ack span accept bsp reply str buffere 阻塞 zab
原文地址:https://www.cnblogs.com/kuangyefeige/p/9822874.html