Я пытаюсь использовать CFWriteStreamWrite для отправки данных с iPhone на ноутбук, на котором работает macox.
На ноутбуке (macOS) код приема равен
int sockfd, newsockfd, portno;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
PacketSize = 672629;
char *blob = (char *)malloc(PacketSize*sizeof(char));
listen(sockfd,5);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");
uint64_t ByteToRead = PacketSize;
char *buffer= blob;
/*
n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);
*/
while (ByteToRead>0)
{
n = recv(newsockfd, buffer, ByteToRead, 0);
//n = read(newsockfd,buffer,min(1024, ByteToRead));
if (n < 0)
error("ERROR reading from socket");
ByteToRead -= n;
buffer += n;
printf ("just read %d, %d left\n", n, ByteToRead);
}
И нана стороне телефона (ios5), я использую метод runloop:
NSString *iHostname = [NSString stringWithFormat:@"192.168.1.66"];
NSString *iPort = [NSString stringWithFormat:@"2553"];
static const CFOptionFlags kWriteNetworkEvents = kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred |
kCFStreamEventCanAcceptBytes |
kCFStreamEventOpenCompleted |
kCFStreamEventNone;
CFStreamClientContext ctxt = {0,(void*)self,NULL,NULL,NULL};
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault,(CFStringRef)iHostname);
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, hostRef, [iPort intValue],
NULL, &writeStream);
CFWriteStreamSetClient(writeStream, kWriteNetworkEvents, MyWriteStreamClientCallBack, &ctxt);
CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFWriteStreamOpen(writeStream);
Функция обратного вызова:
static void MyWriteStreamClientCallBack( CFWriteStreamRef stream, CFStreamEventType type, void *clientCallBackInfo )
{
UploadTableViewController *self = [[(UploadTableViewController *)clientCallBackInfo retain] autorelease];
NSLog(@"in call back");
switch (type)
{
case kCFStreamEventEndEncountered:
{
NSLog(@"in CallBack: kCFStreamEventEndEncounteredd");
CFWriteStreamClose(stream);
break;
}
case kCFStreamEventErrorOccurred:
{
NSLog(@"in CallBack: kCFStreamEventErrorOccurred");
//CFStreamError error = CFWriteStreamGetError(stream);
//NSLog([[self errorFromCFStreamError:error] domain]);
//NSLog(@"%d",[[self errorFromCFStreamError:error] code] );
break;
}
case kCFStreamEventCanAcceptBytes:
{
NSLog(@"in CallBack: kCFStreamEventCanAcceptBytes, %llu bytes to send", self->ByteToSent);
CFIndex ByteSent = CFWriteStreamWrite(stream, self->WriteStart, (CFIndex)(self->ByteToSent));
if (ByteSent > 0)
{
self->ByteToSent -= ByteSent;
self->WriteStart += ByteSent;
NSLog(@"just wrote %ld bytes, %lld left", ByteSent, self->ByteToSent);
}
else
NSLog(@"CFWriteStreamWrite returned %ld", ByteSent);
}
case kCFStreamEventNone:
{
NSLog(@"in CallBack: kCFStreamEventNone");
break;
}
case kCFStreamEventOpenCompleted:
{
NSLog(@"in CallBack: kCFStreamEventNone");
break;
}
}
Проблема в том, что посылка останавливается посередине.Например, на телефоне я получил это от XCODE:
2011-10-19 18:26:04.062 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.736 whereamI[1818:707] in call back
2011-10-19 18:26:26.738 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.739 whereamI[1818:707] in call back
2011-10-19 18:26:26.740 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 672629 bytes to send
2011-10-19 18:26:26.745 whereamI[1818:707] just wrote 131768 bytes, 540861 left
2011-10-19 18:26:26.746 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.753 whereamI[1818:707] in call back
2011-10-19 18:26:26.755 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 540861 bytes to send
2011-10-19 18:26:26.756 whereamI[1818:707] CFWriteStreamWrite returned -1
2011-10-19 18:26:26.757 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:26:26.758 whereamI[1818:707] in call back
2011-10-19 18:26:26.759 whereamI[1818:707] in CallBack: kCFStreamEventErrorOccurred
2011-10-19 18:45:49.101 whereamI[1818:707] in call back
2011-10-19 18:45:49.102 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:45:49.104 whereamI[1818:707] in call back
2011-10-19 18:45:49.105 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 672629 bytes to send
2011-10-19 18:45:49.110 whereamI[1818:707] just wrote 131768 bytes, 540861 left
2011-10-19 18:45:49.111 whereamI[1818:707] in CallBack: kCFStreamEventNone
2011-10-19 18:45:49.120 whereamI[1818:707] in call back
2011-10-19 18:45:49.121 whereamI[1818:707] in CallBack: kCFStreamEventCanAcceptBytes, 540861 bytes to send
2011-10-19 18:45:49.123 whereamI[1818:707] just wrote 2896 bytes, 537965 left
2011-10-19 18:45:49.124 whereamI[1818:707] in CallBack: kCFStreamEventNone
Список на самом деле длинный, и это только конечная часть.
В то время как на ноутбуке вывод
max@machine:~/socket$ ./newserver.x 2553
just read 1448, 671181 left
ERROR reading from socket: Socket operation on non-socket
В коде C я также пробовал
n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);
без цикла и
n = read(newsockfd,buffer,min(1024, ByteToRead));
в цикле.Оба также не могут завершить передачу.
На телефоне, который я также пытался использовать только разъем BSD:
n = write(sockfd, &PackageSize, sizeof(PackageSize));
для сопряжения с
n=recv(newsockfd, blob, PacketSize, MSG_WAITALL);
Ипередача завершается успешно.
Так что, пожалуйста, помогите.
Спасибо.