服务端
package com.ninemax.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) {
try {
// 监听端口
ServerSocket server = new ServerSocket(9999);
// 获取客户端链接
Socket client = server.accept();
// 获取客户端输入流
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
// 获取客户端输出流
PrintWriter out = new PrintWriter(client.getOutputStream());
// 获取系统输入流
BufferedReader sys_in = new BufferedReader(new InputStreamReader(System.in));
// 启动接收消息线程
new ReceiveMsgThread(server, in, out, sys_in, client).start();
// 启动发送消息线程
new SendMsghread(out, sys_in, true).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
package com.ninemax.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketClient {
public static void main(String[] args) {
try {
// 获取客户端链接
Socket client = new Socket(InetAddress.getLocalHost(), 9999);
System.out.println("客户端是否连接成功:" + client.isConnected());
// 获取客户端输入流
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
// 获取系统输入流
BufferedReader sys_in = new BufferedReader(new InputStreamReader(System.in));
// 获取客户端输出流
PrintWriter out = new PrintWriter(client.getOutputStream());
// 启动发送消息线程
new SendMsghread(out, sys_in, false).start();
// 启动接收消息线程
new ReceiveMsgThread(client, in, out, sys_in).start();
} catch (UnknownHostException e) {
System.out.println("Host is error!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
发送消息线程
package com.ninemax.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SendMsghread extends Thread {
// 输出流
PrintWriter out;
// 输入流
BufferedReader in;
// 判断服务器是否启动
boolean isServer;
public SendMsghread(PrintWriter out, BufferedReader in, boolean isServer) {
this.out = out;
this.in = in;
this.isServer = isServer;
}
@Override
public void run() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
while(true){
if(isServer){
out.println("Server "+sf.format(new Date())+"\n\t"+ in.readLine());
}else{
out.println("client "+sf.format(new Date())+"\n\t"+ in.readLine());
}
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
接收消息线程
package com.ninemax.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ReceiveMsgThread extends Thread {
// 输入流
BufferedReader in;
// 输入流
BufferedReader sys_in;
// 输出流
PrintWriter out;
// 服务端socket
ServerSocket server;
// 客户端socket
Socket client;
public ReceiveMsgThread(ServerSocket server, BufferedReader in,
PrintWriter out, BufferedReader sys_in, Socket client) {
this.in = in;
this.server = server;
this.client = client;
this.out = out;
this.sys_in = sys_in;
}
public ReceiveMsgThread(Socket client,BufferedReader in, PrintWriter out,
BufferedReader sys_in) {
this.in = in;
this.client = client;
this.out = out;
this.sys_in = sys_in;
}
@Override
public void run() {
try {
while(true){
String info = in.readLine();
while(info !=null){
System.out.println(info);
info = in.readLine();
}
if(in.readLine().equals("end")){
break;
}
}
in.close();
out.close();
sys_in.close();
if(client != null){
client.close();
}
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果图
来源:oschina
链接:https://my.oschina.net/u/2400848/blog/1510598