package zhang; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; /** * 服务器端的线程处理 * */ public class ServerThread extends Thread { Socket socket=null; //和本线程相关的socket /** * 构造方法 * */ public ServerThread(Socket socket){ this.socket=socket; } /** * 执行线程操作响应客户端的请求 * */ public void run(){ InputStream is=null; InputStreamReader isr=null; BufferedReader br=null; OutputStream os=null; OutputStreamWriter osw=null; BufferedWriter bw=null; //获取输入流,并读取客户端消息 try { is=socket.getInputStream(); isr=new InputStreamReader(is); br=new BufferedReader(isr); String content; while((content=br.readLine())!=null){ System.out.println(content); } socket.shutdownInput(); os=socket.getOutputStream(); osw=new OutputStreamWriter(os); bw=new BufferedWriter(osw); bw.write("欢迎您!"); bw.flush(); socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); } //关闭资源 finally{ try { if(br!=null)br.close(); if(isr!=null) isr.close(); if(is!=null) is.close(); if(os!=null) os.close(); if(osw!=null) osw.close(); if(bw!=null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package zhang; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import org.apache.log4j.Logger; public class Server { static Logger logger=Logger.getLogger("Server"); public static void main(String[] args) { ServerSocket server=null; try { server=new ServerSocket(8887); System.out.println(server.getInetAddress()); int count=0; //客户端连接的个数 System.out.println("服务器已启动,等待客户端的连接..."); Socket socket=null; while(true){ socket=server.accept(); ServerThread serverthread=new ServerThread(socket); serverthread.start(); count++; System.out.println("客户端的数量为:"+count); InetAddress address=socket.getInetAddress(); System.out.println(address); } } catch (IOException e) { e.printStackTrace(); logger.debug("异常:"+e.getMessage()); } finally{ if(server!=null){ try { server.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
package zhang; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) { try { //创建连接服务器的套接字 Socket socket=new Socket("127.0.0.1",8887); //向服务器发送信息 OutputStream os=socket.getOutputStream(); OutputStreamWriter osw=new OutputStreamWriter(os); BufferedWriter bw=new BufferedWriter(osw); bw.write("服务器你好,我是客户端..."); bw.flush(); //关闭输出流 socket.shutdownOutput(); InputStream is= socket.getInputStream(); InputStreamReader isr=new InputStreamReader(is); BufferedReader br=new BufferedReader(isr); String content; while((content=br.readLine())!=null){ System.out.println("来自服务器的响应:"+content); } //关闭资源 bw.close(); osw.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
先启动服务端:
服务端输出:
0.0.0.0/0.0.0.0
服务器已启动,等待客户端的连接...
再启动客户端:
客户端输出:
来自服务器的响应:欢迎您!
服务端输出:
0.0.0.0/0.0.0.0
服务器已启动,等待客户端的连接...
客户端的数量为:1
/127.0.0.1
服务器你好,我是客户端...