Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
- (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
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900