请问有人用过AsyncSocket(客户端)与java socket(服务器端)通信吗?
我遇到这情况,服务器端收不到客户端的信息,客户端也收不到服务器返回的信息
但是客户端与服务器端的socket连接却是创建了,这问题是出在哪里呢
新手,请指教。
客户端(ios)代码
// // TestUdpClientAppDelegate.m // TestUdpClient // // Created by Xie Wei on 11-6-5. // Copyright 2011年 e-linkway.com. All rights reserved. // #import "TestTcpClientAppDelegate.h" #import "AsyncUdpSocket.h" #import "AsyncSocket.h" #define SERVER_IP @"192.168.1.109" //#define SERVER_IP @"127.0.0.1" #define SERVER_PORT 3005 @implementation TestUdpClientAppDelegate @synthesize window = _window; @synthesize sendSocket = _sendSocket; //发送短消息 -(IBAction)sendString { NSData *data = [NSData dataWithBytes:"This is a test\n" length:15]; static BOOL connectOK = NO; if (!_sendSocket) { self.sendSocket = [[[AsyncSocket alloc] initWithDelegate: self] autorelease]; NSError *error; connectOK = [_sendSocket connectToHost: SERVER_IP onPort: SERVER_PORT error: &error]; if (!connectOK) { NSLog(@"connect error: %@", error); }else{ NSLog(@"connect succ"); } [_sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]]; } if (connectOK) { [_sendSocket writeData: data withTimeout: -1 tag: 0]; } } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { float button_center_y = 20; float button_center_offset = 50; _sendButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; _sendButton.frame = CGRectMake(0, 0, 200, 30); _sendButton.center = CGPointMake(320 / 2, button_center_y += button_center_offset); [_sendButton addTarget: self action: @selector(sendString) forControlEvents: UIControlEventTouchUpInside]; [_sendButton setTitle: @"Send String" forState: UIControlStateNormal]; [self.window addSubview: _sendButton]; [self.window makeKeyAndVisible]; return YES; } #pragma mark - tcp - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port { NSLog(@"%s %d", __FUNCTION__, __LINE__); [_sendSocket readDataWithTimeout: -1 tag: 0]; } - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag { NSLog(@"%s %d, tag = %ld", __FUNCTION__, __LINE__, tag); [_sendSocket readDataWithTimeout: -1 tag: 0]; } // 这里必须要使用流式数据 - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { NSString *msg = [[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease]; NSLog(@"%s %d, msg = %@", __FUNCTION__, __LINE__, msg); [_sendSocket readDataWithTimeout: -1 tag: 0]; } - (void)onSocketDidDisconnect:(AsyncSocket *)sock { NSLog(@"%s %d", __FUNCTION__, __LINE__); self.sendSocket = nil; } #pragma mark - - (void)dealloc { [_window release]; [super dealloc]; } @end
服务器端(java)代码
package com.dvn.li.main; import java.net.InetSocketAddress; import java.nio.charset.Charset; import org.apache.log4j.Logger; import org.apache.mina.common.IdleStatus; import org.apache.mina.common.IoAcceptor; import org.apache.mina.filter.codec.ProtocolCodecFilter; import org.apache.mina.filter.codec.textline.LineDelimiter; import org.apache.mina.filter.codec.textline.TextLineCodecFactory; import org.apache.mina.transport.socket.nio.NioSocketAcceptor; import com.dvn.li.handler.Demo1ServerHandler; public class MinaServer01 { private static Logger logger = Logger.getLogger(MinaServer01.class); private static int PORT = 3005; public static void main(String[] args) { IoAcceptor acceptor = null; try { // 创建一个非阻塞的server端的Socket acceptor = new NioSocketAcceptor(); // 设置过滤器(使用Mina提供的文本换行符编解码器) acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset .forName("UTF-8"), LineDelimiter.WINDOWS.getValue(), LineDelimiter.WINDOWS.getValue()))); // 设置读取数据的缓冲区大小 acceptor.getSessionConfig().setReadBufferSize(2048); // 读写通道10秒内无操作进入空闲状态 acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10); // 绑定逻辑处理器 acceptor.setHandler(new Demo1ServerHandler()); // 绑定端口 acceptor.bind(new InetSocketAddress(PORT)); logger.info("服务端启动成功... 端口号为:" + PORT); } catch (Exception e) { logger.error("服务端启动异常....", e); e.printStackTrace(); } } }
package com.dvn.li.handler; import java.util.Date; import org.apache.log4j.Logger; import org.apache.mina.common.IdleStatus; import org.apache.mina.common.IoHandlerAdapter; import org.apache.mina.common.IoSession; public class Demo1ServerHandler extends IoHandlerAdapter { public static Logger logger = Logger.getLogger(Demo1ServerHandler.class); @Override public void sessionCreated(IoSession session) throws Exception { logger.info("服务端111与客户端创建连接..."); //System.out.println("服务端111与客户端创建连接..."); } @Override public void sessionOpened(IoSession session) throws Exception { logger.info("服务端111与客户端连接打开..."); //System.out.println("服务端111与客户端连接打开..."); } @Override public void messageReceived(IoSession session, Object message) throws Exception { System.out.println("messageReceived"); String msg = message.toString(); System.out.println("服务端111接收到的数据为:" + msg); logger.info("服务端111接收到的数据为:" + msg); if ("bye".equals(msg)) { // 服务端断开连接的条件 session.close(); } Date date = new Date(); session.write("1234567"); //Thread.sleep(10000); } @Override public void messageSent(IoSession session, Object message) throws Exception { session.close(); logger.info("服务端111发送信息成功..."); //System.out.println("服务端111发送信息成功..."); } @Override public void sessionClosed(IoSession session) throws Exception { } @Override public void sessionIdle(IoSession session, IdleStatus status) throws Exception { logger.info("服务端111进入空闲状态..."); //System.out.println("服务端111进入空闲状态..."); } @Override public void exceptionCaught(IoSession session, Throwable cause) throws Exception { logger.error("服务端111发送异常...", cause); //System.out.println("服务端111发送异常..."); } }
解决方案:10分
我都是用 bsd socket 写的, 这个没用过啊,帮你顶顶
解决方案:30分
我使用GCD的AsyncSocket。
连接服务器端进行SOCKET通信是没问题的。
你是用RunLoop写的。
有些地方我没细看。
连接服务器端进行SOCKET通信是没问题的。
你是用RunLoop写的。
有些地方我没细看。
解决方案:40分
LZ可以先抓包看看,看数据包是否发送出去,以及对方是否收到数据包