1.多线程方式循环处理多个socket请求-server

server

  1. package com.sunyj.practice.t0001_socket.multiThread;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. import java.util.concurrent.ExecutorService;
  8. import java.util.concurrent.Executors;
  9. /**
  10. * 多线程方式循环处理多个socket请求-server
  11. * @author syj
  12. * @date 2019年5月15日 下午9:52:16
  13. */
  14. public class Server {
  15. public static void main(String[] args) {
  16. int port = 9212;
  17. try {
  18. @SuppressWarnings("resource")
  19. ServerSocket serverSocket = new ServerSocket(port);
  20. // serverSocket.setSoTimeout();
  21. System.out.println("server一直等待client连接的到来...");
  22. ExecutorService executorService = Executors.newFixedThreadPool(100);
  23. while(true) {
  24. Socket socket = serverSocket.accept();
  25. Runnable runnable = () -> {
  26. readMsg(socket);
  27. };
  28. executorService.submit(runnable);
  29. }
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. /**
  35. * 读取socket传来的信息
  36. * @param socket
  37. * @throws IOException
  38. * @throws UnsupportedEncodingException
  39. */
  40. private static void readMsg(Socket socket){
  41. try {
  42. InputStream inputStream = socket.getInputStream();
  43. byte[] bytes = new byte[1024];
  44. int len;
  45. StringBuilder sb = new StringBuilder();
  46. while((len = inputStream.read(bytes)) != -1) {
  47. sb.append(new String(bytes,0,len,"utf-8"));
  48. }
  49. System.out.println(sb.toString());
  50. inputStream.close();
  51. socket.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }

2.可重复发送信息SOCKET - client

client

  1. package com.sunyj.practice.t0001_socket.repeatSend;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.net.InetSocketAddress;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. /**
  8. * 可重复发送信息SOCKET - client
  9. * @author syj
  10. * @date 2019年5月15日 下午9:17:45
  11. */
  12. public class Client {
  13. public static void main(String[] args) throws UnknownHostException,IOException {
  14. String host = "127.0.0.1";
  15. int port = 9212;
  16. try {
  17. Socket socket = new Socket();
  18. socket.connect(new InetSocketAddress(host,port), 10000);
  19. OutputStream outputStream = socket.getOutputStream();
  20. String msg = "你好,12312sfsaf";
  21. byte[] bytes = msg.getBytes("utf-8");
  22. outputStream.write(bytes.length >> 8);
  23. System.out.println(bytes.length);
  24. System.out.println(bytes.length >> 8);
  25. outputStream.write(bytes.length);
  26. outputStream.write(msg.getBytes("utf-8"));
  27. outputStream.flush();
  28. msg = "second send message...";
  29. outputStream.write(bytes.length >> 8);
  30. outputStream.write(bytes.length);
  31. outputStream.write(msg.getBytes("utf-8"));
  32. outputStream.flush();
  33. msg = "第三次发送...";
  34. outputStream.write(bytes.length >> 8);
  35. outputStream.write(bytes.length);
  36. outputStream.write(msg.getBytes("utf-8"));
  37. outputStream.flush();
  38. outputStream.close();
  39. socket.close();
  40. } catch (UnknownHostException e) {
  41. e.printStackTrace();
  42. throw e;
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. throw e;
  46. }
  47. }
  48. }

server

  1. package com.sunyj.practice.t0001_socket.repeatSend;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. /**
  7. * 可重复发送的SOCKET
  8. * @author syj
  9. * @date 2019年5月15日 下午9:06:52
  10. */
  11. public class Server {
  12. public static void main(String[] args) throws IOException {
  13. int port = 9212;
  14. try {
  15. ServerSocket serverSocket = new ServerSocket(port);
  16. System.out.println("-----server一直等待客户端连接的到来...");
  17. serverSocket.setSoTimeout(10000);
  18. Socket socket = serverSocket.accept();
  19. InputStream inputStream = socket.getInputStream();
  20. byte[] bytes;
  21. while(true) {
  22. int first = inputStream.read(); //读取第一个字节
  23. if(first == -1) {
  24. break;
  25. }
  26. int second = inputStream.read();
  27. System.out.println(first << 8);
  28. int len = (first << 8) + second;
  29. System.out.println(len);
  30. bytes = new byte[len];
  31. inputStream.read(bytes, 0, len);
  32. System.out.println("------get message from client:"+new String(bytes,"utf-8"));
  33. }
  34. inputStream.close();
  35. socket.close();
  36. serverSocket.close();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. throw e;
  40. }
  41. }
  42. }

3.simple

client

  1. package com.sunyj.practice.t0001_socket.simple;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.net.UnknownHostException;
  7. /**
  8. *
  9. * @author syj
  10. * @date 2019年5月15日 下午8:24:18
  11. */
  12. public class Client {
  13. public static void main(String[] args) {
  14. String host = "127.0.0.1";
  15. int port = 9212;
  16. try {
  17. Socket socket = new Socket(host,port);
  18. OutputStream outputStream = socket.getOutputStream();
  19. outputStream.write("你好".getBytes("utf-8"));
  20. //通过shutdownoutput来告诉服务器已经发送完数据,后续只能接受数据
  21. socket.shutdownOutput();
  22. InputStream inputStream = socket.getInputStream();
  23. byte[] bytes = new byte[1024];
  24. int len;
  25. StringBuilder sb = new StringBuilder();
  26. while((len = inputStream.read(bytes)) != -1) {
  27. sb.append(new String(bytes,0,len,"UTF-8"));
  28. }
  29. System.out.println("get message from server:"+sb.toString());
  30. inputStream.close();
  31. outputStream.close();
  32. socket.close();
  33. } catch (UnknownHostException e) {
  34. e.printStackTrace();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }

server

  1. package com.sunyj.practice.t0001_socket.simple;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. /**
  7. * socket服务端
  8. * @author syj
  9. * @date 2019年5月15日 下午8:00:49
  10. */
  11. public class Server {
  12. public static void main(String[] args) {
  13. int port = 9212;
  14. try {
  15. ServerSocket serverSocket = new ServerSocket(port);
  16. System.out.println("server一直等待客户端连接的到来。。。");
  17. Socket socket = serverSocket.accept();
  18. InputStream inputStream = socket.getInputStream(); //读取到内存中
  19. byte[] bytes = new byte[1024];
  20. StringBuilder sb = new StringBuilder();
  21. int len;
  22. while((len = inputStream.read(bytes)) != -1) {
  23. sb.append(new String(bytes,0,len,"UTF-8"));
  24. }
  25. System.out.println("get message from client:"+sb.toString());
  26. OutputStream outputStream = socket.getOutputStream();
  27. outputStream.write("hello client,I get the message.".getBytes("UTF-8"));
  28. outputStream.close();
  29. inputStream.close();
  30. socket.close();
  31. serverSocket.close();
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }