iOS中的TCP套接字编程.服务器客户端响应 [英] TCP Socket Programming in iOS. Server Client Response
问题描述
我几乎完成了这项任务,但是由于我得到了部分结果,所以我坚持了一点. 我有服务器(Linux或Windows)和客户端(iOS),两者之间存在TCP IP套接字连接.我在iphone仿真器中使用过表单加载,在该应用程序打开时,服务器和iphone之间的连接会自动发生.服务器将数据发送回我在模拟器上发送的数据,并在日志中打印出来.但是我无法完全收到整个回复.对于创新",我可能只是收到"in"或"Innova"等信息.下面是代码段.
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.
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);
}
}
在读取通知中,我确实更改了数组大小.但是我仍然得到错误.对于按钮的IBAction情况相同.即使每次点击,我都在发送数据,而我却没有得到相同数据的响应.
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.
可以提出宝贵的建议吗?
Can valuable suggestion???
推荐答案
一个错误是
int bytesRead = CFReadStreamRead(readStream, bufr,strlen((char*)bufr));
应该是
int bytesRead = CFReadStreamRead(readStream, bufr, sizeof(bufr));
CFReadStreamRead
的最后一个参数是读取缓冲区的容量,它确定读取的最大字节数. strlen((char*)bufr)
是当前在缓冲区中的字符串的 length .您还应该在bufr
中的字符串之前用NULL终止该字符串.
The last parameter of CFReadStreamRead
is the capacity of the read buffer and determines the maximum number of bytes read. strlen((char*)bufr)
is the length of the string currently in the buffer. You should also NULL-terminat the string in bufr
before printing it.
进行此修改后,您的程序可能会使用短字符串.但是,一旦您尝试发送/接收大量数据,就会出现问题.
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 Guide which describes how to register the socket streams with the runloop and handle stream events asynchronously.
这篇关于iOS中的TCP套接字编程.服务器客户端响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!