其实web 开发的底层就是socket 编程
数据是通过流读取
服务端
public class WeatherServer { public static void main(String[] args) throws IOException { // 创建socket 服务,应用服务端口建议在1万以上 ServerSocket server = null; Socket socket = null; DataInputStream di=null ; DataOutputStream ds=null ; try { server = new ServerSocket(12345); // 按受客户端链接 socket = server.accept(); //为了好处理字符串 // 接受客户端请求 di=new DataInputStream(socket.getInputStream()); // 向服务端发送请求 ds=new DataOutputStream(socket.getOutputStream()); //客户端向服务端发送的天气所在城市 String cityName =di.readUTF(); System.out.println("from client...."+cityName); //服务端向客户端返回天气情况 String result ="晴"; ds.writeUTF("晴"); System.out.println("to client...."+result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { di.close(); ds.close(); //服务端一般是不关闭资源的,由客户端关闭 //socket.close(); } } }
客户端代码:
public class WeatherClient { public static void main(String[] args) throws UnknownHostException, IOException { Socket client = null; DataInputStream di = null; DataOutputStream ds = null; client = new Socket("127.0.0.1", 12345); try { // 接受客户端请求 di = new DataInputStream(client.getInputStream()); // 向服务端发送请求 ds = new DataOutputStream(client.getOutputStream()); String cityName ="北京"; ds.writeUTF(cityName); String result =di.readUTF(); System.out.println("接受服务端.."+result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { di.close() ; ds.close(); client.close(); } } }
其实服务端开发要有可持续能力,保证线程一直在运行,那么上面的代码就可以改成这样:
public class WeatherServerV2 { public static void main(String[] args) throws IOException { // 创建socket 服务,应用服务端口建议在1万以上 ServerSocket server = null; Socket socket = null; DataInputStream di=null ; DataOutputStream ds=null ; while(true){ try { server = new ServerSocket(12345); // 按受客户端链接 socket = server.accept(); //为了好处理字符串 // 接受客户端请求 di=new DataInputStream(socket.getInputStream()); // 向服务端发送请求 ds=new DataOutputStream(socket.getOutputStream()); //客户端向服务端发送的天气所在城市 String cityName =di.readUTF(); System.out.println("from client...."+cityName); //服务端向客户端返回天气情况 String result ="晴"; ds.writeUTF("晴"); System.out.println("to client...."+result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { di.close(); ds.close(); //服务端一般是不关闭资源的,由客户端关闭 //socket.close(); } } } }
同样的道理,服务端不仅要有持续运行能力,还要有处理并发的能力,我们可以把客户端也改成死循环
public class WeatherClientVersion2 { public static void main(String[] args) throws UnknownHostException, IOException { Socket client = null; DataInputStream di = null; DataOutputStream ds = null; client = new Socket("127.0.0.1", 12345); while(true){ try { // 接受客户端请求 di = new DataInputStream(client.getInputStream()); // 向服务端发送请求 ds = new DataOutputStream(client.getOutputStream()); String cityName ="北京"; ds.writeUTF(cityName); String result =di.readUTF(); System.out.println("接受服务端.."+result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { di.close() ; ds.close(); client.close(); } } } }
时间: 2024-09-28 11:26:41