代码示例1:客户端发送信息给服务端,服务端将数据显示在控制台上

    1. //客户端
    2. @Test
    3. public void client() {
    4. Socket socket = null;
    5. OutputStream os = null;
    6. try {
    7. //1.创建Socket对象,指明服务器端的ip和端口号
    8. InetAddress inet = InetAddress.getByName("192.168.14.100");
    9. socket = new Socket(inet,8899);
    10. //2.获取一个输出流,用于输出数据
    11. os = socket.getOutputStream();
    12. //3.写出数据的操作
    13. os.write("你好,我是客户端mm".getBytes());
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. } finally {
    17. //4.资源的关闭
    18. if(os != null){
    19. try {
    20. os.close();
    21. } catch (IOException e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. if(socket != null){
    26. try {
    27. socket.close();
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. }
    31. }
    32. }
    33. }
    34. //服务端
    35. @Test
    36. public void server() {
    37. ServerSocket ss = null;
    38. Socket socket = null;
    39. InputStream is = null;
    40. ByteArrayOutputStream baos = null;
    41. try {
    42. //1.创建服务器端的ServerSocket,指明自己的端口号
    43. ss = new ServerSocket(8899);
    44. //2.调用accept()表示接收来自于客户端的socket
    45. socket = ss.accept();
    46. //3.获取输入流
    47. is = socket.getInputStream();
    48. //不建议这样写,可能会乱码
    49. // byte[] buffer = new byte[1024];
    50. // int len;
    51. // while((len = is.read(buffer)) != -1){
    52. // String str = new String(buffer,0,len);
    53. // System.out.print(str);
    54. // }
    55. //4.读取输入流中的数据
    56. baos = new ByteArrayOutputStream();
    57. byte[] buffer = new byte[5];
    58. int len;
    59. while((len = is.read(buffer)) != -1){
    60. baos.write(buffer,0,len);
    61. }
    62. System.out.println(baos.toString());
    63. System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
    64. } catch (IOException e) {
    65. e.printStackTrace();
    66. } finally {
    67. if(baos != null){
    68. //5.关闭资源
    69. try {
    70. baos.close();
    71. } catch (IOException e) {
    72. e.printStackTrace();
    73. }
    74. }
    75. if(is != null){
    76. try {
    77. is.close();
    78. } catch (IOException e) {
    79. e.printStackTrace();
    80. }
    81. }
    82. if(socket != null){
    83. try {
    84. socket.close();
    85. } catch (IOException e) {
    86. e.printStackTrace();
    87. }
    88. }
    89. if(ss != null){
    90. try {
    91. ss.close();
    92. } catch (IOException e) {
    93. e.printStackTrace();
    94. }
    95. }
    96. }
    97. }

    代码示例2:客户端发送文件给服务端,服务端将文件保存在本地。

    1. /*
    2. 这里涉及到的异常,应该使用try-catch-finally处理
    3. */
    4. @Test
    5. public void client() throws IOException {
    6. //1.
    7. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
    8. //2.
    9. OutputStream os = socket.getOutputStream();
    10. //3.
    11. FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
    12. //4.
    13. byte[] buffer = new byte[1024];
    14. int len;
    15. while((len = fis.read(buffer)) != -1){
    16. os.write(buffer,0,len);
    17. }
    18. //5.
    19. fis.close();
    20. os.close();
    21. socket.close();
    22. }
    23. /*
    24. 这里涉及到的异常,应该使用try-catch-finally处理
    25. */
    26. @Test
    27. public void server() throws IOException {
    28. //1.
    29. ServerSocket ss = new ServerSocket(9090);
    30. //2.
    31. Socket socket = ss.accept();
    32. //3.
    33. InputStream is = socket.getInputStream();
    34. //4.
    35. FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg"));
    36. //5.
    37. byte[] buffer = new byte[1024];
    38. int len;
    39. while((len = is.read(buffer)) != -1){
    40. fos.write(buffer,0,len);
    41. }
    42. //6.
    43. fos.close();
    44. is.close();
    45. socket.close();
    46. ss.close();
    47. }

    代码示例3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。

    1. /*
    2. 这里涉及到的异常,应该使用try-catch-finally处理
    3. */
    4. @Test
    5. public void client() throws IOException {
    6. //1.
    7. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
    8. //2.
    9. OutputStream os = socket.getOutputStream();
    10. //3.
    11. FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
    12. //4.
    13. byte[] buffer = new byte[1024];
    14. int len;
    15. while((len = fis.read(buffer)) != -1){
    16. os.write(buffer,0,len);
    17. }
    18. //关闭数据的输出
    19. socket.shutdownOutput();
    20. //5.接收来自于服务器端的数据,并显示到控制台上
    21. InputStream is = socket.getInputStream();
    22. ByteArrayOutputStream baos = new ByteArrayOutputStream();
    23. byte[] bufferr = new byte[20];
    24. int len1;
    25. while((len1 = is.read(buffer)) != -1){
    26. baos.write(buffer,0,len1);
    27. }
    28. System.out.println(baos.toString());
    29. //6.
    30. fis.close();
    31. os.close();
    32. socket.close();
    33. baos.close();
    34. }
    35. /*
    36. 这里涉及到的异常,应该使用try-catch-finally处理
    37. */
    38. @Test
    39. public void server() throws IOException {
    40. //1.
    41. ServerSocket ss = new ServerSocket(9090);
    42. //2.
    43. Socket socket = ss.accept();
    44. //3.
    45. InputStream is = socket.getInputStream();
    46. //4.
    47. FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg"));
    48. //5.
    49. byte[] buffer = new byte[1024];
    50. int len;
    51. while((len = is.read(buffer)) != -1){
    52. fos.write(buffer,0,len);
    53. }
    54. System.out.println("图片传输完成");
    55. //6.服务器端给予客户端反馈
    56. OutputStream os = socket.getOutputStream();
    57. os.write("你好,美女,照片我已收到,非常漂亮!".getBytes());
    58. //7.
    59. fos.close();
    60. is.close();
    61. socket.close();
    62. ss.close();
    63. os.close();
    64. }