(着急 一个星期了)现在c++调java nio成功了 但是send函数到java端没任何效果啊(已经连通le)

老军医2 2014-01-22 09:12:56
java mina 服务器 的函数sessionOpened 和close 都能正常启动和关闭
唯独messageReceived(IoSession session, Object message) 没任何效果啊
大小端我看了例子 貌似没看到 而且好像也不行 是这个原因导致的没任何反映吗?
我好像看到网上也有些网友跟我一样的问题 他们是怎么解决的啊


//
// JniUtil.cpp
// DotaGames
//
// Created by Apple on 14-1-21.
//
//

#include "JniUtil.h"






int JniUtil::connect(const char *ip, unsigned short port)
{
struct sockaddr_in sa;
struct hostent *hp;

hp=gethostbyname(ip);

if (!ip) {
return -1;
}
memset(&sa, 0, sizeof(sa));
memcpy((char *)&sa.sin_addr, hp->h_addr,hp->h_length);
sa.sin_family=hp->h_addrtype;
sa.sin_port=htons(port);

socketHandle=socket(sa.sin_family, SOCK_STREAM, 0);

if (socketHandle < 0) {
printf("connet false");
return -1;
}
if(::connect(socketHandle, (sockaddr*)&sa, sizeof(sa)) < 0){
printf( "failed to connect socket\n" );
::close(socketHandle);
return -1;
}
CCLOG("Client connect OK ! IP: %s:%d ",ip,port);





return 0;

}



int JniUtil::Sends()
{
char reply_msg[1024];

bzero(reply_msg, 1024);
scanf("%s",reply_msg);


if (send(socketHandle, reply_msg, 1024, 0) == -1) {
perror("send error");
}



return 0;

}














package com;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MinaServer {

private static final int PORT=22222;

public static void main(String[] args) throws IOException
{

IoAcceptor acceptor = new NioSocketAcceptor();

DefaultIoFilterChainBuilder chain=acceptor.getFilterChain();

chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));


acceptor.setHandler(new Handle());
acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));
acceptor.bind();



}

}




package com;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class Handle extends IoHandlerAdapter{

@Override
public void sessionOpened(IoSession session) throws Exception {

System.out.println("cline open"+session.getRemoteAddress());
}

@Override
public void messageReceived(IoSession session, Object message)
throws Exception {

String s=message.toString();

System.out.println("cline message is="+s);


SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd mm::hh::ss");
Date date=new Date();
session.write(sdf.format(date));


}

@Override
public void sessionClosed(IoSession session) throws Exception {
// TODO Auto-generated method stub

// syso

System.out.println("sesson is colse");
}

@Override
public void messageSent(IoSession session, Object message) throws Exception {


System.out.println("send to server="+(String)message);
}



}


...全文
21 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告