服务器线程处理类
package demo4; import java.io.*;
import java.net.Socket; /**
* 服务器线程处理类
* @ClassName ServerThread
* @Author DiYun
* @Version 1.0
**/
public class ServerThread extends Thread { //和 本线程相关的Socket
Socket socket = null; public ServerThread(Socket socket) {
this.socket = socket;
} //线程执行的操作,响应客户端的请求
public void run() {
InputStream inputStream =null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
OutputStream outputStream = null;
PrintWriter printWriter = null; try {
//3.获取输入流 并读取客户端信息
inputStream = socket.getInputStream();//获取字节流
inputStreamReader = new InputStreamReader(inputStream); //读取字节流
bufferedReader = new BufferedReader(inputStreamReader);//为输入流添入缓冲
String info = null;
while ((info = bufferedReader.readLine()) != null) {
//循环读取客户端信息
System.out.println("我是服务器,客户端说:" + info);
} //关闭输入流
socket.shutdownInput(); //4.获取输出流,响应客户端的请求
outputStream = socket.getOutputStream(); //获取输出流,响应客户端的请求
printWriter = new PrintWriter(outputStream);
printWriter.write("欢迎您!");
printWriter.flush(); //调用flush方法将缓冲输出 } catch (IOException e) {
e.printStackTrace();
} finally {
try {
//5.关闭资源
if (printWriter !=null)
printWriter.close();
if(outputStream !=null)
outputStream.close();
if (bufferedReader !=null)
bufferedReader.close();
if(inputStreamReader !=null)
inputStreamReader.close();
if(inputStream !=null)
inputStream.close();
if(socket !=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务器端代码
package demo4; import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays; /**
* 基于TCP 协议Socket 服务端
* @ClassName Server
* @Author DiYun
* @Version 1.0
**/
public class Server {
public static void main(String[] args) {
try {
//1.创建一个服务端Socket即ServerSocket 指定服务的端口并监听
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = null;
//记录客户端连接的数量
int count = 0;
System.out.println("服务端已经启动,等待客户端连接************************");
while (true) {
//2.调用accept()方法开始监听,等待客户端的连接
socket = serverSocket.accept();
//创建一个新的线程
ServerThread serverThread = new ServerThread(socket);
serverThread.start();
count++; //统计客户端的数量
System.out.println("客户端的数量" + count);
InetAddress inetAddress = socket.getInetAddress();
System.out.println("当前IP地址是:" + inetAddress.getHostAddress());
System.out.println("客户端信息:" + Arrays.toString(inetAddress.getAddress()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
package demo4; import java.io.*;
import java.net.Socket; /**
* 基于CTP 协议的 Socket 客户端
*
* @ClassName Client
* @Author DiYun
* @Version 1.0
**/
public class Client {
public static void main(String[] args) {
try {
//1.创建客户端 Socket 指定服务器地址和端口
Socket socket = new Socket("localhost", 8888);
//2.获取输出流,向服务端发送信息
OutputStream outputStream = socket.getOutputStream();
//将输出流包装为打印流
PrintWriter pw = new PrintWriter(outputStream);
pw.write("用户名:admin 密码: 12346");
pw.flush();
socket.shutdownOutput();//关闭输出流 //3.获取输出流 并读取服务端的响应信息
InputStream inputStream = socket.getInputStream();
//
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String info = null;
while ((info = bufferedReader.readLine()) != null) {
//循环读取客户端信息
System.out.println("我是客户端,服务端说:" + info);
} //4.关闭资源
bufferedReader.close();
inputStream.close(); pw.close();
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意,必须首先启动服务器再启动客户端