Socket编程
package cn.bdqn.tcp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * @author 小豆腐 * * 服务器(超市) * */ public class ServerSocketTest { public static void main(String[] args) { ServerSocket serverSocket=null; Socket socket=null; //顾客 InputStream stream=null; //购物车 BufferedReader br=null; //收银员 OutputStream os=null; //收银员的回应语句 try { //创建一个指定位置的超市 serverSocket=new ServerSocket(8800); //超市开门 等待顾客上门购物 socket=serverSocket.accept(); //顾客开始购物 stream=socket.getInputStream(); /** * 买单 * BufferedReader收银员拿InputStreamReader(扫码枪) * 扫购物车中的物品 stream */ br=new BufferedReader(new InputStreamReader(stream)); /** * 怎么扫码的?只要是购物车中有物品就 一个一个扫码 * info理解成 账单 * 每扫描一次,账单就多一个记录 */ String info=null; while((info=br.readLine())!=null){ //说明购物车中有东西 System.out.println("顾客的账单====》"+info); } //只是说 顾客结账完毕 并没有走出超市(关闭) socket.shutdownInput(); os=socket.getOutputStream(); //打开输出流 //服务员说:谢谢光临! os.write("谢谢您的光临!".getBytes()); } catch (IOException e) { e.printStackTrace(); }finally{ try { os.close(); //超市关门 这个超市 只接待了一个顾客 就关门了??? br.close(); stream.close(); socket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package cn.bdqn.tcp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; /** * * @author 小豆腐 * * *客户端(顾客) */ public class SocketTest { public static void main(String[] args) { Socket socket=null; //顾客 OutputStream os=null; //钱包 InputStream stream=null; //账单 BufferedReader br=null; //收银员 try { //去哪个地方(localhost)的哪个超市(8800)购物? socket=new Socket("localhost", 8800); //打开钱包,疯狂购物 花钱===》出! os=socket.getOutputStream(); //开始消费 买什么东西? os.write("我买了一束玫瑰花!".getBytes()); //购物完毕 socket.shutdownOutput(); //接收收银员的回应 stream=socket.getInputStream(); br=new BufferedReader(new InputStreamReader(stream)); String info=null; while((info=br.readLine())!=null){ System.out.println(info); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ //关闭资源 try { br.close(); stream.close(); os.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
传递一个对象
package cn.bdqn.tcp.object; import java.io.Serializable; /** * * @author 小豆腐 * 用户的实体类 */ public class User implements Serializable { private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public User(String name, String pwd) { super(); this.name = name; this.pwd = pwd; } public User() { super(); } @Override public String toString() { return "User [name=" + name + ", pwd=" + pwd + "]"; } }
package cn.bdqn.tcp.object; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * * @author 小豆腐 * *服务器 */ public class ServerLogin { public static void main(String[] args) { ServerSocket serverSocket=null; Socket socket=null; InputStream is=null; OutputStream os=null; //反序列化 ObjectInputStream ois=null; try { //创建serverSocket serverSocket=new ServerSocket(8800); //监听客户端的登录信息 socket=serverSocket.accept(); //打开输入输出流 is=socket.getInputStream(); os=socket.getOutputStream(); ois=new ObjectInputStream(is); //获取登录用户的信息 User user=(User) ois.readObject(); System.out.println("登录用户的用户名是:"+user.getName()); System.out.println("登录用户的密码是:"+user.getPwd()); socket.shutdownInput(); //服务器给客户端一个响应 os.write("恭喜您!登录成功!".getBytes()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally{ try { //释放资源 os.close(); socket.close(); ois.close(); is.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package cn.bdqn.tcp.object; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; /** * * @author 小豆腐 * *客户端 */ public class ClientLogin { public static void main(String[] args) { Socket socket=null; InputStream is=null; OutputStream os=null; //序列化 ObjectOutputStream oos=null; BufferedReader br=null; try { //创建socket对象 socket=new Socket("localhost", 8800); //打开输入输出流 is=socket.getInputStream(); os=socket.getOutputStream(); //创建一个登录的对象 User user=new User("小白", "123456"); oos=new ObjectOutputStream(os); //开始把对象发送给服务器 oos.writeObject(user); socket.shutdownOutput(); //接收服务器的回应 br=new BufferedReader(new InputStreamReader(is)); String info=null; while((info=br.readLine())!=null){ System.out.println("服务器给我们的回应是:"+info); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { //释放资源 br.close(); socket.close(); oos.close(); os.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
服务器循环监听客户端的登陆信息, 客户端代码和User类不变
package cn.bdqn.tcp.thread; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * * @author 小豆腐 * 服务器 */ public class ServerLogin { public static void main(String[] args) { ServerSocket serverSocket=null; Socket socket=null; try { //创建服务器的serverSocket对象 serverSocket=new ServerSocket(8800); //一直监听 while(true){ socket=serverSocket.accept(); //创建对应的线程类 LoginThread thread=new LoginThread(socket); //启动 thread.start(); } } catch (IOException e) { e.printStackTrace(); } } }
需要的线程类
package cn.bdqn.tcp.thread; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.net.Socket; import cn.bdqn.tcp.object.User; /** * * @author 小豆腐 * 服务器监听的线程类 * */ public class LoginThread extends Thread { Socket socket=null; public LoginThread(Socket socket) { this.socket=socket; } @Override public void run() { InputStream is=null; OutputStream os=null; //反序列化 ObjectInputStream ois=null; try { //打开输入输出流 is=socket.getInputStream(); os=socket.getOutputStream(); ois=new ObjectInputStream(is); //获取登录用户的信息 User user=(User) ois.readObject(); System.out.println("登录用户的用户名是:"+user.getName()); System.out.println("登录用户的密码是:"+user.getPwd()); socket.shutdownInput(); //服务器给客户端一个响应 os.write("恭喜您!登录成功!".getBytes()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally{ try { //释放资源 os.close(); socket.close(); ois.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } } }