Answered by:
is it possible to connect to server and send data using TCP socket from back ground task?

Question
-
var
tcpServerHostName = newWindows.Networking.HostName("xxxxxxxxxxx . com");
vartcpServiceName = "1351";
vartcpClientSocket = newWindows.Networking.Sockets.StreamSocket();
try{
vartcpConnStartTime = (newDate()).getTime();
tcpClientSocket.connectAsync(tcpServerHostName, tcpServiceName).done(
function(onSucess) {
console.log(
"connected to socket");
});
}
catch(ex) {
console.log(ex.description);
}
the following code snippet im using to connect server from BG task,un fortunately im failing,can any one guide me!!!
Tuesday, December 11, 2012 11:10 AM
Answers
-
So we have kind of a dilemma here - I believe that your code works fine. What's happening is that by the time that the .connectAsync method is done, the background task has finished processing and it does not show the toast. I've been trying to find a correct pattern to fix this - you ought to be able to ensure that you get something done, but it might not be the toast. I *think* that a WNS toast would work in this case but I haven't tried. I sent a question to our devs asking about a pattern that should work in this case. I'll keep you updated.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.- Marked as answer by Matt SmallMicrosoft employee, Moderator Friday, January 11, 2013 3:42 PM
Thursday, January 10, 2013 5:35 PMModerator -
Hello Matt Small, good to hear that.
yes - you are correct that BG task being finished before connectasync() function is done.
Now I tried in the case you suggested.im finishing the BGtask when only the connection is established to server.
like
tcpClientSocket.connectAsync(tcpServerHostName, tcpServiceName).done(function (onSucess) { // connection done showToaster("TCP connection Established from BG Task"); backgroundTaskInstance.succeeded = true; close(); }, function (error) { //connection failed showToaster("TCP connection error in BG Task"); backgroundTaskInstance.succeeded = true; close(); });
it is establishing TCP connection successfully.
here is the sample which works fine
https://skydrive.live.com/redir?resid=FE4B416453F604E7!124
- Edited by NagendraVivek Friday, January 11, 2013 7:06 AM sky drive link added
- Marked as answer by Matt SmallMicrosoft employee, Moderator Friday, January 11, 2013 3:42 PM
Friday, January 11, 2013 7:04 AM
All replies
-
Hi,
Please refer to the code as follow:
var serverHostName = new Windows.Networking.HostName(document.getElementById("hostNameConnect").value); var serviceName = document.getElementById("serviceNameConnect").value; socketsSample.clientSocket = new Windows.Networking.Sockets.StreamSocket(); socketsSample.displayStatus("Client: connection started."); socketsSample.clientSocket.connectAsync(serverHostName, serviceName).done(function () { socketsSample.displayStatus("Client: connection completed."); socketsSample.connected = true; }, onError);
#Quickstart: Connecting with a stream socket (Windows Store apps using JavaScript and HTML) (Windows)
http://msdn.microsoft.com/en-us/library/windows/apps/hh452996.aspx
#StreamSocket sample
http://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, December 12, 2012 2:03 AM -
Hi Song tian, Thanks for your reply.. but I could not see any change in your code snippet from mine.
even I tried with your code too, but still being same(connection not success).im using this tcp connection in Background task, means tcp connection should established when the background event triggered.
can you check once
thanks in advance.
- Edited by NagendraVivek Wednesday, December 12, 2012 6:52 AM
Wednesday, December 12, 2012 6:51 AM -
Hi,
I want to know whether it is running correct without background event triggered mode. And do you test whether background event triggered? You may also set a breakpoint, and debug first.
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, December 12, 2012 8:52 AM -
hi Song Tian,
yes it is working fine without background task and then can to connect to my server too using this socket.
I tested my background task and it is triggering finely, I have debugged the background task with the help of breakpoints. the flow is executing successfully up to
socketsSample.clientSocket.connectAsync(serverHostName, serviceName).done(function () {
but the "connectAsync" function not completing. even it didn't fire any error
Wednesday, December 12, 2012 9:24 AM -
Hi,
Could you share your complete code? Please upload that to skydrive.
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, December 12, 2012 9:49 AM -
hi ,Sure
here is the link
https://skydrive.live.com/redir?resid=FE4B416453F604E7!191
that sample will trigger the background event when the internet came available, and it will show a toaster message if the TCP connection is succeeded.
Wednesday, December 12, 2012 10:33 AM -
Hi,
I will involve more experts to investigate it.
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Wednesday, December 12, 2012 11:17 AM -
HI thanks, will wait for your feedback.Wednesday, December 12, 2012 11:28 AM
-
Hi Song Tian,
any news from the experts?
-cheers
Friday, December 14, 2012 6:06 AM -
Hi,
They will reply here directly. Thank you.
Roy
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.Friday, December 14, 2012 6:08 AM -
I've tried reproducing this problem and cannot seem to get anything to happen except that I see a bunch of toast messages. Can you please describe exactly what I should do to reproduce this problem?
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Monday, December 31, 2012 6:41 PMModerator -
hi Matt small,
thanx for the review , you just try to establish a TCP connection from client machine to server. i just need to establish a TCP connection to server from background task or on a back ground task event fire..
Monday, December 31, 2012 7:55 PM -
I ran your app and it seems to work. Is there something I'm missing here?
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Wednesday, January 2, 2013 4:43 PMModerator -
hi @Matt Small
you mean the connection establishing to that server on that port?
even I tried again now, still it is not establish the connection. The connectasync() function did not completing.
Thursday, January 3, 2013 4:10 AM -
Have you tried from other machines not inside your network? Nobody else seems to be able to reproduce your problem.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Thursday, January 3, 2013 2:30 PMModerator -
Hi Matt Small,
I have tried with other network connections too, even after you latest suggestion I tested on mobile internet connection, but TCP connection Not establishing :(
please have look on this sample from SkyDrive
https://skydrive.live.com/redir?resid=FE4B416453F604E7!192
it should show a toaster soon after tcp connection is established ,and the connection will start when the internet available event
Friday, January 4, 2013 5:29 AM -
Before I do any other work, please explain the following to me:
When I run your program, what would be the desired outcome? Please explain exactly the toasts that I am seeing, what they represent, etc.
When I run your program, what is the actual outcome? Please explain exactly the toasts that I am seeing, what they represent, etc.You may want to consider opening a support case for this problem if we are not able to come to any other conclusion via the forums. You get two support cases with your Store account.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Monday, January 7, 2013 3:49 PMModerator -
Hi Matt Small,
please take latest sample from here(the modified one)
https://skydrive.live.com/redir?resid=FE4B416453F604E7!193
it will show a toast only when the internet came available and then if a TCP connection established successfully(from bg task).
the toaster text will be as "TCP connection Established".no other toasters are there now.
please try it
- Edited by NagendraVivek Tuesday, January 8, 2013 5:20 AM skydrive link modified
Tuesday, January 8, 2013 4:36 AM -
Please answer the questions that I posed above.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Wednesday, January 9, 2013 2:47 PMModerator -
in the previous sample you can see the bunch of toasts which are represent the completed event of background task. It just showing the completed event of background task ,not on the tcp connection completion. it will show another toast when tcp connection established. But that toast is not showing as it is failed to complete the connection to tcp server.
in the second sample I gave, you just will see a toast if tcp connection is completed successfully to server.no other toasts are kept there which makes you confused.
Thursday, January 10, 2013 11:30 AM -
So we have kind of a dilemma here - I believe that your code works fine. What's happening is that by the time that the .connectAsync method is done, the background task has finished processing and it does not show the toast. I've been trying to find a correct pattern to fix this - you ought to be able to ensure that you get something done, but it might not be the toast. I *think* that a WNS toast would work in this case but I haven't tried. I sent a question to our devs asking about a pattern that should work in this case. I'll keep you updated.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.- Marked as answer by Matt SmallMicrosoft employee, Moderator Friday, January 11, 2013 3:42 PM
Thursday, January 10, 2013 5:35 PMModerator -
Hello Matt Small, good to hear that.
yes - you are correct that BG task being finished before connectasync() function is done.
Now I tried in the case you suggested.im finishing the BGtask when only the connection is established to server.
like
tcpClientSocket.connectAsync(tcpServerHostName, tcpServiceName).done(function (onSucess) { // connection done showToaster("TCP connection Established from BG Task"); backgroundTaskInstance.succeeded = true; close(); }, function (error) { //connection failed showToaster("TCP connection error in BG Task"); backgroundTaskInstance.succeeded = true; close(); });
it is establishing TCP connection successfully.
here is the sample which works fine
https://skydrive.live.com/redir?resid=FE4B416453F604E7!124
- Edited by NagendraVivek Friday, January 11, 2013 7:06 AM sky drive link added
- Marked as answer by Matt SmallMicrosoft employee, Moderator Friday, January 11, 2013 3:42 PM
Friday, January 11, 2013 7:04 AM -
After establishing TCP connection successfully from BG task,
my another question is why the BG task triggering more than one time with internetAvailable condition? by this reason it is establishing TCP connection more than one time from BG task and showing bunch of toasters.
- Edited by NagendraVivek Friday, January 11, 2013 7:13 AM more detailed
Friday, January 11, 2013 7:12 AM -
I did exactly the same thing in my code as your successful code.
I was wondering why there were multiple tasks as well... I think it's because there are multiple system events firing. Does it pose a problem for you?Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Friday, January 11, 2013 2:50 PMModerator -
yes Matt Small,
if the trigger firing more times then my code which has to execute in BG task is also executing multiple times.which was not need for me,any solution for tringgering only once like other triggers?
Saturday, January 12, 2013 7:17 AM -
I recommend that you just work around this.
Matt Small - Microsoft Escalation Engineer - Forum Moderator
If my reply answers your question, please mark this post as answered.
NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.Tuesday, January 15, 2013 4:05 PMModerator