开发者社区> 问答> 正文

iOS socket怎么和java通讯啊

本人新手,在网上下载了ios的socket通信的列子。我用java做服务器 。但是服务器读不出来数据
客户端:

//
//  clientTest.m
//  clientTest
//
//  Created by apple on 12-4-24.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
 
#import "clientTest.h"
 
#define WELCOME_MSG  0
#define ECHO_MSG     1
#define WARNING_MSG  2
 
#define READ_TIMEOUT 15.0
#define READ_TIMEOUT_EXTENSION 10.0
 
#define SERVER_IP   @"192.168.1.104"
#define SERVER_PORT 9527 
 
@implementation clientTest
 
@synthesize sendSocket = _sendSocket;
 
 
- (id)init
{
    if(self = [super init])
    {
        NSString * inputMsgStr = @"你好";
        NSString * content = [inputMsgStr stringByAppendingString:@"\r\n"]; //stringByAppendingString:str是在一个字符串后面追加一个字符串str
        NSData *data = [content dataUsingEncoding: NSUTF8StringEncoding];//数据使用编码类型
         
        NSLog(@"data:%@",[data description]);
        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);
            }
             
            [_sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
        }
         
        if (connectOK) 
        {
            NSLog(@"---------------%@",[data description]);
            [_sendSocket writeData: data withTimeout: -1 tag: 0];
             
        }
  
    }
    return self;
}
 
-(void)connect
{
//
//    NSData *data = [@"12345678" dataUsingEncoding: NSUTF8StringEncoding]; 
//    
//    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);
//        }
//        
//        [_sendSocket setRunLoopModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
//    }
//    
//    if (connectOK) 
//    {
//        [_sendSocket writeData: data withTimeout: -1 tag: 0];
//    }
//    
//    [NSThread detachNewThreadSelector:@selector(newThread) toTarget:self withObject:nil];
}
 
-(void)newThread{
     
    NSAutoreleasePool* p = [[NSAutoreleasePool alloc] init];
     
  
     
     
    [p release];
}
 
#pragma mark - tcp
 
 
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
    NSLog(@"hh%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)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err
//{
//    NSLog(@"%s %d, err = %@", __FUNCTION__, __LINE__, err);
//}
//
- (void)onSocketDidDisconnect:(AsyncSocket *)sock
{
    NSLog(@"%s %d", __FUNCTION__, __LINE__);
     
    self.sendSocket = nil;
}
 
@end

服务端:?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

package com.chg; import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.IO

展开
收起
杨冬芳 2016-06-30 17:51:29 2527 0
1 条回答
写回答
取消 提交回答
  • IT从业

    在 while((len = is.read(b))!=-1){就阻塞了
    好看看你的代码,有可能是java这边按行读,而ios那边又没有换行符,这样就会阻塞了。最好仔ios没有用第三方的socket 用苹果提供的NSStream写socket java端就用最普通的流

    2019-07-17 19:49:37
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载