-------------------------------------------------------------  Dart    SocketClient--------------------------------------------------------------------------------

import 'dart:io';
import 'dart:async';
import 'dart:convert' show utf8;

Socket socket;

void main() {
  Socket.connect("10.10.10.180", 6666).then((Socket sock) {
    socket = sock;
    sock.write("这是客户端消息!");
      socket.listen(dataHandler,
          onError: errorHandler,
          onDone: doneHandler,
          cancelOnError: false);
    socket.close();
  }).catchError((AsyncError e) {
    print("无法连接: $e");
  });
}

//接收报文
void dataHandler(data){
  print(utf8.decode(data));
}

void errorHandler(error, StackTrace trace){
  print(error);
}

void doneHandler(){
  socket.destroy();
}

-------------------------------------------------Java SocketServer------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    /**
     * Socket服务端
     */
    public static void main(String[] args) {

        try {
            ServerSocket serverSocket=new ServerSocket(6666);
            System.out.println("服务端已启动,等待客户端连接..");
            while (true){
                Socket socket=serverSocket.accept();//侦听并接受到此套接字的连接,返回一个Socket对象
                new Habdler(socket).start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class Habdler extends Thread{
        Socket socket = null;
        public Habdler(Socket socket){
            this.socket = socket;
        }

        @Override
        public void run() {
            //根据输入输出流和客户端连接
            InputStream inputStream= null;//得到一个输入流,接收客户端传递的信息
            BufferedReader bufferedReader= null;//加入缓冲区
            OutputStream outputStream= null;//获取一个输出流,向服务端发送信息
            PrintWriter printWriter= null;//将输出流包装成打印流
            try {
                inputStream = socket.getInputStream();
                InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
                bufferedReader = new BufferedReader(inputStreamReader);
                String temp=null;
                String info="";
                while((temp=bufferedReader.readLine())!=null){
                    info+=temp;
                    System.out.println("服务端接收到客户端信息:"+info+",当前客户端ip为:"+socket.getInetAddress().getHostAddress());
                }

                outputStream = socket.getOutputStream();
                printWriter = new PrintWriter(outputStream);
                printWriter.print("服务器消息");
                printWriter.flush();
                socket.shutdownOutput();//关闭输出流
                // 关闭相对应的资源
                printWriter.close();
                outputStream.close();
                bufferedReader.close();
                inputStream.close();
                socket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }finally {
                // 关闭相对应的资源
                printWriter.close();
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
05-23 22:02