nio/mina(4) 클라이언트 socket과mina 서버 통신
클라이언트 socket 전송 대상이mina 서버에 잠시 연결되지 않습니다. 아래 중국어 문자열과 유사할 수 있습니다.
미나단 필터에 대한 설정과 관련이 있어야 합니다.
서비스 포트:
1 MinaServer.java
package com.nafio.server;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
public class MinaServer {
private static MinaServer minaServer = null;
// Server Socket
private SocketAcceptor acceptor = new NioSocketAcceptor();
//
private DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
private int bindPort = 8888;
//
public static MinaServer getInstances() {
if (null == minaServer) {
minaServer = new MinaServer();
}
return minaServer;
}
private MinaServer() {
//
// ----------------------------------------
// chain.addLast("myChin", new ProtocolCodecFilter(
// new ObjectSerializationCodecFactory()));
// : MinaServerHandler ,
// acceptor.setHandler(ServerHandler.getInstances());
// ---------------------------------------
//
chain.addLast("myChin", new ProtocolCodecFilter(
new TextLineCodecFactory()));
//
// chain.addLast("myChin", new ProtocolCodecFilter(
// new ObjectSerializationCodecFactory()));
acceptor.setHandler(new ServerHandler());
try {
// ,
acceptor.bind(new InetSocketAddress(bindPort));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(" : --->" + bindPort);
}
public static void main(String[] args) throws Exception {
MinaServer.getInstances();
//new MinaServer();
}
}
2 ServerHandler.java
package com.nafio.server;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import com.nafio.obj.TransferObj;
// , ?
//public class ServerHandler extends IoHandlerAdapter {
public class ServerHandler extends IoFilterAdapter implements IoHandler {
private static ServerHandler samplMinaServerHandler = null;
public static ServerHandler getInstances() {
if (null == samplMinaServerHandler) {
samplMinaServerHandler = new ServerHandler();
}
return samplMinaServerHandler;
}
public ServerHandler() {
}
public void sessionOpened(IoSession session) throws Exception {
System.out.println(" : ");
}
public void sessionClosed(IoSession session) {
}
public void messageReceived(IoSession session, Object message)throws Exception {
//
String str = (String)message;
System.out.println(" : --->"+str);
//System.out.println(" : ");
//
// if (message instanceof TransferObj) {
// TransferObj obj = (TransferObj) message;
// System.out.println(" : --->"+obj.getDate());+
// }
}
public void exceptionCaught(IoSession arg0, Throwable arg1)throws Exception {
}
public void messageSent(IoSession arg0, Object arg1) throws Exception {
}
public void sessionCreated(IoSession arg0) throws Exception {
System.out.println(" : ");
}
public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
}
}
socket 클라이언트
SocketClient.java
package com.nafio.socketclient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import com.nafio.obj.TransferObj;
public class SocketClient {
private Socket s;
private DataOutputStream out;
private DataInputStream in;
public SocketClient() throws IOException {
}
public static void main(String[] args) throws Exception {
SocketClient c = new SocketClient();
c.talk();
}
//
// ObjectOutputStream oos;
// TransferObj obj;
public void sendMessage(Socket s) {
try {
//socket
out = new DataOutputStream(s.getOutputStream());
byte[] bt="
".getBytes();
out.write(bt);
out.writeBytes("nafio_date
");
//out.writeUTF("
");//by nafio
//socket
// oos = new ObjectOutputStream(s.getOutputStream());
// obj=new TransferObj();
// obj.setDate("socketDateToMina");
// oos.writeObject(obj);
// oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void receiveMessage(Socket s) {
try {
in = new DataInputStream(s.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
public void talk() throws Exception {
/*while (true)*/ {
try {
//
//oos.close();
s = new Socket("localhost", 8888);
System.out.println(" : ");
sendMessage(s);
System.out.println(" !");
//
//receiveMessage(s);
out.close();
//in.close();
}
catch(Exception e){
e.printStackTrace();
}
finally {
try{
if(s!=null)s.close(); //
}catch (IOException e) {e.printStackTrace();}
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.