这是一个基于控制台的多线程聊天程序,感觉在线程这块代码可以优化下
话不多说,直接贴出代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* 客户端
* @author Administrator
*
*/
public class Client {
public static void main(String[] args) {
Socket client = null;
try {
client = new Socket("127.0.0.1", 9999);
// 发送消息
Thread out = new Thread(new Output(client));
out.start();
// 接受消息
Thread in = new Thread(new Input(client));
in.start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 客户端的接收线程
* @author Administrator
*
*/
class Input implements Runnable {
private Socket client;
private BufferedReader br;
public Input(Socket client) {
try {
br = new BufferedReader(new InputStreamReader(
client.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
char[] msg = new char[2048];
try {
br.read(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("服务器:" + String.valueOf(msg));
}
}
}
/**
* 客户端的发送线程
* @author Administrator
*
*/
class Output implements Runnable {
private Socket client = null;
private BufferedWriter bw = null;
Scanner sc = new Scanner(System.in);
public Output(Socket client) {
try {
bw = new BufferedWriter(new OutputStreamWriter(
client.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String info = null;
@Override
public void run() {
while (info != "exit") {
try {
System.out.println("请输入消息: ");
info = sc.nextLine();
bw.write(info);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
<pre name="code" class="java">
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
/**
* 服务器端
*
* @author Administrator
*
*/
public class Server {
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(9999);
System.out.println("服务器已启动。。。。。。。。。");
Socket socket = server.accept();
System.out.println("客户机上线了!");
// 读取客户端的消息
Thread out = new Thread(new SOutput(socket));
out.start();
// 向客户端发送消息
Thread in = new Thread(new SInput(socket));
in.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("服务器启动失败。。。。。。。。。");
}
}
}
/**
* 服务器的接收线程
* @author Administrator
*
*/
class SInput implements Runnable {
private Socket client;
private BufferedReader br;
public SInput(Socket client) {
try {
br = new BufferedReader(new InputStreamReader(
client.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
char[] msg = new char[2048];
try {
br.read(msg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("客户端:" + String.valueOf(msg));
}
}
}
/**
* 服务器的发送线程
* @author Administrator
*
*/
class SOutput implements Runnable {
private Socket client = null;
private BufferedWriter bw = null;
Scanner sc = new Scanner(System.in);
public SOutput(Socket client) {
try {
bw = new BufferedWriter(new OutputStreamWriter(
client.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
System.out.println("请输入消息: ");
String info = sc.nextLine();
try {
bw.write(info);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}