代码示例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. @Test
  2. public void client() {
  3. Socket socket = null;
  4. FileInputStream fis = null;
  5. OutputStream os = null;
  6. try {
  7. InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
  8. socket = new Socket(inetAddress, 8899);
  9. fis = new FileInputStream("bg1.jpg");
  10. os = socket.getOutputStream();
  11. byte[] buff = new byte[10];
  12. int len;
  13. while ((len = fis.read(buff)) != -1) {
  14. os.write(buff, 0, len);
  15. }
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. } finally {
  19. try {
  20. if (os != null)
  21. os.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. try {
  26. if (fis != null)
  27. fis.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. try {
  32. if (socket != null)
  33. socket.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. @Test
  40. public void server() {
  41. ServerSocket ss = null;
  42. Socket socket = null;
  43. InputStream is = null;
  44. FileOutputStream fos = null;
  45. try {
  46. ss = new ServerSocket(8899);
  47. socket = ss.accept();
  48. is = socket.getInputStream();
  49. fos = new FileOutputStream("bg4.jpg");
  50. byte[] buff = new byte[10];
  51. int len;
  52. while ((len = is.read(buff)) != -1) {
  53. fos.write(buff, 0, len);
  54. }
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. } finally {
  58. try {
  59. if (fos != null)
  60. fos.close();
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. try {
  65. if (is != null)
  66. is.close();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. }
  70. try {
  71. if (socket != null)
  72. socket.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. try {
  77. ss.close();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }

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

  1. @Test
  2. public void client() {
  3. Socket socket = null;
  4. FileInputStream fis = null;
  5. OutputStream os = null;
  6. ByteArrayOutputStream baos = null;
  7. try {
  8. InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
  9. socket = new Socket(inetAddress, 8899);
  10. fis = new FileInputStream("bg1.jpg");
  11. os = socket.getOutputStream();
  12. byte[] buff = new byte[10];
  13. int len;
  14. while ((len = fis.read(buff)) != -1) {
  15. os.write(buff, 0, len);
  16. }
  17. System.out.println("图片发送完成!");
  18. // 关闭数据的输出
  19. socket.shutdownOutput();
  20. InputStream is = socket.getInputStream();
  21. baos = new ByteArrayOutputStream();
  22. byte[] buffer = new byte[20];
  23. int len1;
  24. while ((len1 = is.read(buffer)) != -1) {
  25. baos.write(buffer, 0, len1);
  26. }
  27. System.out.println(baos.toString());
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } finally {
  31. try {
  32. if (fis != null)
  33. fis.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. try {
  38. if (os != null)
  39. os.close();
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. try {
  44. if (socket != null)
  45. socket.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. try {
  50. if (baos != null)
  51. baos.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. @Test
  58. public void server() {
  59. ServerSocket ss = null;
  60. InputStream is = null;
  61. FileOutputStream fos = null;
  62. try {
  63. ss = new ServerSocket(8899);
  64. Socket socket = ss.accept();
  65. is = socket.getInputStream();
  66. fos = new FileOutputStream("bg7.jpg");
  67. byte[] buff = new byte[10];
  68. int len;
  69. while ((len = is.read(buff)) != -1) {
  70. fos.write(buff, 0, len);
  71. }
  72. System.out.println("图片传输完成!");
  73. OutputStream os = socket.getOutputStream();
  74. os.write("图片已经收到了,很好看".getBytes());
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. } finally {
  78. try {
  79. if (fos != null)
  80. fos.close();
  81. } catch (IOException e) {
  82. e.printStackTrace();
  83. }
  84. try {
  85. if (is != null)
  86. is.close();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. try {
  91. if (ss != null)
  92. ss.close();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }