集册 Java实例教程 基于线程的客户端套接字

基于线程的客户端套接字

欢马劈雪     最近更新时间:2020-01-02 10:19:05

417
基于线程的客户端套接字

import java.io.*;/*from n o w  j a v a  . c o m*/

import java.net.*;

import java.util.*;


public class Client extends Thread {

    Socket clientSocket = null;


    public Client(Socket s) {

        clientSocket = s;

    }


    public void run() {

        if (clientSocket == null) {

            return;

        }

        /*来自 
         时 代 J a v a 公 众 号*/

        PrintStream out = null;


        System.out.println("creating output stream");


        try {

            out = new PrintStream(clientSocket.getOutputStream());

        } catch (IOException e) {

            System.err.println("Error binding output to socket, " + e);

            System.exit(1);

        }


        System.out.println("writing current date");


        Date d = new Date();

        out.println(d);


        try {

            out.close();

            clientSocket.close();

        } catch (IOException e) {

        }

    }


    protected void finalize() {

        if (clientSocket != null) {

            try {

                clientSocket.close();

            } catch (IOException e) {

            }

            clientSocket = null;

        }

    }

}


展开阅读全文