1. import org.junit.Test;
    2. import java.io.ByteArrayOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.io.OutputStream;
    6. import java.net.InetAddress;
    7. import java.net.ServerSocket;
    8. import java.net.Socket;
    9. /**
    10. * 实现TCP的网络编程
    11. * 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
    12. */
    13. public class TCPTest {
    14. //客户端
    15. @Test
    16. public void client() {
    17. Socket socket = null;
    18. OutputStream os = null;
    19. try {
    20. //1.创建Socket对象,指明服务器端的ip和端口号
    21. InetAddress inet = InetAddress.getByName("192.168.14.100");
    22. socket = new Socket(inet,8899);
    23. //2.获取一个输出流,用于输出数据
    24. os = socket.getOutputStream();
    25. //3.写出数据的操作
    26. os.write("你好,我是客户端HH".getBytes());
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. } finally {
    30. //4.资源的关闭
    31. if(os != null){
    32. try {
    33. os.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. if(socket != null){
    39. try {
    40. socket.close();
    41. } catch (IOException e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. }
    46. }
    47. //服务端
    48. @Test
    49. public void server() {
    50. ServerSocket ss = null;
    51. Socket socket = null;
    52. InputStream is = null;
    53. ByteArrayOutputStream baos = null;
    54. try {
    55. //1.创建服务器端的ServerSocket,指明自己的端口号
    56. ss = new ServerSocket(8899);
    57. //2.调用accept()表示接收来自于客户端的socket
    58. socket = ss.accept();
    59. //3.获取输入流
    60. is = socket.getInputStream();
    61. //不建议这样写,可能会有乱码
    62. // byte[] buffer = new byte[1024];
    63. // int len;
    64. // while((len = is.read(buffer)) != -1){
    65. // String str = new String(buffer,0,len);
    66. // System.out.print(str);
    67. // }
    68. //4.读取输入流中的数据
    69. baos = new ByteArrayOutputStream();
    70. byte[] buffer = new byte[5];
    71. int len;
    72. while((len = is.read(buffer)) != -1){
    73. baos.write(buffer,0,len);
    74. }
    75. System.out.println(baos.toString());
    76. System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
    77. } catch (IOException e) {
    78. e.printStackTrace();
    79. } finally {
    80. if(baos != null){
    81. //5.关闭资源
    82. try {
    83. baos.close();
    84. } catch (IOException e) {
    85. e.printStackTrace();
    86. }
    87. }
    88. if(is != null){
    89. try {
    90. is.close();
    91. } catch (IOException e) {
    92. e.printStackTrace();
    93. }
    94. }
    95. if(socket != null){
    96. try {
    97. socket.close();
    98. } catch (IOException e) {
    99. e.printStackTrace();
    100. }
    101. }
    102. if(ss != null){
    103. try {
    104. ss.close();
    105. } catch (IOException e) {
    106. e.printStackTrace();
    107. }
    108. }
    109. }
    110. }
    111. }
    1. import org.junit.Test;
    2. import java.io.*;
    3. import java.net.InetAddress;
    4. import java.net.ServerSocket;
    5. import java.net.Socket;
    6. /**
    7. * 实现TCP的网络编程
    8. * 例题2:客户端发送文件给服务端,服务端将文件保存在本地。
    9. *
    10. * @author subei
    11. * @create 2020-05-16 17:05
    12. */
    13. public class TCPTest2 {
    14. /**
    15. * 这里涉及到的异常,应该使用try-catch-finally处理
    16. * @throws IOException
    17. */
    18. @Test
    19. public void test() throws IOException {
    20. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
    21. OutputStream os = socket.getOutputStream();
    22. FileInputStream fis = new FileInputStream(new File("164.jpg"));
    23. byte[] buffer = new byte[1024];
    24. int len;
    25. while((len = fis.read(buffer)) != -1){
    26. os.write(buffer,0,len);
    27. }
    28. fis.close();
    29. os.close();
    30. socket.close();
    31. }
    32. /**
    33. * 这里涉及到的异常,应该使用try-catch-finally处理
    34. * @throws IOException
    35. */
    36. @Test
    37. public void test2() throws IOException {
    38. ServerSocket ss = new ServerSocket(9090);
    39. Socket socket = ss.accept();
    40. InputStream is = socket.getInputStream();
    41. FileOutputStream fos = new FileOutputStream(new File("1641.jpg"));
    42. byte[] buffer = new byte[1024];
    43. int len;
    44. while((len = is.read(buffer)) != -1){
    45. fos.write(buffer,0,len);
    46. }
    47. fos.close();
    48. is.close();
    49. socket.close();
    50. ss.close();
    51. }
    52. }
    1. import org.junit.Test;
    2. import java.io.*;
    3. import java.net.InetAddress;
    4. import java.net.ServerSocket;
    5. import java.net.Socket;
    6. /**
    7. * 实现TCP的网络编程
    8. * 例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。
    9. * 并关闭相应的连接。
    10. *
    11. */
    12. public class TCPTest3 {
    13. /**
    14. * 这里涉及到的异常,应该使用try-catch-finally处理
    15. * @throws IOException
    16. */
    17. @Test
    18. public void test() throws IOException {
    19. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
    20. OutputStream os = socket.getOutputStream();
    21. FileInputStream fis = new FileInputStream(new File("164.jpg"));
    22. byte[] buffer = new byte[1024];
    23. int len;
    24. while((len = fis.read(buffer)) != -1){
    25. os.write(buffer,0,len);
    26. }
    27. //关闭数据的输出
    28. socket.shutdownOutput();
    29. //5.接收来自于服务器端的数据,并显示到控制台上
    30. InputStream is = socket.getInputStream();
    31. ByteArrayOutputStream baos = new ByteArrayOutputStream();
    32. byte[] bufferr = new byte[20];
    33. int len1;
    34. while((len1 = is.read(buffer)) != -1){
    35. baos.write(buffer,0,len1);
    36. }
    37. System.out.println(baos.toString());
    38. fis.close();
    39. os.close();
    40. socket.close();
    41. baos.close();
    42. }
    43. /**
    44. * 这里涉及到的异常,应该使用try-catch-finally处理
    45. * @throws IOException
    46. */
    47. @Test
    48. public void test2() throws IOException {
    49. ServerSocket ss = new ServerSocket(9090);
    50. Socket socket = ss.accept();
    51. InputStream is = socket.getInputStream();
    52. FileOutputStream fos = new FileOutputStream(new File("1642.jpg"));
    53. byte[] buffer = new byte[1024];
    54. int len;
    55. while((len = is.read(buffer)) != -1){
    56. fos.write(buffer,0,len);
    57. }
    58. System.out.println("图片传输完成");
    59. //6.服务器端给予客户端反馈
    60. OutputStream os = socket.getOutputStream();
    61. os.write("你好,照片我已收到,风景不错!".getBytes());
    62. fos.close();
    63. is.close();
    64. socket.close();
    65. ss.close();
    66. os.close();
    67. }
    68. }