基于Socket的TCP编程
Java语言的基于套接字编程分为服务端编程和客户端编程,其通信模型如图所示:

客户端Socket的工作过程
- 创建Socket对象,指明服务器端的ip和端口号。
- 获取一个输出流,用于输出数据。
- 写出数据的操作。
- 资源的关闭。
服务器端ServerSocket的工作过程
- 创建服务器端的ServerSocket,指明自己的端口号。
- 调用accept()表示接收来自于客户端的socket。
- 获取输入流。
- 读取输入流中的数据。
- 关闭资源。
示例一:客户端发送信息给服务端,服务端将数据显示在控制台上。
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
try {
// 1. 创建Socket对象,指明服务器端的ip和端口号。
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress,8088);
// 2. 获取一个输出流,用于输出数据。
outputStream = socket.getOutputStream();
// 3. 写出数据的操作。
outputStream.write("客户端发送消息".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. 资源的关闭
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1.创建服务器端的ServerSocket,指明自己的端口号
serverSocket = new ServerSocket(8088);
//2.调用accept()表示接收来自于客户端的socket
accept = serverSocket.accept();
//3.获取输入流
inputStream = accept.getInputStream();
//4.读取输入流中的数据
inputStreamReader = new InputStreamReader(inputStream,"utf-8");
// //方式一:因为inputStream是字节型输入流,所以使用ByteArrayOutputStream解决乱码问题
// byteArrayOutputStream = new ByteArrayOutputStream();
// byte[] buffer = new byte[5];
// int len;
// while ((len = inputStream.read(buffer)) != -1){
// byteArrayOutputStream.write(buffer,0,len);
//
// }
// System.out.println(byteArrayOutputStream.toString());
//方式二:使用转换流inputStreamReader解决乱码问题
char[] cbuf = new char[5];
int len;
while ((len = inputStreamReader.read(cbuf)) != -1){
String str = new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭资源
try {
if (byteArrayOutputStream != null)
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStreamReader != null)
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
示例二:客户端发送文件给服务端,服务端将文件保存在本地。
public class TCPTest2 {
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream= null;
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress,8099);
outputStream = socket.getOutputStream();
fileInputStream = new FileInputStream(new File("04.PNG"));
bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[10];
int len;
while ((len = bufferedInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedInputStream != null)
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
serverSocket = new ServerSocket(8099);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
fileOutputStream = new FileOutputStream("08.PNG");
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[10];
int len;
while((len = inputStream.read(buffer)) != -1){
bufferedOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedOutputStream != null)
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
示例三:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
public class TCPTest3 {
@Test
public void client(){
Socket socket = null;
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream= null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress,8099);
outputStream = socket.getOutputStream();
fileInputStream = new FileInputStream(new File("04.PNG"));
bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[10];
int len;
while ((len = bufferedInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
//关闭数据的输出
socket.shutdownOutput();
//接收来自服务器端的反馈
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer1 = new byte[20];
int len1;
while ((len = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (byteArrayOutputStream != null)
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedInputStream != null)
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(8099);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
fileOutputStream = new FileOutputStream("09.PNG");
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[10];
int len;
while((len = inputStream.read(buffer)) != -1){
bufferedOutputStream.write(buffer,0,len);
}
//服务器端接收成功,给予客户端反馈
outputStream = accept.getOutputStream();
outputStream.write("数据已接收!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedOutputStream != null)
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (accept != null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}