手记

网络编程+IO流+多线程 实现简单的局域网聊天终端 无界面(大神路过请指点)

服务端 :Servers_Test.java

package Chitchat;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by WTFX on 2016/12/20.
 */

public class Server_Test {
    private static ServerSocket ser=null;   //
    private static Socket sck=null;
    private static String output ="";  //输出流的容器 存放输出的数据
    private static String input=""; //输入流的容器  存放收到的数据
    boolean flag=true;
    boolean flag2=true;
    public static void main(String[] args) {
        Server_Test Test=new Server_Test();
        Test.getStart();
    }
    public void getStart(){
        Input O=new Input();
        Output I=new Output();
        monitor m=new monitor();
        Thread T=new Thread(I,"输入流");
        Thread T2=new Thread(O,"输出流");
        Thread T3=new Thread(m,"服务端");
        T3.start(); //启动服务端
        T3.setPriority(Thread.MAX_PRIORITY);  //将线程优先级设置为最高
        T.start();
        T2.start();
    }

    class Output implements Runnable{  //输出流  向服务端输出消息的流
        @Override
        public void run() {
            if(flag){   //如果此线程先启  则让此线程先睡五秒
                flag=false;
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            while (true) {
                synchronized (this) {
                    InputStreamReader isr=new InputStreamReader(System.in);
                    BufferedReader br=new BufferedReader(isr);
                    try {
                        //this.wait();
                        PrintWriter writer = new PrintWriter(sck.getOutputStream(), true);
                        output = br.readLine();
                        writer.println("服务端发来信息:\t"+output);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    class Input implements  Runnable{

        @Override
        public void run() {
            if(flag2){    //如果此线程先启  则让此线程先睡五秒
                try {
                    flag2=false;
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                BufferedReader br=new BufferedReader(new InputStreamReader(sck.getInputStream()));
                while (true){
                    input=br.readLine();
                    if(input.equals("客户端发来信息:\texit")){
                        System.out.println("客户端"+sck.getInetAddress()+":"+sck.getPort()+"\t下线了");
                        input="";
                    }
                    if(input!=null && input!=""){
                        System.out.println(input);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            }
        }

    class monitor implements Runnable{

        @Override
        public void run() {
            synchronized (this) {
                try {
                    ser = new ServerSocket(8888);
                    while (true) {
                        sck = ser.accept();
                        System.out.println(sck.getInetAddress() + ":" + sck.getPort() + "\t有一个客户端连接上来了!");
                        //this.notifyAll();  //唤醒其他所有线程
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    }

客户端:Client_Test.java

package Chitchat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Created by WTFX on 2016/12/20.
 */
/**
 * 两边的代码几乎一样  所以那边备注我就不写了
 */
public class Client_Test {
    private Socket sck=null;
    private String iuput=""; //输入流的容器 存放输入的容器
    private String output=""; //输出流的容器 存放输出的容器
    boolean flag=true;
    boolean flag2=true;
    public static void main(String[] args) {
        Client_Test Test=new Client_Test();
        Test.getStart();
    }

    public void getStart(){
        Input I=new Input();
        Output O=new Output();
        Link L=new Link();
        Thread T=new Thread(I,"输入流");
        Thread T2=new Thread(O,"输出流");
        Thread T3=new Thread(L,"客户端");
        T2.setDaemon(true);  //设置为守护线程  该线程结束  后台线程也会跟随着结束
        T3.start(); //启动客户端
        T3.setPriority(Thread.MAX_PRIORITY);  //设置线程的优先级
        T.start();
        T2.start();
        /**用线程睡的方法  保证  连接线程先启动
         *
         */
    }

    class Input implements Runnable{  //输入流  接受客户端消息的流

        @Override
        public void run() {
            if(flag){    //如果此线程先启  则让此线程先睡五秒
                try {
                    flag=false;
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (this) {
                try {
                    //this.wait();
                    BufferedReader br = new BufferedReader(new InputStreamReader(sck.getInputStream()));  //获取sck提供的输入流
                    while (true) {
                        iuput = br.readLine();  //获取流中的数据
                        if (iuput != null && iuput != "") {  //判断数据  为空则不进行输出  不为空则输出数据
                            System.out.println(iuput);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class Output implements Runnable{

        @Override
        public void run() {
            if(flag2){   //如果此线程先启  则让此线程先睡五秒
                try {
                    flag2=false;
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));  //创建输入流  获取系统输入的数据
            while(true){
                try {
                    PrintWriter writer=new PrintWriter(sck.getOutputStream(),true);   //创建sckock提供的输出流
                    output=br.readLine();   //获取键盘输入赋值给uotput
                    writer.println("客户端发来信息:\t"+output);  //拼接字符串后写入输出流中
                    if(output.equals("exit")){   //输入exit  退出
                        System.out.println("谢谢使用!");
                        br.close();  //关闭流
                        break;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    class Link implements Runnable{

        @Override
        public void run() {
            synchronized (this) {
                try {
                    sck = new Socket("127.0.0.1", 8888);  //链接 服务端
                    //this.notifyAll();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
0人推荐
随时随地看视频
慕课网APP

热门评论

这个服务端和客户端能正常通信吗?

查看全部评论