JavaSocket框架ApacheMINA实现Socket服务器-客户端双向
即时消息通信
Java Socket框架Apache MINA实现Socket服务器-客户端双向即时消息通信
基于Java Socket框架Apache MINA,实现⼀对简单的基于Socket的服务-客户端双向即时消息通信。服务器绑定本地80端⼝,客户端连接服务器,建⽴连接后,双⽅开始互发消息服务器端可客户端均能在控制台收发消息(控制台输⼊字符串然后按enter键发送)。当任意⼀⽅发送消息字符串“bye”时,通信结束。
服务器端程序代码,⽤Apache MINA实现:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.InetSocketAddress;
import java.nio.charset.Charset;
import urrent.CompletableFuture;
import org.service.IoAcceptor;
import org.service.IoHandlerAdapter;
import org.session.IdleStatus;
import org.session.IoSession;
import org.apache.dec.ProtocolCodecFilter;
import org.apache.line.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.ansport.socket.nio.NioSocketAcceptor;
public class MINATest {
private IoSession mIoSession;
public static void main(String[] args) {
try {
MINATest test = new MINATest();
} catch (Exception e) {
e.printStackTrace();
}
}
public MINATest() throws Exception {
IoAcceptor acceptor = new NioSocketAcceptor();
// 过滤链。
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
SocketServerHandler socketServerHandler = new SocketServerHandler();
// 处理。
acceptor.setHandler(socketServerHandler);
// 服务器端绑定80端⼝,等待客户端连接请求。
acceptor.bind(new InetSocketAddress(80));
waitServerInput();
}
// Apache MINA的Socket服务器端。
private class SocketServerHandler extends IoHandlerAdapter {
public SocketServerHandler() {
}
}
// 会话创建。
@Override
public void sessionCreated(IoSession session) throws Exception {
super.sessionCreated(session);
// System.out.println("#sessionCreated#");
}
// 会话打开时触发(第⼀次连接打开时先触发sessionCreated,后触发本函数)。
@Override
public void sessionOpened(IoSession session) throws Exception {
super.sessionOpened(session);
// System.out.println("#sessionOpened#");
mIoSession = session;
}
@Override
public void messageSent(IoSession session, Object message) throws Exception {
// System.out.println("\n#messageSent#");
// System.out.String());
// System.out.println("=============");
}
// 接收到消息时触发。
@Override
public void messageReceived(IoSession session, Object message) throws Exception {  ssageReceived(session, message);
// System.out.println("messageReceived");
System.out.println("客户端说:" + String());
}
@Override
public void sessionClosed(IoSession session) throws Exception {
super.sessionClosed(session);
System.out.println("\n#sessionClosed#");
sessionInfoPrinter(session);
}
@Override
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
super.sessionIdle(session, status);
System.out.println("#sessionIdle#");
}
@Override
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {  ptionCaught(session, cause);
System.out.println("#exceptionCaught#");
sessionInfoPrinter(session);
cause.printStackTrace();
}
}
private void sessionInfoPrinter(IoSession session) {
StringBuffer sb = new StringBuffer();
sb.append("\n" + String());
sb.append("\nId:" + Id());
sb.append("\nLocalAddress:" + LocalAddress());
sb.append("\nRemoteAddress:" + RemoteAddress());
System.out.String());
}
private void waitServerInput() {
CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
while (true) {
try {
s = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
try {
mIoSession.write(s);
} catch (Exception e) {
e.printStackTrace();
}
if (s.equals("bye")) {
break;
}
}
try {
br.close();
mIoSession.closeOnFlush();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
客户端代码,⽤Java⾃⾝的Socket实现:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.Socket;
import urrent.CompletableFuture;
public class ClientTest {
private Socket socket;
public static void main(String[] args) {
try {
ClientTest test = new ClientTest();
} catch (Exception e) {
e.printStackTrace();
}mina框架
}
public ClientTest() throws Exception {
socket = new Socket("localhost", 80);
BufferedInputStream bis = new InputStream());
BufferedInputStream bis = new InputStream());
waitClientInput();
int count = 0;
byte[] buffer = new byte[1024];
while (true) {
count = ad(buffer);
if (count == -1) {
break;
}
System.out.print("服务器说:" + new String(buffer, 0, count, "UTF-8"));
}
bis.close();
socket.close();
}
private void waitClientInput() {
CompletableFuture.runAsync(new Runnable() {
@Override
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStream()));    } catch (Exception e) {
e.printStackTrace();
}
String s = null;
while (true) {
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s.equals("bye")) {
try {
bw.write(s);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
try {
// 注意!此处如果不主动为输⼊的字符串加⼊\n换⾏符,则MINA服务器端收不到。      s = s + "\n";
bw.write(s);
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
br.close();
socket.close();
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
先启动服务器端程序,程序运⾏后输出结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。