1.建客户端DemoSend
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 org.springframework.util.StringUtils;
/**
*
* @author lidaxing
* 客户端建两个线程读、写
*/
public class DemoSend {
public static void main(String[] args) throws IOException {
Socket client = new Socket("127.0.0.1", 8888);
System.out.println("客户端已经建立...");
new ClientRead(client).start();
new ClientWrite(client).start();
}
}
//读服务器信息
class ClientRead extends Thread {
private Socket client;
public ClientRead(Socket client) {
this.client = client;
}
@Override
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = "";
while (true) {
line = br.readLine();
if (!StringUtils.isEmpty(line)) {
System.out.println("服务器说:" + line);
}
if (line.equals("bye")) {
System.out.println("聊天结束!");
return;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//写到服务器信息,回应
class ClientWrite extends Thread {
private Socket client;
public ClientWrite(Socket client) {
this.client = client;
}
@Override
public void run() {
super.run();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
String str = "";
while (true) {
str = input.readLine();
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.建服务端DemoServer
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 org.springframework.util.StringUtils;
/**
*
* @author lidaxing
* 建两个线程,读写
*/
public class DemoServer {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(8888);
System.out.println("服务器启动,等待客户端连接...");
Socket accept = ss.accept();
System.out.println("客户端连接成功...");
new ServerRead(accept).start();
new ServerWrite(accept).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读客户端信息线程
class ServerRead extends Thread {
private Socket s;
public ServerRead(Socket s) {
this.s = s;
}
@Override
public void run() {
super.run();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = "";
while (true) {
line = br.readLine();
if (!StringUtils.isEmpty(line)) {
System.out.println("客户端说:" + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//回应客户端信息线程,写
class ServerWrite extends Thread {
private Socket socket;
public ServerWrite(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
super.run();
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String str = "";
while (true) {
str = input.readLine();
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.测试结果
客户端:
客户端已经建立...
i love you
服务器说:me too
服务端:
服务器启动,等待客户端连接...
客户端连接成功...
客户端说:i love you
me too