记录学习(1)网络编程:TCP(socket,IO流)
总结:
服务器端第1步:1.创造一个地址和端口号
ServerSocket serverSocket = new ServerSocket(9999);
客户端第2步:2.创建一个socket连接
socket = new Socket(serverIP, port);
客户端第3步:3.1发送消息IO流
OutputStream os = socket.getOutputStream();
os.write(“用户端发送IO流”.getBytes());
服务器端第2步:2.等待客户端连接进来
服务器端第3步:3.读取客户端消息
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
服务器端第4步4.管道流
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println("最后一步结果内容:"+baos.toString());
1.客户端类
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientSocket {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
// 1.要知道服务器的地址、端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
System.out.println("客户端第1步:1.要知道服务器的地址、端口号");
// 2.创建一个socket连接
socket = new Socket(serverIP, port);
System.out.println("客户端第2步:2.创建一个socket连接");
// 3.发送消息IO流
os = socket.getOutputStream();
System.out.println("客户端第3步:3.1发送消息IO流");
// getBytes()使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
os.write("用户端发送IO流".getBytes());
System.out.println("客户端第3步:3.2写入发送的消息");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
System.out.println("用户端os关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
System.out.println("用户端socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2.服务器端类:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerSocket {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket =null;
InputStream is =null;
ByteArrayOutputStream baos =null;
try {
//1.创造一个地址和端口号
serverSocket = new ServerSocket(9999);
System.out.println("服务器端第1步:1.创造一个地址和端口号");
//2.等待客户端连接进来
socket = serverSocket.accept();
System.out.println("服务器端第2步:2.等待客户端连接进来");
//3.读取客户端消息
is = socket.getInputStream();
System.out.println("服务器端第3步:3.读取客户端消息");
//4.管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){//read从该输入流读取下一个数据字节。
//write从指定的字节数组写入 len字节,从偏移量为 off开始,输出到这个字节数组输出流。
baos.write(buffer,0,len);
}
System.out.println("最后一步结果内容:"+baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {//捕获异常并关闭
if (baos != null) {
try {
baos.close();
System.out.println("服务器端baos关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
if (is !=null) {
try {
is.close();
System.out.println("服务器端is关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
System.out.println("服务器端socket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
System.out.println("服务器端serverSocket关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.运行结果:
服务器端第1步:1.创造一个地址和端口号
服务器端第2步:2.等待客户端连接进来
服务器端第3步:3.读取客户端消息
最后一步结果内容:用户端发送IO流
服务器端baos关闭
服务器端is关闭
服务器端socket关闭
服务器端serverSocket关闭