基于TCP的socket编程(有序,有连接,可靠)
套接字使用TCP提供了两台计算机之间的通信机制。
主要类有
- Socket:客户端
- ServerSocket:服务端
ServerSocket类
java.net.ServerSocket 类以获取一个端口,并且侦听客户端请求。
构造方法
- ServerSocket()//创建非绑定服务器套接字
- ServerSocket(int port) //创建绑定到特定端口的服务器套接字
- ServerSocket(int port, int backlog) //指定的 backlog 创建服务器套接字并将其绑定到指定的本地端口号
- ServerSocket(int port, int backlog, InetAddress address) //使用指定的端口、侦听 backlog 和要绑定到的本地 IP 地址创建服务器
常用方法
- Socket accept()//侦听并接受到此套接字的连接
- void bind(SocketAddress endpoint) //将 ServerSocket 绑定到特定地址
- void bind(SocketAddress host, int backlog) //将 ServerSocket 绑定到特定地址
- int getLocalPort() //返回此套接字在其上侦听的端口
- boolean isBound() //返回 ServerSocket 的绑定状态
- boolean isClosed() //返回 ServerSocket 的关闭状态
- static void setSocketFactory(SocketImplFactory fac) //设置服务器套接字实现工厂
- void setSoTimeout(int timeout) //通过指定超时值启用/禁用 SO_TIMEOUT
Socket类
java.net.Socket 类代表客户端和服务器都用来互相沟通的套接字,套接字是两台机器间通信的端点。
关闭时,io流同时关闭。
构造方法
- Socket() //系统默认 SocketImpl 创建未连接套接字
- Socket(InetAddress host, int port)//创建套接字并连接到指定 IP 地址的指定端口号
- Socket(String host, int port) //连接到指定主机上的指定端口号
- Socket(Proxy proxy) //创建一个未连接的套接字并指定代理类型
- Socket(String host, int port, InetAddress localAddr, int localPort) //连接到指定远程主机上的指定远程端口
- Socket(InetAddress host, int port, InetAddress localAddr, int localPort) //连接到指定远程主机上的指定远程端口
常用方法
- void bind(SocketAddress bindpoint) //绑定到本地地址
- void connect(SocketAddress endpoint) //连接到服务器
- void connect(SocketAddress endpoint, int timeout) //连接到服务器,并指定一个超时值
- SocketChannel getChannel() //返回与此数据报关联的唯一 SocketChannel 对象
- InetAddress getInetAddress() //返回套接字连接的地址
- InputStream getInputStream() //返回此套接字的输入流
- OutputStream getOutputStream() //返回此套接字的输出流
- SocketAddress getRemoteSocketAddress() //返回此套接字连接的端点的地址
实例应用——单线程
//服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class Server {
public static void main(String[] args) {
try {
//1.创建server实例并绑定端口
ServerSocket serverSocket = new ServerSocket(6666);
System.out.println("***服务端开始启动,等待客户端的连接***");
//2.调用accept(),监听客户端请求
Socket socket = serverSocket.accept();
//3.读取客户端请求信息
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String info = null;
while ((info = bufferedReader.readLine()) != null) {
System.out.println("客户端说:" + info);
}
socket.shutdownInput();
//4.输出服务端响应信息
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.write("欢迎使用!");
printWriter.flush();
//5.关闭相关资源
printWriter.close();
outputStream.close();
bufferedReader.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//客户端
import java.io.*;
import java.net.Socket;
//客户端
public class Client {
public static void main(String[] args) {
try {
//1.创建socket实例,并连接服务端
Socket socket = new Socket("localhost", 6666);
//2.建立连接后,发出请求
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.write("用户名:max ; 密码:123");
printWriter.flush();
socket.shutdownOutput();
//3.获取服务端响应信息
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String info = null;
while ((info = bufferedReader.readLine()) != null) {
System.out.println("服务端说:" + info);
}
//4.关闭相关资源
bufferedReader.close();
printWriter.close();
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
实例应用——多线程
//线程类
import java.io.*;
import java.net.Socket;
public class ServerThread extends Thread {
Socket socket = null;
public ServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader bufferedReader = null;
OutputStream outputStream = null;
PrintWriter printWriter = null;
try {
//3.读取客户端请求信息
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
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();
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭相关资源
try {
if (printWriter != null)
printWriter.close();
if (outputStream != null)
outputStream.close();
if (bufferedReader != null)
bufferedReader.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
//1.创建server实例并绑定端口
ServerSocket serverSocket = new ServerSocket(6666);
//2.调用accept(),监听客户端请求
int count = 0;//客户端数量
System.out.println("***服务端开始启动,等待客户端的连接***");
//循环监听
while (true) {
Socket socket = serverSocket.accept();
ServerThread serverThread = new ServerThread(socket);
serverThread.start();
count++;
System.out.println("输出客户端连接的数量:"+count);
InetAddress address = socket.getInetAddress();
System.out.println("当前客户端ip:"+address);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}