本人新手,在网上下载了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
在 while((len = is.read(b))!=-1){就阻塞了
好看看你的代码,有可能是java这边按行读,而ios那边又没有换行符,这样就会阻塞了。最好仔ios没有用第三方的socket 用苹果提供的NSStream写socket java端就用最普通的流
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。