Tom猫2.0-Socket+Thread

  • Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TestSockClient {
    public static void main(String[] args) {
        DataInputStream dis = null;
        DataOutputStream dos = null;
        Socket socket = null;
        Scanner scan = null;
        String  sendStr;
        String receiveStr;
        try {
            socket = new Socket("localhost", 8888);
            dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());
            scan = new Scanner(System.in);
            do {
                System.out.print("我:");
                sendStr = scan.nextLine();
                dos.writeUTF(sendStr);
                if ((receiveStr = dis.readUTF()) != null) {
                    System.out.println(receiveStr);
                }
            } while (!sendStr.equals("88"));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                dis.close();
                dos.close();
                socket.close();
                scan.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class TestSockServer {
    public static void main(String[] args) {
        ServerSocket server = null;
        Socket socket = null;
        List<ClientService> cslist = new ArrayList<>();
        try{
             //开门营业
            server = new ServerSocket(8888);
            System.out.println("服务器已开启");
            while(true){
                //顾客来
                socket = server.accept();
                //给分配服务员
                ClientService cs = new TestSockServer().new ClientService(socket);
                //记录分配情况
                cslist.add(cs);
                 //开始服务
                cs.start();
            }
        } catch(IOException e){
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            try{
                socket.close();
                server.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
    }
     class ClientService extends Thread{
        Socket socket;
        public ClientService(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            DataOutputStream dos = null;
            DataInputStream dis = null;
            try{
                dos = new DataOutputStream(socket.getOutputStream());
                dis = new DataInputStream(socket.getInputStream());
                String receiveStr ;
                while((receiveStr = dis.readUTF())!= null) {
                    System.out.println(socket.getPort()+"客户端发送:"+receiveStr);
                    dos.writeUTF(socket.getPort()+":"+receiveStr);
                }
            }catch(EOFException e) {
                System.out.println(socket.getPort()+"客户端结束服务");
            }catch(IOException e){
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try{
                    dos.close();
                    dis.close();
                    socket.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 运行结果


ljm要加油
原文地址:https://www.cnblogs.com/ljmmm1/p/14296506.html