1. 流程
- 构建TCP客户端、服务端
- 客户端发送数据,并接收服务器传回的数据长度
- 服务器接收并打印数据
2. 客户端代码
运行环境:IDEA
import java.io.*;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
public static void main(String []args) throws IOException {
Socket socket = new Socket();
//超时时间
socket.setSoTimeout(3000);
//连接本地,超时时间3000
socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),3000);
System.out.println("已连接服务器");
System.out.println("服务器信息:"+socket.getLocalAddress()+" port:"+socket.getLocalPort());
System.out.println("客户端信息:"+socket.getInetAddress()+" port:"+socket.getPort());
try{
todo(socket);
}catch(Exception e){
System.out.println("异常关闭");
}
//释放资源
socket.close();
System.out.println("客户端已退出 ");
}
private static void todo(Socket client)throws IOException{
//构建键盘输入流
InputStream in=System.in;
BufferedReader input = new BufferedReader(new InputStreamReader(in));
//得到输出流,转化为打印流
OutputStream out = client.getOutputStream();
PrintStream ptStream = new PrintStream(out);
//得到socket输入流,转化为Bufferedreader
InputStream inputStream = client.getInputStream();
BufferedReader socketBufferedreader = new BufferedReader(new InputStreamReader(inputStream));
boolean flag=true;
do{
//键盘读取一行
String s = input.readLine();
//发送到服务器
ptStream.println(s);
//从服务器读取一行
String echo = socketBufferedreader.readLine();
if("bye".equalsIgnoreCase(echo)){
flag = false;
}
else{
System.out.println(echo);
}
}while(flag);
//资源释放
ptStream.close();
socketBufferedreader.close();
}
}
3. 服务器端代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String []args) throws IOException {
ServerSocket serverSocket = new ServerSocket(2000);
System.out.println("服务器准备就绪");
System.out.println("服务器信息:"+serverSocket.getInetAddress()+" port:"+serverSocket.getLocalPort());
//等待客户端连接
for(;;){
//得到客户端
Socket client = serverSocket.accept();
//客户端构建异步线程
ClientHander clientHander = new ClientHander(client);
//启动线程
clientHander.start();
}
}
private static class ClientHander extends Thread{
private Socket socket;
private boolean flag = true;
ClientHander(Socket socket){
this.socket = socket;
}
@Override
public void run() {
super.run();
System.out.println("新客户端连接:"+this.socket.getInetAddress()+" port:"+this.socket.getLocalPort());
try{
//得到输出流,用于数据输出
PrintStream pstream = new PrintStream(this.socket.getOutputStream());
//得到输入流,用于接收数据
BufferedReader socketinput = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
do{
String s = socketinput.readLine();
if("bye".equalsIgnoreCase(s)){
flag = false;
pstream.println("bye");
}
else{
System.out.println(s);
pstream.println("回送数据长度:"+s.length());
}
}while(flag);
pstream.close();
socketinput.close();
}catch(Exception e){
System.out.println("连接异常断开");
}finally {
//关闭连接
try{
this.socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
System.out.println("客户端已退出"+this.socket.getInetAddress()+" port:"+this.socket.getLocalPort());
}
}
}
4. 运行结果
![]() | ![]() |
![]() | ![]() |
![]() | ![]() |
可见客户端退出后服务器并未停止,当有新客户端连接时,服务器仍正常连接。
每次客户端连接的服务器端口并不同,但其实都是本地电脑。