xcode iOS 中的 TCP 套接字编程。服务器客户端响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15583366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:03:03  来源:igfitidea点击:

TCP Socket Programming in iOS. Server Client Response

iosobjective-cxcodetcpclienttcpsocket

提问by iPhany

I'm almost done with this task but i'm stuck a point due to which i'm getting partial result. I have server(linux or windows) and client(iOS) between which TCP IP socket connection exist. I have used form load in my iphone simulator where the connection between server and iphone happens automatically as the application opens. Server send the data back what ever I send on simulator and print it in log. But i'm not able to exactly receive the whole response. For "Innovations" I receive maybe just "in" or "Innova"etc.. Below are the code snippets.

我几乎完成了这项任务,但由于我得到了部分结果,我陷入了困境。我有服务器(linux 或 windows)和客户端(iOS),它们之间存在 TCP IP 套接字连接。我在我的 iphone 模拟器中使用了表单加载,其中服务器和 iphone 之间的连接在应用程序打开时自动发生。服务器将数据发回我在模拟器上发送的内容并将其打印在日志中。但我无法完全收到整个回复。对于“创新”,我收到的可能只是“in”或“Innova”等。下面是代码片段。

void TCPClient()
{

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &readStream, &writeStream);
    [NSThread sleepForTimeInterval:2]; //Delay


    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    if(!CFWriteStreamOpen(writeStream))
    {
        NSLog(@"Error Opening Socket");
    }
    else
    {
        UInt8 buf[] = "Innovations";
        int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
        NSLog(@"Written: %d", bytesWritten);
    }
    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    if(!CFReadStreamOpen(readStream))
    {
        NSLog(@"Error reading");
    }
    else
    {
        UInt8 bufr[15];
        int bytesRead  = CFReadStreamRead(readStream, bufr,strlen((char*)bufr));
        NSLog(@"Read: %d", bytesRead);
        NSLog(@"buffer: %s", bufr);
    }
}

Notice in the read I did change the array size. But I still get the error. Same in the case of IBAction of a button. Even in that for every click i'm sending a data and i'm not getting the response of the same data.

请注意,在读取中我确实更改了数组大小。但我仍然收到错误消息。按钮的 IBAction 也是如此。即使每次点击我都会发送数据,但我没有得到相同数据的响应。

Can valuable suggestion???

可以提出宝贵意见吗???

回答by Martin R

One error is that

一个错误是

int bytesRead  = CFReadStreamRead(readStream, bufr,strlen((char*)bufr));

should be

应该

int bytesRead  = CFReadStreamRead(readStream, bufr, sizeof(bufr));

The last parameter of CFReadStreamReadis the capacityof the read buffer and determines the maximum number of bytes read. strlen((char*)bufr)is the lengthof the string currently in the buffer. You should also NULL-terminat the string in bufrbefore printing it.

的最后一个参数CFReadStreamRead是读取缓冲区的容量,决定了读取的最大字节数。strlen((char*)bufr)是当前缓冲区中字符串的长度。您还应该bufr在打印之前以NULL 结尾的字符串。

With this modification, your program might work with short strings. But there will be problems as soon as you try to send/receive larger amounts of data.

通过这种修改,您的程序可能会使用短字符串。但是,一旦您尝试发送/接收大量数据,就会出现问题。

A socket write can write less bytes than you asked it to, and a socket read can return less bytes than you requested.

套接字写入可以写入比您要求的更少的字节,而套接字读取可以返回比您请求的更少的字节。

Have a look at the Stream Programming Guidewhich describes how to register the socket streams with the runloop and handle stream events asynchronously.

查看Stream Programming Guide,它描述了如何使用 runloop 注册套接字流并异步处理流事件。