1、 TCP协议 :传输控制协议
- 使用tcp协议前,需先建立TCP连接,形成传输数据通道,
- 传输前,采用三次握手方式,是可靠的
- TCP协议进行通信的两个应用进程:客户端、服务端
- 在连接中,课进行大量数据的传输
- 传输完毕,需释放已建立的连接,效率低
一下为一个简单的聊天程序:
1.1 字节流传输
服务端
public class TcpServerDemo {
public static void main(String[] args) throws IOException, IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务器启动!等待连接...");
Socket clientSocket = serverSocket.accept();
System.out.println("服务端连接:" + clientSocket.getClass().getName());
InputStream inputStream = clientSocket.getInputStream();
int i;
StringBuilder stringBuilder = new StringBuilder();
System.out.print("客户端发来的消息:");
while ((i = inputStream.read()) != -1) {
stringBuilder.append((char) i);
}
System.out.print(stringBuilder);
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write((stringBuilder + "(服务器消息)").getBytes());
clientSocket.shutdownOutput();
outputStream.close();
serverSocket.close();
inputStream.close();
}
}
客户端
public class TcpClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端连接:" + socket.getClass().getName());
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello".getBytes());
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int redLin = 0;
while ((redLin = inputStream.read(bytes)) != -1) {
System.out.println("服务器发来的消息:" + new String(bytes, 0, redLin));
}
outputStream.close();
socket.close();
System.out.println("客户端退出");
}
}
1. 2 字符流传输
如果客户端使用字符流传输,那么服务端要使用字符流读取 服务端代码:
public class TcpServerDemo {
public static void main(String[] args) throws IOException, IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("服务器启动!等待连接...");
Socket clientSocket = serverSocket.accept();
System.out.println("服务端连接:" + clientSocket.getClass().getName());
InputStream inputStream = clientSocket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String str = bufferedReader.readLine();
System.out.println(str);
OutputStream outputStream = clientSocket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write((str + "字符流,(服务器消息)"));
bufferedWriter.newLine();
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
serverSocket.close();
inputStream.close();
}
}
客户端
public class TcpClientDemo {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端连接:" + socket.getClass().getName());
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello");
bufferedWriter.newLine();
bufferedWriter.flush();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println(bufferedReader.readLine());
bufferedReader.close();
outputStream.close();
socket.close();
System.out.println("客户端退出");
}
}
2、UDP
- 将数据封装成数据包,不建立连接
- 每个数据包的大小为64kb,不适合大量数据
- 因无需连接,所以是不可靠的
- 发送数据结束时不需要释放资源(因为不是面向连接的),速度快
- 例如:发短信
|