Client

  1. public class Client {
  2. public static void main(String[] args) throws IOException {
  3. //客户端启动必备
  4. Socket socket = null;
  5. //实例化与服务端通信的输入输出流
  6. ObjectOutputStream output = null;
  7. ObjectInputStream input = null;
  8. //服务器的通信地址
  9. InetSocketAddress addr
  10. = new InetSocketAddress("127.0.0.1",10001);
  11. try{
  12. socket = new Socket();
  13. socket.connect(addr);//连接服务器
  14. output = new ObjectOutputStream(socket.getOutputStream());
  15. input = new ObjectInputStream(socket.getInputStream());
  16. /*向服务器输出请求*/
  17. output.writeUTF("Mark");
  18. output.flush();
  19. //接收服务器的输出
  20. System.out.println(input.readUTF());
  21. }finally{
  22. if (socket!=null) socket.close();
  23. if (output!=null) output.close();
  24. if (input!=null) input.close();
  25. }
  26. }
  27. }

Server

  1. public class Server {
  2. public static void main(String[] args) throws IOException {
  3. //服务端启动必备
  4. ServerSocket serverSocket = new ServerSocket();
  5. //表示服务端在哪个端口上监听
  6. serverSocket.bind(new InetSocketAddress(10001));
  7. System.out.println("Start Server ....");
  8. try{
  9. while(true){
  10. new Thread(new ServerTask(serverSocket.accept())).start();
  11. }
  12. }finally {
  13. serverSocket.close();
  14. }
  15. }
  16. //每个和客户端的通信都会打包成一个任务,交个一个线程来执行
  17. private static class ServerTask implements Runnable{
  18. private Socket socket = null;
  19. public ServerTask(Socket socket){
  20. this.socket = socket;
  21. }
  22. @Override
  23. public void run() {
  24. //实例化与客户端通信的输入输出流
  25. try(ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
  26. ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream())){
  27. //接收客户端的输出,也就是服务器的输入
  28. String userName = inputStream.readUTF();
  29. System.out.println("Accept client message:"+userName);
  30. //服务器的输出,也就是客户端的输入
  31. outputStream.writeUTF("Hello,"+userName);
  32. outputStream.flush();
  33. }catch(Exception e){
  34. e.printStackTrace();
  35. }finally {
  36. try {
  37. socket.close();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. }
  44. }

使用线程池的server

  1. public class ServerPool {
  2. private static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  3. public static void main(String[] args) throws IOException {
  4. //服务端启动必备
  5. ServerSocket serverSocket = new ServerSocket();
  6. //表示服务端在哪个端口上监听
  7. serverSocket.bind(new InetSocketAddress(10001));
  8. System.out.println("Start Server ....");
  9. try{
  10. while(true){
  11. executorService.execute(new ServerTask(serverSocket.accept()));
  12. }
  13. }finally {
  14. serverSocket.close();
  15. }
  16. }
  17. //每个和客户端的通信都会打包成一个任务,交个一个线程来执行
  18. private static class ServerTask implements Runnable{
  19. private Socket socket = null;
  20. public ServerTask(Socket socket){
  21. this.socket = socket;
  22. }
  23. @Override
  24. public void run() {
  25. //实例化与客户端通信的输入输出流
  26. try(ObjectInputStream inputStream =
  27. new ObjectInputStream(socket.getInputStream());
  28. ObjectOutputStream outputStream =
  29. new ObjectOutputStream(socket.getOutputStream())){
  30. //接收客户端的输出,也就是服务器的输入
  31. String userName = inputStream.readUTF();
  32. System.out.println("Accept client message:"+userName);
  33. //服务器的输出,也就是客户端的输入
  34. outputStream.writeUTF("Hello,"+userName);
  35. outputStream.flush();
  36. }catch(Exception e){
  37. e.printStackTrace();
  38. }finally {
  39. try {
  40. socket.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. }
  47. }