package com.demo; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import javax.swing.JTextArea; //这个类是用来监听数据的 public class SocketData extends Thread{ private Socket socket; private DataOutputStream dos; private DataInputStream dis; private JTextArea textField; public SocketData(Socket clientSocket,JTextArea textField) { this.textField=textField; try { // 得到socket连接 socket = clientSocket; dis = new DataInputStream(socket.getInputStream()); // 得到客户端发来的消息 // in= socket.getInputStream(); } catch (IOException e) { e.printStackTrace(); } } public void run() { try { while (true) { String str = dis.readUTF(); System.out.println("------------来自本地服务器:" + str); textField.append(str+"\n"); } }catch (IOException e){ //e.printStackTrace(); }finally { try { if (dis != null) dis.close(); if (dos != null) dos.close(); if (socket != null) { socket.close(); } } catch (IOException e1) { e1.printStackTrace(); } } } }