理解一下servlet
Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
理解一下TCP/IP协议
TCP/IP不是一个协议,而是一个协议族的统称。里面包括了IP协议,IMCP协议,TCP协议,以及我们更加熟悉的http、ftp、pop3协议等等。电脑有了这些,就好像学会了外语一样,就可以和其他的计算机终端做自由的交流了。
通过代码加深一下印象。
客户端代码Client.java
package com.java.tcp; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import com.java.util.PrintUtil; public class Client { public static void main(String[] args) { try { Socket socket = new Socket("localhost",8888); //向服务端输出 OutputStream outputStream = socket.getOutputStream(); PrintWriter printWriter = new PrintWriter(outputStream); String clientName = Integer.toString( (int)(Math.random()*100) ); printWriter.print("服务端你好,我是客户端"+clientName+",今天天气不错啊"); printWriter.flush(); socket.shutdownOutput(); //获取客户端信息 InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String temp = null; StringBuilder info = new StringBuilder(); while( (temp = bufferedReader.readLine()) !=null ) { info.append(temp); } PrintUtil.show("服务端说:"+info); socket.shutdownInput(); //关闭流对象等 printWriter.close(); outputStream.close(); bufferedReader.close(); inputStreamReader.close(); inputStream.close(); socket.close(); } catch (UnknownHostException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
服务端代码ServerThread.java
import com.java.util.PrintUtil; public class ServerThread extends Thread { private Socket socket; public ServerThread(Socket socketIn) { this.socket = socketIn; } public void run() { try { //读取客户端的信息 InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String temp = null; StringBuilder infoBuilder = new StringBuilder(); while((temp = bufferedReader.readLine()) !=null) { infoBuilder.append(temp); } PrintUtil.show("已经接收到客户端的连接"); PrintUtil.show("服务端收到客户端信息:"+infoBuilder.toString()); PrintUtil.show("服务端地址为:"+socket.getInetAddress().getHostAddress()); socket.shutdownInput(); //向客户端发送信息 OutputStream outputStream = socket.getOutputStream(); PrintWriter printWriter = new PrintWriter(outputStream); printWriter.print("您好,服务端已经接收到您的信息"); printWriter.flush(); socket.shutdownOutput(); //关闭需要关闭的对象 printWriter.close(); outputStream.close(); bufferedReader.close(); inputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
服务端代码Server.java
package com.java.tcp; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import com.java.util.PrintUtil; public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8888); PrintUtil.show("---服务端已经启动,等待客户端连接---"); while (true) { Socket socket = serverSocket.accept();// 侦听并接受到此套接字的连接,返回一个Socket对象 ServerThread socketThread = new ServerThread(socket); socketThread.start(); } } catch (IOException e) { e.printStackTrace(); } } }
跑起来的示例
( 写得粗糙,只做mark,后期再完善 )
时间: 2024-10-29 04:57:32