Server:
import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { /** * Socket服务端 */ public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务端已启动,等待客户端连接.."); while (true) { Socket socket = serverSocket.accept();// 侦听并接受到此套接字的连接,返回一个Socket对象 SocketThread socketThread = new SocketThread(socket); socketThread.start(); } } catch (IOException e) { e.printStackTrace(); } } }
Client:
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; public class Client { /** * Socket客户端 */ public static void main(String[] args) { try { //创建Socket对象 Socket socket=new Socket("localhost",8888); //根据输入输出流和服务端连接 OutputStream outputStream=socket.getOutputStream();//获取一个输出流,向服务端发送信息 PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流 printWriter.print("服务端你好,我是客户1"); printWriter.flush(); socket.shutdownOutput();//关闭输出流 InputStream inputStream=socket.getInputStream();//获取一个输入流,接收服务端的信息 InputStreamReader inputStreamReader=new InputStreamReader(inputStream);//包装成字符流,提高效率 BufferedReader bufferedReader=new BufferedReader(inputStreamReader);//缓冲区 String info=""; String temp=null;//临时变量 while((temp=bufferedReader.readLine())!=null){ info+=temp; System.out.println("客户端接收服务端发送信息:"+info); } //关闭相对应的资源 bufferedReader.close(); inputStream.close(); printWriter.close(); outputStream.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }