×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    May 2013
    Location
    India
    Posts
    13

    ServerSocket for Windows Phone 8

    Hi,
    I am trying to port an Application I made for another OS in which the phone listens for HTTP requests (Web Browser generated) and responds accordingly. For that, I need something similar to "ServerSocket" (in Java , flexible enough to mould as per my requirements). Upon a quick search I found "StreamSocketListener" .. but I'm not able to fully understand how It works. And I didn't find enough tutorials for WP8 (may be, lazy on my part).
    But it would be helpful, if someone here can help me on either another class or StreamSocketListener .

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,694

    Re: ServerSocket for Windows Phone 8

    StreamSocketListener docs, http://msdn.microsoft.com/en-us/libr...tlistener.aspx has a short summary of "The typical order of operations is as follows" in its Remarks. Those steps are pretty much the same as you would use in Java, just it follows an event-based approach, so instead of a synchronous call to ServerSocket.accept, you have an event listener which you register prior to binding.
    http://babaandthepigman.wordpress.co...ample-c-metro/ seems to contain some related code fragments.

  3. #3
    Registered User
    Join Date
    May 2013
    Location
    India
    Posts
    13

    Re: ServerSocket for Windows Phone 8

    Quote Originally Posted by wizard_hu_ View Post
    StreamSocketListener docs, http://msdn.microsoft.com/en-us/libr...tlistener.aspx has a short summary of "The typical order of operations is as follows" in its Remarks. Those steps are pretty much the same as you would use in Java, just it follows an event-based approach, so instead of a synchronous call to ServerSocket.accept, you have an event listener which you register prior to binding.
    http://babaandthepigman.wordpress.co...ample-c-metro/ seems to contain some related code fragments.
    Thanks, will give it a look..

    Edit: I'm still facing the problem that I faced earlier.. May be it's because My understanding of StreamSocketListener is less as compared to ServerSocket.
    The problem is : I want to read HTTP Headers and the data that precedes the headers (where size of header strings is not known). In case of Java, I could simply read on the socket in while using bufferedreader/datainputstream until I encounter a Blank line for HTTP Headers and then continue for the data until the size of read data becomes equal to the size specified in "length" header.
    However it appears with Streamsocket that I have to send the size of the String first so that the listener can allocate buffer, which will not work as per my requirement.
    Any solutions to this would be appreciated.
    Last edited by nsbabra; 2013-08-15 at 15:21. Reason: added problem faced with tutorial

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: ServerSocket for Windows Phone 8

    Do you really have to go the socket way?

    Since it seems to be HTTP, can't you use a HttpWebRequest/HttpWebResponse pair? Or the more modern HttpClient (needs Microsoft HTTP Client Libraries)?

  5. #5
    Registered User
    Join Date
    May 2013
    Location
    India
    Posts
    13

    Re: ServerSocket for Windows Phone 8

    Quote Originally Posted by paulo.morgado View Post
    Do you really have to go the socket way?

    Since it seems to be HTTP, can't you use a HttpWebRequest/HttpWebResponse pair? Or the more modern HttpClient (needs Microsoft HTTP Client Libraries)?
    Yes, It is HTTP but the Server will be in the phone. And your provided libraries are for clients not servers. So I don't think I can use them and as far as my knowledge goes StreamSocket is the only class that provides Listening capabilities in the WP8 device.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: ServerSocket for Windows Phone 8

    Sorry, my bad.

    On the desktop/server .NET implementations there is a TcpListener and, since version 2.0, an HttpListener that would be perfect for that.

    I once implemented a simple web server using a TcpListener (.NET 1.1 times - a long time ago) but I've never worked with sockets.

    Handling was very simple. The headers section ends with 2 consecutive line breaks (don't recall if with carriage returns).

    Do you have any code we can look at? Can you post here a sample HTTP request and point out what you're trying to do with it?

  7. #7
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: ServerSocket for Windows Phone 8

    You are using the StreamSocket provided by the Listener and furthermore the InputStream. It only provides an asynchronous way to access the data which is why you have to provide a buffer to read to.

    So you would first create a Buffer of say length 1000 and tell the Stream to read into that buffer. Then after this was done you receive an Event that tells you so and you can now analyze the data. You repeat the process until you have received all the data that is relevant to you. It does not mean that you have to provide it with a buffer that is able to read all data that is going to be received in the whole request.

    I hope that helps a little in understanding the concept. Perhaps it helps you to use a DataReader as it allows you to work more along the lines you are used to from synchronous programming.

  8. #8
    Registered User
    Join Date
    May 2013
    Location
    India
    Posts
    13

    Re: ServerSocket for Windows Phone 8

    Quote Originally Posted by paulo.morgado View Post
    Sorry, my bad.

    On the desktop/server .NET implementations there is a TcpListener and, since version 2.0, an HttpListener that would be perfect for that.

    I once implemented a simple web server using a TcpListener (.NET 1.1 times - a long time ago) but I've never worked with sockets.

    Handling was very simple. The headers section ends with 2 consecutive line breaks (don't recall if with carriage returns).

    Do you have any code we can look at? Can you post here a sample HTTP request and point out what you're trying to do with it?
    Here is a Sample GET Request Generated by a Browser :

    GET / HTTP/1.1
    Host: 192.168.1.100
    Accept: */*
    Authorization: Basic XXX
    Accept-Encoding: gzip, deflate
    ---------------------



    What I want to do is : to read the actual request , in this case "/" and then read any data, if data is sent along with request. And response accordingly.
    eg.-> on "/" request I want to be able to generate a response say,

    HTTP/1.1 200 OK
    Content-Type: text/plain
    Content-Length: 11
    Date: Wed, 21 Aug 2013 01:00:59 GMT
    Connection: close
    <Blank Line>
    Hello World

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    34

    Re: ServerSocket for Windows Phone 8

    Given that and SB Dev's suggestion, what are you missing?

  10. #10
    Registered User
    Join Date
    May 2013
    Location
    India
    Posts
    13

    Re: ServerSocket for Windows Phone 8

    Thanks, I will lot into it, Once I start working on this app again. I have dropped this idea for now and focussing on learning sockets in depth.
    Thanks a lot for your help.
    Quote Originally Posted by SB Dev View Post
    You are using the StreamSocket provided by the Listener and furthermore the InputStream. It only provides an asynchronous way to access the data which is why you have to provide a buffer to read to.

    So you would first create a Buffer of say length 1000 and tell the Stream to read into that buffer. Then after this was done you receive an Event that tells you so and you can now analyze the data. You repeat the process until you have received all the data that is relevant to you. It does not mean that you have to provide it with a buffer that is able to read all data that is going to be received in the whole request.

    I hope that helps a little in understanding the concept. Perhaps it helps you to use a DataReader as it allows you to work more along the lines you are used to from synchronous programming.

  11. #11
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: ServerSocket for Windows Phone 8

    I built a small demo application that implements a HTTP server on WP8. You can download it from my SkyDrive here: -- The new Location is in post 13 -- If you simply direct your phone to the phone's IP (as displayed by the App) it will generate a HTML file that contains all contacts that you have stored on the phone. The only issue is that there aren't any in the Emulator so you either have to test it on an actual device or you can simply adapt the relevant code in "HandleRequest".

    All the relevant code for the server itself is in the HttpServer.cs file to keep the demo project small and easier to understand though I would advise to structure it in a better way if you do anything more with it. Hope it's helpful.
    Last edited by SB Dev; 2013-09-16 at 19:17.

  12. #12
    Registered User
    Join Date
    May 2013
    Location
    India
    Posts
    13

    Re: ServerSocket for Windows Phone 8

    Awesome, Exactly What I was looking for. Thanks a ton.
    I built a similar app for Android... now will start building that one for WP too..

    Hopefully Microsoft or Nokia will give 3rd party devs (us) more access to System in GDR3.
    thanks a ton...

    Quote Originally Posted by SB Dev View Post
    I built a small demo application that implements a HTTP server on WP8. You can download it from my SkyDrive here: http://sdrv.ms/1950Te1 If you simply direct your phone to the phone's IP (as displayed by the App) it will generate a HTML file that contains all contacts that you have stored on the phone. The only issue is that there aren't any in the Emulator so you either have to test it on an actual device or you can simply adapt the relevant code in "HandleRequest".

    All the relevant code for the server itself is in the HttpServer.cs file to keep the demo project small and easier to understand though I would advise to structure it in a better way if you do anything more with it. Hope it's helpful.

  13. #13
    Nokia Developer Champion
    Join Date
    Mar 2013
    Posts
    511

    Re: ServerSocket for Windows Phone 8

    I have put the sample including a description of how it does things and why into the Wiki. You can find the article here: http://developer.nokia.com/Community..._Windows_Phone

Similar Threads

  1. Replies: 2
    Last Post: 2013-04-15, 12:10
  2. Windows phone 8 app installation on phone without putting it on windows store
    By KnowledgeSeeker in forum Windows Phone General
    Replies: 2
    Last Post: 2013-03-05, 12:12
  3. Replies: 1
    Last Post: 2013-02-14, 17:47
  4. Cell phone doesn't receive ServerSocket connections
    By Lucky Luke in forum Mobile Java Networking & Messaging & Security
    Replies: 6
    Last Post: 2011-07-25, 08:48
  5. Replies: 1
    Last Post: 2002-07-16, 06:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×