二.升级版(可以随意聊天)利用多线程
服务器端
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(); } }}