案例1
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo1; 2 3 import java.io.*; 4 import java.net.Socket; 5 6 /** 7 * 客户端 8 */ 9 public class Client { 10 public static void main(String[] args) { 11 try { 12 Socket socket = new Socket("127.0.0.1",8080); 13 OutputStream outputStream = socket.getOutputStream(); 14 String info = "下午好"; 15 //输出 16 outputStream.write(info.getBytes()); 17 //关闭 18 socket.shutdownOutput(); 19 //接收服务端的响应 20 InputStream inputStream = socket.getInputStream(); 21 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 22 while ((info=br.readLine())!=null){ 23 System.out.println("接收到了服务端的响应"+info); 24 } 25 26 //刷新缓冲区 27 outputStream.flush(); 28 outputStream.close(); 29 inputStream.close(); 30 socket.close(); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 } 35 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo1; 2 3 import java.io.*; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 /** 8 * 服务端 9 */ 10 public class Server { 11 public static void main(String[] args) { 12 try { 13 ServerSocket serverSocket = new ServerSocket(8080); 14 System.out.println("服务器启动完成.........监听中"); 15 //开启监听,等待客户端的访问 16 Socket socket = serverSocket.accept(); 17 //获取输入流,因为是客户端向服务器发送了数据 18 InputStream inputStream = socket.getInputStream(); 19 //创建一个缓冲流 20 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 21 String info = null; 22 while ((info = br.readLine())!=null){ 23 System.out.println("这里是服务端 客户端说:"+info); 24 } 25 //向客户端作出响应 26 OutputStream outputStream = socket.getOutputStream(); 27 info="这里是服务端我们接收到了你的消息"; 28 outputStream.write(info.getBytes()); 29 //刷新 30 outputStream.flush(); 31 //关闭 32 outputStream.close(); 33 } catch (IOException e) { 34 e.printStackTrace(); 35 } 36 } 37 }
运行后:
Server输出:
服务器启动完成.........监听中
这里是服务端 客户端说:下午好
Client输出:
接收到了服务端的响应这里是服务端我们接收到了你的消息
案例2
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo2; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 private String userName; 7 private String password; 8 9 @Override 10 public String toString() { 11 return "User{" + 12 "userName='" + userName + '\'' + 13 ", password='" + password + '\'' + 14 '}'; 15 } 16 17 public String getUserName() { 18 return userName; 19 } 20 21 public void setUserName(String userName) { 22 this.userName = userName; 23 } 24 25 public String getPassword() { 26 return password; 27 } 28 29 public void setPassword(String password) { 30 this.password = password; 31 } 32 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo2; 2 3 import java.io.IOException; 4 import java.io.ObjectOutputStream; 5 import java.io.OutputStream; 6 import java.net.Socket; 7 8 public class Client { 9 public static void main(String[] args) { 10 try { 11 Socket socket = new Socket("127.0.0.1",8080); 12 //获取输出流 13 OutputStream outputStream = socket.getOutputStream(); 14 //创建一个对象 15 User user =new User(); 16 user.setUserName("张三"); 17 user.setPassword("123546"); 18 //创建对象输出流 19 ObjectOutputStream oos = new ObjectOutputStream(outputStream); 20 //序列化对象 21 oos.writeObject(user); 22 //关闭流 23 oos.close(); 24 socket.close(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo2; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.ObjectInputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 public class Server { 10 public static void main(String[] args) { 11 try { 12 ServerSocket serverSocket = new ServerSocket(8080); 13 System.out.println("服务端启动完成.............."); 14 //开始监听 15 Socket socket = serverSocket.accept(); 16 InputStream inputStream = socket.getInputStream(); 17 ObjectInputStream stream = new ObjectInputStream(inputStream); 18 User user= (User) stream.readObject(); 19 System.out.println("客户端发送过来的数据是:"+user); 20 21 } catch (IOException e) { 22 e.printStackTrace(); 23 } catch (ClassNotFoundException e) { 24 e.printStackTrace(); 25 } 26 } 27 }
运行后:
Server输出:
服务端启动完成..............
客户端发送过来的数据是:User{userName='张三', password='123546'}
案例3
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo3; 2 3 import java.io.IOException; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 public class Server { 8 public static void main(String[] args) { 9 try { 10 ServerSocket serverSocket = new ServerSocket(8080); 11 while (true){ 12 Socket socket = serverSocket.accept(); 13 ServerThread serverThread = new ServerThread(socket); 14 serverThread.start(); 15 } 16 } catch (IOException e) { 17 e.printStackTrace(); 18 } 19 } 20 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package cn.kgc.sockettest.demo3; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.net.Socket; 8 9 public class ServerThread extends Thread { 10 11 private Socket socket =null; 12 13 public static int num=0; 14 15 /** 16 * 每一次连接就创建一个socket对象 17 * @param socket 18 */ 19 public ServerThread(Socket socket){ 20 this.socket=socket; 21 num++; 22 } 23 24 @Override 25 public void run() { 26 try { 27 //获取到输入流 28 InputStream inputStream = socket.getInputStream(); 29 //转成字符流对象 30 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 31 String info =null; 32 while ((info =br.readLine())!=null){ 33 System.out.println("第"+num+"个客服端说"+info); 34 } 35 br.close(); 36 inputStream.close(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 }