一、服务端
package com.czhappy.hello.socket; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { //创建ServerSocket示例,指定端口,侦听 try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务即将启动,等待客户端连接"); //记录客户端的数量 int count = 0; Socket socket = null; while(true){ //开始监听,等待客户端的连接 socket = serverSocket.accept(); ServerThread serverThread = new ServerThread(socket); serverThread.start(); count++; System.out.println("客户端的数量="+count); InetAddress address = socket.getInetAddress(); System.out.println("当前客户端的IP地址为:"+address.getHostAddress()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.czhappy.hello.socket; 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.io.UnsupportedEncodingException; import java.net.Socket; /** * 服务器线程处理类 * @author Administrator * */ public class ServerThread extends Thread{ Socket socket = null; public ServerThread(Socket socket){ this.socket = socket; } public void run() { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStream os = null; PrintWriter pw = null; try { //获取字节输入流 is = socket.getInputStream(); //将字节输入流转化为字符输入流 isr = new InputStreamReader(is, "utf-8"); //为字符输入流添加缓冲 br = new BufferedReader(isr); String data = br.readLine(); while(data!=null){ System.out.println("我是服务器,客户端说:"+data); data = br.readLine(); } //关闭输入流 socket.shutdownInput(); //获取输出流,响应客户端请求 os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("欢迎您!"); pw.flush(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(pw!=null){ pw.close(); } if(os!=null){ os.close(); } if(br!=null){ br.close(); } if(isr!=null){ isr.close(); } if(is!=null){ is.close(); } if(socket!=null){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
二、客户端
package com.czhappy.hello.socket; 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 { public static void main(String[] args) { try { //创建客户端Socket,指定服务器地址和端口 Socket socket = new Socket("192.168.0.45", 8888); //获取输出流,向服务器发送消息 OutputStream os = socket.getOutputStream();//字节输出流 PrintWriter pw = new PrintWriter(os);//将输出流包装成打印流 pw.write("用户名:admin;密码:123"); pw.flush(); socket.shutdownOutput();//关闭输出流 //获取输入流,读取服务器端响应信息 InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String data = br.readLine(); while(data!=null){ System.out.println("我是客户端,服务器说:"+data); data = br.readLine(); } br.close(); is.close(); pw.close(); os.close(); socket.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
三、测试结果
需要先启动服务端,再启动客户端
服务端运行结果:
客户端运行结果:
原文地址:https://www.cnblogs.com/chenzheng8975/p/9579901.html
时间: 2024-10-06 17:23:19