how to use java sockets to connect to an online server - java

I have spent some time learning Java and in doing so have built myself a rather complex game. My game is a desktop ran swing app which after play results in a score value that gets entered onto a leaderboard. The game is pretty much complete, except a few security mechanisms.
At first I never intended to have my game connected to the net as the phrase 'networking' has always put me off. Now that my game has slowly developed, it has actually turned out far better than I had imagined which has kinda twisted my arm into wanting to learn how to network.
After lots of research into the subject, I decided to have a look into sockets and build myself a simple client/socket server. Both programs connected on my local machine which has bought me here.
I already know that allowing the client to send the score to the server is always vulnerable to cheats, but originally this game was never intended for online score posting. Instead I have decided to make my game code so damn unreadable and messy that it may just be easier to cheat/crack someone elses program. (wishful thinking).
This game is only a learning curve, so abuse of the score system in my eyes will only serve to teach.
My questions may seem silly, but its only trying to get a fuller understanding.
I'm wanting to use a socket to send a number, lets say 2371 from my desktop game to a java server that is hosted on a random webserver. I then want the java server to find the position of this players score (2371) and then print the score on a website leaderboard.
Is it really as simple as adjusting the code URL and then uploading the socketServer code to webhosting space?
If it is, what am I uploading..... a .java file, or any other?
I have also read many times in my research that alot of webhosts dont allow the uploading of java files with open ports. Is this correct?
Many thanks guys, this is a subject that I have always steered clear of, so all and any advice would be appreciated.

What you need is a computer on the internet that can run a process (your Java server application), that your Java client applications can connect to. So you would upload the file(s) (maybe it's a jar, or some class files), and tell the server through whatever interface you have to run the process. This server application can then hold everyone's scores and save them to disk and whatever else you need.
I have also read many times in my research that alot of webhosts dont allow the uploading of java files with open ports. Is this correct?
This is correct. You'll need to rent a server (if you don't have one yourself that you can put on your own internet connection), a simple web hosting companies will not allow you to host your own running process.
Instead I have decided to make my game code so damn unreadable and messy that it may just be easier to cheat/crack someone elses program. (wishful thinking).
I'm not sure if this a joke but there's source code obfuscators out there like ProGuard which do exactly this.
If you really want to protect against people simply writing scores out to the server with their own modified client then you'll have to have some of the logic of the game be handled by the server process itself. Of course someone could still go further and create a bot or whatever else they need to cheat and get their bogus score listed.

Related

Is there a way to create keyboard macros through an android app?

First of all, I am extremely new to coding and I just learned the basics of java. I want to do an android app as my first project. The idea of the app would be to have buttons on your phone which would work as keyboard macros. I was wondering if I also needed to create specific windows drivers for it or if I could just make it run on the default windows keyboard drivers? As I said I am super new to all of this (about 10hrs of programming experience), so feel free to correct me and educate me as much as you want! I won't take it personally, I'm looking to learn :)
You would need to set up some sort of communication between the app and your PC. You wouldn't need to do anything with the keyboard drivers. Java has something called the "Robot class" which allows you to simulate a keypress.
Here is the documentation on the robot class: https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
As for the communications, you will need to create a server/client connection. One of your devices will act as the client (probably your phone) and one will act as the server (probably your PC).
This is just a rough idea of how it would work but:
When you tap the button on your phone, it would send some specified data to your server that is running on your PC. You should set the data that gets sent from the client to the server as the key(s) keycode that you would like it to simulate so it will be easier to implement. When the server gets data from the client, it should send that data to the robot.keypress(data) function.
This honestly sounds like it will be a big project for your skill level but I wish you the best of luck on this. This will probably be a frustrating experience but don't let it get the better of you.

AVR Microcontroller temp sensor communication over LAN

Hello infinitely more skilled people than me ;-),
I have a question for my Brothers School project and I want to help him.
He has a micro-controller board with a LAN port and wants to attach a temperature Reader to it.
Has anyone a Suggestion on How to communicate this data let's say for example to an Android app that can Display the temperature graph or just the temp at the current Moment?
If not Android he wants to Display it via a Java app on a desktop that displays a temp graph and average temps.
I tried to Google it but I haven't found anything that describes this case. Maybe I don't know how to formulate it.
I know it super vague to describe it but maybe some nice guy can send me code example how it would look like, since I am a total beginner.
Here is the AVR board (it has a LAN port)
http://www.pollin.de/shop/dt/MTQ5OTgxOTk-/Bausaetze_Module/Bausaetze/Bausatz_AVR_NET_IO.html
Thank you :)
Thanks and have a nice day :)
As far as I can see, the board features an ATmega32 controller and an ENC28J60 Ethernet controller (with SPI!).
It seems they provide some sort of server application that communicates with the board (via Ethernet/LAN). The app is not trivial, as I see it also has firmware update feature (so the AVR chip has also a bootloader in its flash).
Anyway, an interesting approach would be to connect your temperature sensor on one analog input, then start the board and the net-server app (connected to LAN). You'll see the related measurements on some ADCx (in Volts).
After that, using Wire-Shark or similar tool, you could spy the communication messages between the board and the server (knowing their IPs, to filter out all other crap lurking on the LAN).
Next step would be to find out the specific message server sends to request the ADCx, and also the related answer from the board. Not trivial, yet doable I guess :)
Finally, knowing what messages are exchanged (I here assume you don't have the specs for the protocol or the source code), you should replicate/implement something similar in your Windows/Android application (further conversion from voltage to °C temperature to be displayed is yet another thing to be done by your app - the easiest part, I suppose :).
With the data you provided, I think this is the best suggestion I can make.
Best luck with your project!
BTW: Best place where you can find help (and source code examples, compilers and related stuff) for AVR is: www.avrfreaks.net
Did you see the software and firmware that comes with the board (download...)?
It pretty much has everything you'd need, i.e. TCP/IP stack, simple command/response server to access digital and analog IOs &c.
See the documentation: https://www.pollin.de/shop/downloads/D810058B.PDF, esp. "Protokollaufbau und Kommandos AVR-NET-IO", e.g. "GETADC 3 (ruft den akt. Wert des ADC-Eingangs 3 ab)"
Hence, you should be able to use the stock firmware and talk to it from your app/whatever via simple commands over TCP. You can query the boards input states and do any calculations on that in the app before displaying the result.
(Edit: You did not state what kind of sensor he wants to use. If the sensor's output is not an analog voltage but some digital protocol, the stock firmware in fact won't get you far.)
The boards firmware is written in C (closed source, I believe), but as I said, you'd probably not have to bother with that. However, if you want, there are alternative firmware versions available as open source from the 'net; and if you're inclined to a greater experience, you can also create your own, possibly building on an available TCP/IP stack and ENC28J60 libraries (readily available too). You could also add a simple HTTP server on top of that &c, &c.
Some links that instantly turned up when I googled for "avr net io firmware":
http://www.fhemwiki.de/wiki/AVR-NET-IO
http://engelhuber.de/blog/2013/10/29/pollin-avr-net-io-mit-e2000-firmware-ueber-php-ansteuern/
http://netio.davideickhoff.de/de/
http://son.ffdf-clan.de/

Building a music player for both android and Desktop at the same time

I'm going to build a music player working on both Android and Desktops. It won't be anything special, I'm doing it more to training myself and know more or less what problems I might encounter if I want to do a real app/program one day. Therefore, since I'm already rather decent at web technologies, I'll try to use something else: Java.
My app / program with have to
be able to read music files and play them (I'm planning on reading the files myself, meaning that I only need to be able to read "raw" sound, WAV or such)
be able to write to music files (to change tags)
be able to communicate with another instance of the program on another device that's on the same network (I want to be able to use my phone as a remote control and my pc as a remote control for my phone)
If possible, show some play/pause buttons on the screen even if it's locked (probably just on android)
And this is where I need your help: What you I do to write as little "device specific" code as possible?
It's obvious I can reuse classes used to encode/decode some music types. Finding the files, reading them, writing them, playing raw sound and connection to the network will be easy to abstract if needed.
But then there is the UI and it looks like if I don't plan carefully, I'll have to do it twice... I've seen libGDX but they kinda insist a lot on the fact it's for games...
All I need is some way to build a simple UI (a few buttons, the cover of the albums) that'd work for both the desktop and the phone.
Should I use libGDX, the "normal" libs (*WT, Swing, neither of which seem to be "compatible" with Android) or something else?
I'd also like to request as few permissions as possible. Meaning that I'd like to have a base music player that only request access to the sd card, and then features requiring additional permissions would be added as other apps/programs or addons.
From what I understood, the only way to achieve this is to create a second app and make the user install it. I think I'll manage to make the two apps communicate (with Intent?) but is it really the only solution?
Thank you in advance for your answers.
Maybe you could consider building the app with something such as Phonegap: http://phonegap.com/ This would let you use your web technologies strength and write a very slim layer of device specific code if any at all!
As for getting a phonegap app to run on the desktop....you could use something like :http://ripple.incubator.apache.org/ to have it run on the desktop. I know this is slightly different and you wanted to tackle writing something in Java - however this is the way mobile development is moving so you may want to get started like this!

How to go about setting up a Java server and database for a multiplayer Android application

I want to make a multiplayer Android game and am an absolute novice at anything and everything networking. I have some knowledge of socket programming in C and Java, however from what I understand, it is not that simple when communicating from an Android device to a remote server. Basically, I am just unsure of how to go about setting up server-side components of this project. I've been told to look into frameworks, but am not really sure exactly how they would be used in my particular case.
Here is a breakdown of how my application works and how I want it to communicate to the server. The client application will send a request to the server whenever a user wants to login, create game, make a move, check statistics, etc. The server will update or retrieve information from a database(somehow) and send it to the client. If the player is to make a move, the server is responsible for doing all calculations, storing the gamestate, and pushing the updated game to both players.
I was hoping somebody could shed some light on how I would go about starting this. Any information would be greatly appreciated. Thanks
Essentially you want to set up a Socket Server program listening on your server for connections from the client. You can achieve this through any number of ways, but it seems like you are going to want to tap into a database (such as MySQL) running server side in order to handle authentication of users logging in, etc. Since you say you have familiarity with Java already, and Android is Java based, I would suggest you go with Java for the server application. A few things to look into to get started would be:
Socket Programming in Java:
http://docs.oracle.com/javase/tutorial/networking/sockets/index.html
Chat Client/Server Examples in Java here:
http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html
Also look into Java JDBC tutorials on Oracle's website (for interfacing Java with a MySQL server), and you will likely find that Android AsyncTask class will come in handy when you implement your Android client. It might be better to start with a pure Java client to get the server aspect working first and then move to an Android-based client. Once you establish some code you can make more refined posts here and gather more specific answers. Hope that helps and good luck.

Send commands between two computers over the internet

I wish to control my computer (and usb devices attached to the computer) at home with any computer that is connected to the internet. The computer at home must have a program installed that receives commands from any other computer that is connected to the internet. I thought it would be best if I do this with a web interface as it would not be necessary to install software on that computer. For obvious reasons it would require log in details.
Extra details: The main part of the project is actually a device that I will develop that connects to the computer's usb port. Sorry if it was a bit vague in my original question. This device will perform simple functions such as turning lights on etc. At first I will just attempt to switch the lights remotely using the internet. Later on I will add commands that can control certain aspects of the computer such as the music player. I think doing a full remote desktop connection to control my device is therefore not quite necessary. Does anybody know of any open source projects that can perform these functions?
So basically the problem is sending encrypted commands from a web interface to my computer at home. What would be the best method to achieve this and what programming languages should I use? I know Java, Python and C quite well, but have very little experience with web applications, such as Javascript and PHP.
I have looked at web chat examples as it is sort of similar concept to what I wish to achieve, except the text can be replaced with commands. Is this a viable solution or are there better alternatives?
Thank you
VNC
SSH
Remote Desktop (Windows)
You can write a WEB APPLICATION. The encryption part is solved by simple HTTPS usage. On the server side (your home computer with USB devices attached to it) you should use Python (since you're quite experienced with it) and a Python Web Framework you want (I.E. Django).
While it is an interesting programming question, perhaps you should ask it on ServerFault instead? There you can probably get a lot of nice input on web-based administration / control tools.
Unless this is a "for fun" project for you, there are about a jillion things out there that already do this. And if you want to control a computer from a web browser, be prepared to require installing some kind of custom plugin - since browsers can't touch arbitrary files on the local filesystem, execute local applications, or do other things that are flagrant security risks.
I've been using VNC for over a decade - free and easy.
http://en.wikipedia.org/wiki/Virtual_Network_Computing
Well, I think that java can work well, in fact you have to deal with system calls to manage usb devices and things like that (and as far as I know, PHP is not the best language to do this). Also shouldn't be so hard to create a basic server/client program, just use good encryption mechanism to not show commands around web.
I you are looking for solution you could use from any computer anywhere in the worls without the need to install any software on client pc, try logmein.com (http://secure.logmein.com).
It is free, reliable, works in any modern browser, you don't have to remmeber IPs and hope they won't change, ...
Or if this is a "for fun project" why not write a php script, open port 80 in your router so you can access you script from outside, possibly dynamically link some domain to your IP (http://www.dyndns.com/). In the script you would just login and then for example type the orders in textfield in some form in your script. Lets just say you want to do some command prompt stuf, so you will basically remotely construst a *.bat file for example. Then the script stores this a fromtheinternets.bat to a folder on your desktop that is being constantly monitored for changes. And when such a change is found you just activate the bat file.
Insecure? Yes (It could be made secureER)
Fun to write? Definitely
PS: I am new here, hope it's not "illegal" to post link to actual services, instead of wiki lists. This is by no means and advertisement, I am just a happy user. :)

Categories

Resources