开发者

iPod client permanently sending data to C server?

I have an iOS client set up with a linux C server via TCP/IP. The problem i am facing is: After the connection is done, the server waits for data (read()) and presents it in the screen once received from the iPod. Then it goes back to the read() again and so on. I can do this read/write once, but not permanently. The code is:

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent

      case NSStreamEventHasSpaceAvailable:
         event = @"NSStreamEventHasSpaceAvailable";
        connectButton.enabled = NO;
        disconnectButton.enabled = YES;

        if (theStream == oStream)
         {
         //send data                

             uint8_t buffer[11] = "I send this";             
             int len;

             len = [oStream write:buffer maxLength:sizeof(buffer)];
             if (len > 0)
            {
                NSLog(@"Data sent");
                [oStream close];
            }
         }
开发者_运维知识库
        break;

OK. So for what i know, this method and case will run automatically once there is a read on the server side. This works great in the first read() call on the server, but on the second call the server just keeps there hanging for some data. The log i have in xcode shows what happens:

2011-06-17 16:23:50.154 sliderFinal[7430:207] >> : NSStreamEventOpenCompleted //One of the streams was opened

2011-06-17 16:23:50.156 sliderFinal[7430:207] << : NSStreamEventOpenCompleted //The other stream was opened

2011-06-17 16:23:50.157 sliderFinal[7430:207] Data sent //The data was sent after the first read() from the server

2011-06-17 16:23:50.159 sliderFinal[7430:207] << : NSStreamEventHasSpaceAvailable //This refers to the first read() call from the server

I see that after the first round, the server keeps waiting for something. This message just seems not to reach the NSStreamEventHasSpaceAvailable again. Any ideas?


Seems i got it working. Don't know exactly if the code i posted in my question had errors (i don't think so, because it actually sent the msg i wanted), but I ended up using another way of writing.

This was the method used:

 -(NSInteger) writeToServer:(const uint8_t *) buf 
  {
     return [oStream write:buf maxLength:strlen((char*)buf)];    
  }

And this the code I used (instead of the one posted in the question):

case NSStreamEventHasSpaceAvailable:
    event = @"NSStreamEventHasSpaceAvailable";
        connectButton.enabled = NO;
        disconnectButton.enabled = YES;

        if (theStream == oStream)
         {
         //send data

             NSString *msg = [[NSString alloc] initWithFormat:@"ping"];
             const uint8_t *buffer = (const uint8_t *)[msg UTF8String];  
             NSInteger err = [self writeToServer:buffer];
             [msg release];

             if ( err == -1)
                 NSLog(@"Error sending data."); 
             else   
                 NSLog(@"Success sending data.");

break;

Also I noticed that the server after the read() and printf(msg), it would present me with a msg saying the connection had been terminated, so I changed the code on the server side where I had a check whether the server was still connected with the client, like this:

check = recv(newsockf,buffer,256, MSG_PEEK|MSG_DONTWAIT);

if(!(check < 0 && errno == EAGAIN))
{
  //close socket;
  //accept();
}

..to this:

if((check < 0 && errno != EAGAIN))
{
  //close socket;
  //accept();
}

It seems to have done the trick. Now it doesn't disconnect for nothing. Problem Solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜