问题描述
我在使用c sharp服务器连接xcode客户端时遇到了一些问题。我在虚拟机中运行mac并获得此输出
014-03-19 10:30:12.468 FYPSeconf [761:c07] Tcp客户端初始化
2014-03 -19 10:30:12.487 FYPSeconf [761:c07] TCP客户端 - 无法连接到主机
2014-03-19 10:30:12.487 FYPSeconf [761:c07]数据已发送登录,,, 192.168.1.1
2014-03-19 10:30:12.488 FYPSeconf [761:c07]收到的数据(null)
2014-03-19 10: 30:12.679 FYPSeconf [761:c07] TCP客户端 - 无法连接到主机
以下是我正在使用的代码。
- (无效)TcpClientInitialise
{
NSLog(@Tcp客户端初始化);
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@localhost,9050,& readStream,& writeStream);
InputStream =(__ bridge NSInputStream *)readStream;
OutputStream =(__ bridge NSOutputStream *)writeStream;
[InputStream setDelegate:self];
[OutputStream setDelegate:self];
[InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[InputStream打开];
[OutputStream打开];
}
和接收流
i am facing some problems with connecting xcode client with c sharp server. i am running mac in a virtual machine and getting this output
014-03-19 10:30:12.468 FYPSeconf[761:c07] Tcp Client Initialise
2014-03-19 10:30:12.487 FYPSeconf[761:c07] TCP Client - Can't connect to the host
2014-03-19 10:30:12.487 FYPSeconf[761:c07] Data Sent LOGIN,,,192.168.1.1
2014-03-19 10:30:12.488 FYPSeconf[761:c07] Data Recieved (null)
2014-03-19 10:30:12.679 FYPSeconf[761:c07] TCP Client - Can't connect to the host
following is the code that i am using.
- (void)TcpClientInitialise
{
NSLog(@"Tcp Client Initialise");
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 9050, &readStream, &writeStream);
InputStream = (__bridge NSInputStream *)readStream;
OutputStream = (__bridge NSOutputStream *)writeStream;
[InputStream setDelegate:self];
[OutputStream setDelegate:self];
[InputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[OutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[InputStream open];
[OutputStream open];
}
and for receiving stream
- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)StreamEvent
{
switch (StreamEvent)
{
case NSStreamEventOpenCompleted:
NSLog(@"TCP Client - Stream opened");
break;
case NSStreamEventHasBytesAvailable:
if (theStream == InputStream)
{
uint8_t buffer[1024];
int len;
while ([InputStream hasBytesAvailable])
{
len = [InputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output)
{
NSLog(@"TCP Client - Server sent: %@", output);
}
//Send some data (large block where the write may not actually send all we request it to send)
int ActualOutputBytes = [OutputStream write:[OutputData bytes] maxLength:[OutputData length]];
//if (ActualOutputBytes >= ChunkToSendLength)
//{
//It was all sent
//[OutputData release];
OutputData = nil;
//}
//else
//{
//Only partially sent
// [OutputData replaceBytesInRange:NSMakeRange(0, ActualOutputBytes) withBytes:NULL length:0]; //Remove sent bytes from the start
//}
myRecievedString = output;
}
}
}
break;
case NSStreamEventErrorOccurred:
NSLog(@"TCP Client - Can't connect to the host");
break;
case NSStreamEventEndEncountered:
NSLog(@"TCP Client - End encountered");
[theStream close];
[theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
break;
case NSStreamEventNone:
NSLog(@"TCP Client - None event");
break;
case NSStreamEventHasSpaceAvailable:
NSLog(@"TCP Client - Has space available event");
if (OutputData != nil)
{
//Send rest of the packet
int ActualOutputBytes = [OutputStream write:[OutputData bytes] maxLength:[OutputData length]];
if (ActualOutputBytes >= [OutputData length])
{
//It was all sent
//[OutputData release];
OutputData = nil;
}
else
{
//Only partially sent
[OutputData replaceBytesInRange:NSMakeRange(0, ActualOutputBytes) withBytes:NULL length:0]; //Remove sent bytes from the start
}
}
break;
default:
NSLog(@"TCP Client - Unknown event");
}
}
please help
and thanks in advance
推荐答案
这篇关于我在xcode客户端使用c#socket编程时遇到某种连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!