问题
i have implemented some code of file transfer by using this tutorial :
Please Click Here.
and the code which i am using :
- (void)sendToOtherDevice:(NSData *)fileData receiverJid:(NSString *)receiverJid
{
XMPPJID *jid = [XMPPJID jidWithString:receiverJid];
XMPPSIFileTransfer *sifiletransfer = [[XMPPSIFileTransfer alloc] init];
[sifiletransfer initiateFileTransferTo:jid withData:fileData];
NSString *s = [NSString stringWithFormat:@"%@/spark",receiverJid];
NSString *jabbarID = [[[[self appDelegate] xmppStream] myJID] bare];
XMPPJID *senderjid = [XMPPJID jidWithString:jabbarID];
//[TURNSocket setProxyCandidates:[NSArray arrayWithObjects:s, nil]];
// [TURNSocket setProxyCandidates:[NSArray arrayWithObjects:s,jabbarID, nil]];
[TURNSocket setProxyCandidates:[NSArray arrayWithObjects:jid.domain,senderjid.domain, nil]];
// [TURNSocket setProxyCandidates:[NSArray arrayWithObjects:jid.domain, nil]];
//[TURNSocket setProxyCandidates:[NSArray arrayWithObjects:@"111.11.111.111", nil]];
TURNSocket *socket1 = [[TURNSocket alloc] initWithStream:[self xmppStream] toJID:jid];
// [turnSockets addObject:turnSocket];
[socket1 startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}
-(void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket
{
[socket writeData:photoData withTimeout:60.0f tag:0];
[socket disconnectAfterWriting];
}
- (void)turnSocketDidFail:(TURNSocket *)sender
{
NSLog(@"Couldn't set up bytestream for file transfer!");
}
then on running this code i would get the following XMLStangas :
recieving Logs at android end :
12-03 10:26:59.359: D/SMACK(2021): 10:26:59 AM RCV (1095046240): <iq type="set" id="47198142-86E8-41E2-9F68-40C52AFD5469" to="kadhirkarbonn@ip-10-123-60-876/smack" from="kadhirsduos@ip-10-123-60-876/smack"><si xmlns="http://jabber.org/protocol/si" id="A0E740DE-CDF1-408E-ABE1-8F582A615F1E" mime-type="image/jpg" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="4_9.jpg" size="75261"/><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-single"><option><value>http://jabber.org/protocol/bytestreams</value></option></field></x></feature></si></iq>
12-03 10:26:59.399: I/Insert(2021): insert data with file name ===========4_9.jpg
12-03 10:27:01.749: I/Kadhir(2021): 4_9.jpg downloading started
12-03 10:27:01.779: D/SMACK(2021): 10:27:01 AM SENT (1095046240): <iq id="47198142-86E8-41E2-9F68-40C52AFD5469" to="kadhirsduos@ip-10-123-60-876/smack" from="kadhirkarbonn@ip-10-123-60-876/smack" type="result"><si xmlns="http://jabber.org/protocol/si"><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="submit"><field var="stream-method"><value>http://jabber.org/protocol/bytestreams</value></field></x></feature></si></iq>
12-03 10:27:02.039: D/SMACK(2021): 10:27:02 AM RCV (1095046240): <iq type="get" id="ECF5E292-013C-45E5-8053-E2EA86219622" to="kadhirkarbonn@ip-10-123-60-876/smack" from="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
12-03 10:27:02.049: D/SMACK(2021): 10:27:02 AM SENT (1095046240): <iq id="ECF5E292-013C-45E5-8053-E2EA86219622" to="kadhirsduos@ip-10-123-60-876/smack" type="result"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="client" name="quytech" type="phone"/><feature var="http://jabber.org/protocol/caps"/><feature var="http://jabber.org/protocol/disco#info"/><feature var="jabber:iq:privacy"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/ibb"/><feature var="http://jabber.org/protocol/si/profile/file-transfer"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/ibb"/><feature var="http://jabber.org/protocol/si/profile/file-transfer"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/ibb"/></query></iq>
12-03 10:27:02.739: D/SMACK(2021): 10:27:02 AM RCV (1095046240): <iq type="set" to="kadhirkarbonn@ip-10-123-60-876/smack" id="E5132784-1493-458B-98B0-8E3902A541C0" from="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/bytestreams" sid="A0E740DE-CDF1-408E-ABE1-8F582A615F1E" mode="tcp"><streamhost jid="kadhirsduos@ip-10-123-60-876/smack" host="127.0.0.1" port="7777"/><streamhost jid="proxy.ip-10-123-60-876" host="10.123.60.876" port="7777"/></query></iq>
12-03 10:30:02.759: D/SMACK(2021): 10:30:02 AM RCV (1095046240): <iq type="get" id="863-206317" from="ip-10-123-60-876" to="kadhirkarbonn@ip-10-123-60-876/smack"><ping xmlns="urn:xmpp:ping"/></iq>
12-03 10:30:02.779: D/SMACK(2021): 10:30:02 AM SENT (1095046240): <iq id="863-206317" to="ip-10-123-60-876" from="kadhirkarbonn@ip-10-123-60-876/smack" type="error"><error code="501" type="CANCEL"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error></iq>
12-03 10:30:12.259: D/SMACK(2021): 10:30:12 AM SENT (1095046240): <iq id="E5132784-1493-458B-98B0-8E3902A541C0" to="kadhirsduos@ip-10-123-60-876/smack" from="kadhirkarbonn@ip-10-123-60-876/smack" type="error"><error code="406" type="MODIFY"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/><text xml:lang="en" xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">Could not establish socket with any provided host</text></error></iq>
Sending Logs at IOS ends :
2013-12-03 10:21:41:731 iPhoneXMPP[525:3523] SEND: <message type="chat" to="kadhirkarbonn@ip-10-123-60-876"><body>enter message here..</body></message>
2013-12-03 10:21:53.987 iPhoneXMPP[525:a0b] Sender ID is ==== kadhirsduos@ip-10-123-60-876/smack
2013-12-03 10:21:53:990 iPhoneXMPP[525:3523] SEND: <iq type="set" id="47198142-86E8-41E2-9F68-40C52AFD5469" to="kadhirkarbonn@ip-10-123-60-876/smack" from="kadhirsduos@ip-10-123-60-876/smack"><si xmlns="http://jabber.org/protocol/si" id="A0E740DE-CDF1-408E-ABE1-8F582A615F1E" mime-type="image/jpg" profile="http://jabber.org/protocol/si/profile/file-transfer"><file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="4_9.jpg" size="75261"/><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="form"><field var="stream-method" type="list-single"><option><value>http://jabber.org/protocol/bytestreams</value></option></field></x></feature></si></iq>
2013-12-03 10:21:56:721 iPhoneXMPP[525:5407] RECV: <iq xmlns="jabber:client" id="47198142-86E8-41E2-9F68-40C52AFD5469" to="kadhirsduos@ip-10-123-60-876/smack" from="kadhirkarbonn@ip-10-123-60-876/smack" type="result"><si xmlns="http://jabber.org/protocol/si"><feature xmlns="http://jabber.org/protocol/feature-neg"><x xmlns="jabber:x:data" type="submit"><field var="stream-method"><value>http://jabber.org/protocol/bytestreams</value></field></x></feature></si></iq>
2013-12-03 10:21:56:721 iPhoneXMPP[525:a0b] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ:
2013-12-03 10:21:56.721 iPhoneXMPP[525:5407] didRecieveIQ*****************
2013-12-03 10:21:56.722 iPhoneXMPP[525:5407] IQ type === result
2013-12-03 10:21:56.723 iPhoneXMPP[525:5407] IQ type === si
2013-12-03 10:21:56.723 iPhoneXMPP[525:5407] IQ type === result/si
2013-12-03 10:21:56:724 iPhoneXMPP[525:5217] SEND: <iq type="get" id="ECF5E292-013C-45E5-8053-E2EA86219622" to="kadhirkarbonn@ip-10-123-60-876/smack" from="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
2013-12-03 10:21:56:725 iPhoneXMPP[525:5217] SEND: <iq type="get" to="ip-10-123-60-876" id="41BB64C7-4216-4E7E-BC0F-CAEB7D093D45"><query xmlns="http://jabber.org/protocol/disco#items"/></iq>
2013-12-03 10:21:56:965 iPhoneXMPP[525:5407] RECV: <iq xmlns="jabber:client" type="result" id="41BB64C7-4216-4E7E-BC0F-CAEB7D093D45" from="ip-10-123-60-876" to="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/disco#items"><item jid="search.ip-10-123-60-876" name="User Search"/><item jid="conference.ip-10-123-60-876" name="Public Chatrooms"/><item jid="proxy.ip-10-123-60-876" name="Socks 5 Bytestreams Proxy"/><item jid="pubsub.ip-10-123-60-876" name="Publish-Subscribe service"/></query></iq>
2013-12-03 10:21:56:966 iPhoneXMPP[525:a0b] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ:
2013-12-03 10:21:56.966 iPhoneXMPP[525:5217] didRecieveIQ*****************
2013-12-03 10:21:56:967 iPhoneXMPP[525:3523] SEND: <iq type="get" to="proxy.ip-10-123-60-876" id="567B54C6-FE53-4A80-9DC6-13AA3E3C5445"><query xmlns="http://jabber.org/protocol/disco#info"/></iq>
2013-12-03 10:21:56.967 iPhoneXMPP[525:5217] IQ type === result
2013-12-03 10:21:57:087 iPhoneXMPP[525:5217] RECV: <iq xmlns="jabber:client" id="ECF5E292-013C-45E5-8053-E2EA86219622" to="kadhirsduos@ip-10-123-60-876/smack" type="result" from="kadhirkarbonn@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="client" name="quytech" type="phone"/><feature var="http://jabber.org/protocol/caps"/><feature var="http://jabber.org/protocol/disco#info"/><feature var="jabber:iq:privacy"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/ibb"/><feature var="http://jabber.org/protocol/si/profile/file-transfer"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/ibb"/><feature var="http://jabber.org/protocol/si/profile/file-transfer"/><feature var="http://jabber.org/protocol/si"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/ibb"/></query></iq>
2013-12-03 10:21:57:088 iPhoneXMPP[525:a0b] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ:
2013-12-03 10:21:57.089 iPhoneXMPP[525:3523] didRecieveIQ*****************
2013-12-03 10:21:57.090 iPhoneXMPP[525:3523] IQ type === result
2013-12-03 10:21:57:205 iPhoneXMPP[525:3523] RECV: <iq xmlns="jabber:client" type="result" id="567B54C6-FE53-4A80-9DC6-13AA3E3C5445" from="proxy.ip-10-123-60-876" to="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/disco#info"><identity category="proxy" name="SOCKS5 Bytestreams Service" type="bytestreams"/><feature var="http://jabber.org/protocol/bytestreams"/><feature var="http://jabber.org/protocol/disco#info"/></query></iq>
2013-12-03 10:21:57:205 iPhoneXMPP[525:a0b] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ:
2013-12-03 10:21:57:206 iPhoneXMPP[525:5217] SEND: <iq type="get" to="proxy.ip-10-123-60-876" id="F9F121E4-918C-4437-8B4A-E5568E123F57"><query xmlns="http://jabber.org/protocol/bytestreams"/></iq>
2013-12-03 10:21:57.206 iPhoneXMPP[525:5407] didRecieveIQ*****************
2013-12-03 10:21:57.208 iPhoneXMPP[525:5407] IQ type === result
2013-12-03 10:21:57:325 iPhoneXMPP[525:5407] RECV: <iq xmlns="jabber:client" type="result" id="F9F121E4-918C-4437-8B4A-E5568E123F57" from="proxy.ip-10-123-60-876" to="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/bytestreams"><streamhost jid="proxy.ip-10-123-60-876" host="10.123.60.876" port="7777"/></query></iq>
2013-12-03 10:21:57:326 iPhoneXMPP[525:a0b] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ:
2013-12-03 10:21:57:327 iPhoneXMPP[525:5407] SEND: <iq type="set" to="kadhirkarbonn@ip-10-123-60-876/smack" id="E5132784-1493-458B-98B0-8E3902A541C0" from="kadhirsduos@ip-10-123-60-876/smack"><query xmlns="http://jabber.org/protocol/bytestreams" sid="A0E740DE-CDF1-408E-ABE1-8F582A615F1E" mode="tcp"><streamhost jid="kadhirsduos@ip-10-123-60-876/smack" host="127.0.0.1" port="7777"/><streamhost jid="proxy.ip-10-123-60-876" host="10.123.60.876" port="7777"/></query></iq>
2013-12-03 10:21:57.326 iPhoneXMPP[525:3523] didRecieveIQ*****************
2013-12-03 10:21:57.329 iPhoneXMPP[525:3523] IQ type === result
2013-12-03 10:23:16.726 iPhoneXMPP[525:a0b] SOCKS5 Connection failed!
2013-12-03 10:25:07:188 iPhoneXMPP[525:3523] RECV: <iq xmlns="jabber:client" id="E5132784-1493-458B-98B0-8E3902A541C0" to="kadhirsduos@ip-10-123-60-876/smack" from="kadhirkarbonn@ip-10-123-60-876/smack" type="error"><error code="406" type="MODIFY"><not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" lang="en">Could not establish socket with any provided host</text></error></iq>
2013-12-03 10:25:07:189 iPhoneXMPP[525:a0b] iPhoneXMPPAppDelegate: xmppStream:didReceiveIQ:
2013-12-03 10:25:07.189 iPhoneXMPP[525:3523] didRecieveIQ*****************
in this i have never get called turnSocket : didSuccedd method. Can anyone tell me about what i am missing . Please help.
回答1:
Note: My XEP-0096 implementation is different from your's. Any way you created session so just get that ID It may help you:
- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq
{
//**************** Handling the file transfer task *****************//
NSLog(@"---------- Check if this IQ is for a File Transfer ----------");
NSString *myFileTransferID = YOUR_SESSION_ID_GET_FROM_XEP--0096//[xmppFileTransfer xmppFileTransferResponse:(XMPPIQ *)iq];
if ([myFileTransferID length]>0) {
// Get the file size
NSXMLElement *si = [iq elementForName:@"si"];
NSXMLElement *file = [si elementForName:@"file"];
if (file) {
//All are the attribute of file while creating section Id you need to take car for that
filesize = [file attributeUInt32ValueForName:@"size"];
received_FileOwner = [iq fromStr];
received_FileType = [si attributeStringValueForName:@"mime-type"];
received_FileName = [file attributeStringValueForName:@"name"];
}
}
if([myFileTransferID length] > 0 && [[iq type] isEqualToString:@"result"]) {
// Create bytestream socket for File Transfer via XEP-0065
TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:xmppStream toJID:[iq from] fileTransferSID:myFileTransferID];
[turnSockets addObject:turnSocket];
[turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}
else if([myFileTransferID length] > 0 && [[iq type] isEqualToString:@"set"]) {
if ([TURNSocket isNewStartTURNRequest:iq]) {
NSLog(@"TURN Connectio started:: to establish:: incoming file transfer request..");
TURNSocket *turnSocket = [[TURNSocket alloc]initWithStream:sender incomingTURNRequest:iq withSessionID:myFileTransferID];
[turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[turnSockets addObject:turnSocket];
}
}
}
TurnSocket delegates
- (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket
{
NSLog(@"Socket Suceeed Port For File Transfer: %d",socket.localPort);
DDLogInfo(@"TURN Connection succeeded!");
DDLogInfo(@"You now have a socket that you can use to send/receive data to/from the other person.");
if ([imageDataFile length]>0) {
// fileTransferData data to write
[socket fileTransferData withTimeout:200.0f tag:TAG_FILETRANSFER_STREAM];
// [turnSockets removeObject:sender];
}
else {
fileTransferData = [[NSMutableData alloc]init];
[socket readDataToLength:filesize withTimeout:290.0f tag:0];
}
}
- (void)turnSocketDidFail:(TURNSocket *)sender
{
DDLogInfo(@"TURN Connection failed!");
[turnSockets removeObject:sender];
}
- (void)readRecievedData:(NSData*)data withTurnSocket:(TURNSocket *)receiver {
[fileTransferData appendData:data];
float progress = (float)[fileTransferData length] / (float)[data length];
NSLog(@"Progresaa value is: %f",progress);
}
来源:https://stackoverflow.com/questions/20114783/filetransfer-using-xmppframework-in-ios