File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes App to server connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "App to server connection" Watch "App to server connection" New topic
Author

App to server connection

Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

Hey,

Thanks for all the help in advance

I want to write an app that will connect to my self made Java server,

I was wondering what type of connection should it be? Is it a ServerSocket or a DatagramSocket? I assume a datagram socket to obtain the connection receive the xml and the get the details from the user (which will have been passed) and fire back an xml reply?!

Is this right?

Also can one handle multiple socket connections or it is one port one connection?

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Matthew Tilic wrote:I was wondering what type of connection should it be? Is it a ServerSocket or a DatagramSocket? I assume a datagram socket to obtain the connection receive the xml and the get the details from the user (which will have been passed) and fire back an xml reply?!

DatagramSocket should never be used if you need a reliable connection that preserves the order of the data you sent. DatagramSocket uses UDP, and UDP does not guarantee that packets a) arrive in the order they were sent, or b) arrive at all.
Using a ServerSocket in your server and a Socket in your client should be just fine.

Also can one handle multiple socket connections or it is one port one connection?

If you use a ServerSocket you can have multiple concurrent connections. Read this for more information.
Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

That seems good. I know about the server socket and I know it is the preferred choice but was wondering with a mobile phone app if this is still the optimal choice?

Or is their another way?
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

I've had pretty good results using a web server and a java client to pass stuff back and forth. Security with (ptional) https and login cookies that live for a session.

There's good support in Java for the client side and the server side is very clean with all the networking particulars handled by someone else.

Of course if there's no webserver on the server side this can be overkill for a simple app.

Joe


It's not what your program can do, it's what your users do with the program.
Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

I am looking at the other way around. I want to build a Java server and then the client will be an Android app.

I was wondering if the ServerSocket to the Socket is the ideal connection (in case of dropped connection via lose of signal etc) type for an app that sends XML to a server and then receives an XML back to the client.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Matthew Tilic wrote:I am looking at the other way around. I want to build a Java server and then the client will be an Android app.

I was wondering if the ServerSocket to the Socket is the ideal connection (in case of dropped connection via lose of signal etc) type for an app that sends XML to a server and then receives an XML back to the client.

Matthew,

Ideal is a concept I'm not going to touch. It depends way to much on what YOU consider important.

A ServerSocket will work pretty well but it is fairly low level. Advantage is complete control, disadvantage is you have to completely control it so it's a lot of work. I would certainly choose it over a DatagramSocket for the reasons Rob mentioned.

I'm not defending the web server idea, it's up to you how you want to implement the data transfer but I do want to explain a bit further.

If the goal is to get xml files back and forth between mobile apps the use of a home brewed server or a web server is just an implementation detail. From the user perspective it's invisible. From the programmer's perspective it's just a different set of network protocols, the data encoding/decoding is the same, and once the data is transferred neither the rest of the program nor the end user have to know let alone care which one was used.

On a separate topic, you didn't ask about, I think many mobile apps would be better if they were implemented solely as mobile web pages or thin[er] apps. Unless they keep local data and are able run without a network connection implementing them on the server with a browser interface makes them portable across devices, more secure, easier to update, and usually better debugged. But that is really a discussion for a different thread.

Joe

Joe
Matthew Tilic
Ranch Hand

Joined: Apr 07, 2011
Posts: 41

Joe that is the response I was looking for. Just perfect.

I was thinking of making the app as mobile web page, however, I believe that an app looks a lot better when developing for that device rather than as a link to the interwebz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: App to server connection