一.普通版(不能实现随意输入)

电脑A(服务器端)

package day;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Mysever {
    public static void main(String[] args) {
        //服务器端 IP 172.16.242.163  PORT: 5566
        //Socket 插头
        //ServerSocket 插座
        try {
            ServerSocket ss = new ServerSocket(5566);
            Socket server = ss.accept();
            Scanner sca = new Scanner(System.in);
            //服务器发信息
            while (true) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());
                System.out.println("请client发言:");
                String msg = sca.nextLine();
                String info = String.format("Server[%s]:%s\r\n", time, msg);
                server.getOutputStream().write(info.getBytes());
             //接受信息
                byte[] buf = new byte[2012];
                server.getInputStream().read(buf);
                String read = new String(buf);
                System.out.println(read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

电脑B(客户端)

import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Mynet {
    public static void main(String[] args) {
        try{
            Scanner san = new Scanner(System.in);
            Socket client = new Socket("172.16.242.163",5566);

            while(true) {
                byte[] buf = new byte[2048];
                client.getInputStream().read(buf);
                System.out.println(new String(buf));//收信息

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String time = sdf.format(new Date());
                System.out.print("给service回话:");
                String msg = san.nextLine();
                String info = String.format("Client[%s]:%s\r\n", time, msg);
                client.getOutputStream().write(info.getBytes());//发信息
            }

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

}

二.升级版(可以随意聊天)利用多线程

服务器端

package day;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class MySend implements Runnable {
    private OutputStream os;

    public MySend(OutputStream os) {
        this.os = os;
    }

    @Override
    public void run() {
        while(true){
            Scanner sc = new Scanner(System.in);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(new Date());
            System.out.print("给用户发言:");
            String msg = sc.nextLine();
            String info = String.format("Server[%s]:%s\r\n", time, msg);
            try {
                this.os.write(info.getBytes());
                this.os.flush();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

-------------------------------------------------------------------------------------------------

package day;

import java.io.InputStream;

public class MyRead implements Runnable {
    private InputStream is;
    public MyRead(InputStream is){
        this.is = is;
    }
    @Override
    public void run() {
        while(true){
            //接收信息
            byte[] buf = new byte[2014];
            try {
                this.is.read(buf);
                System.out.println(new String(buf));
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
--------------------------------------------------------------------------------------------------
package day; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; //service 服务器端 public class Servertext { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(5566); Socket server = ss.accept(); new Thread(new MySend(server.getOutputStream())).start(); new Thread(new MyRead(server.getInputStream())).start(); } catch (IOException e) { e.printStackTrace(); } } } //多个用户怎么办 /* list<Socket> sss = new ArrayList<Socket>(); while(true){ Socket server = ss.accept(); sss.add(server); } */

用户端

package day;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class MySend implements Runnable {
    private OutputStream os;

    public MySend(OutputStream os) {
        this.os = os;
    }

    @Override
    public void run() {
        while(true){
            Scanner sc = new Scanner(System.in);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(new Date());
            System.out.print("给服务器发言:");
            String msg = sc.nextLine();
            String info = String.format("Client[%s]:%s\r\n", time, msg);
            try {
                this.os.write(info.getBytes());
                this.os.flush();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

-------------------------------------------------------------------------------------------------

package day;

import java.io.InputStream;

public class MyRead implements Runnable {
    private InputStream is;
    public MyRead(InputStream is){
        this.is = is;
    }
    @Override
    public void run() {
        while(true){
            //接收信息
            byte[] buf = new byte[2014];
            try {
                this.is.read(buf);
                System.out.println(new String(buf));
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
--------------------------------------------------------------------------------------------------
package day;

import java.net.Socket;
//client 客户端
public class Text {
public static void main(String[] args) {
try{
Socket client = new Socket("172.16.242.163",5566);
new Thread(new MySend(client.getOutputStream())).start();
new Thread(new MyRead(client.getInputStream())).start();
}catch (Exception e){
e.printStackTrace();
}
}
}