iPod客户端永久向C服务器发送数据?

发布于 2024-11-16 02:37:56 字数 1538 浏览 3 评论 0原文

我有一个 iOS 客户端,通过 TCP/IP 设置了 Linux C 服务器。我面临的问题是: 连接完成后,服务器等待数据 (read()),并在从 iPod 接收到数据后将其显示在屏幕上。然后它再次返回到 read() ,依此类推。我可以执行一次读/写操作,但不能永久执行此操作。代码是:

-(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;

好的。据我所知,一旦服务器端有读取,此方法和案例就会自动运行。这在服务器上的第一次 read() 调用中效果很好,但在第二次调用时,服务器只是保持挂起状态以获取一些数据。 我在 xcode 中的日志显示了发生的情况:

2011-06-17 16:23:50.154 sliderFinal[7430:207] >>> : NSStreamEventOpenCompleted //其中一个流已打开

2011-06-17 16:23:50.156 sliderFinal[7430:207] << : NSStreamEventOpenCompleted //另一个流已打开

2011-06-17 16:23:50.157 sliderFinal[7430:207] 数据已发送 //第一次read()后从服务器发送数据

2011-06-17 16:23:50.159 sliderFinal[7430:207] << : NSStreamEventHasSpaceAvailable //这是指来自服务器的第一个 read() 调用

我看到在第一轮之后,服务器一直在等待某些东西。该消息似乎不会再次到达 NSStreamEventHasSpaceAvailable 。 有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2024-11-23 02:37:56

看来我已经成功了。不确切知道我在问题中发布的代码是否有错误(我不这么认为,因为它实际上发送了我想要的消息),但我最终使用了另一种编写方式。

这是使用的方法:

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

这是我使用的代码(而不是问题中发布的代码):

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;

我还注意到 read()printf(msg)< 之后的服务器/em>,它会向我显示一条消息,说明连接已终止,因此我更改了服务器端的代码,在其中我检查服务器是否仍与客户端连接,例如这个:

<代码>检查= recv(newsockf,buffer,256, MSG_PEEK|MSG_DONTWAIT);

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

..对此:

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

它似乎已经成功了。现在它不会无缘无故地断开连接。问题解决了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文