my github
english | español

Playing With Sockets

Wow! It's been a long time since my last post. Well, it's been some crazy months for me in my personal/professional life so i didn't have too much time or determination to write anything. This time around i'll refer to network sockets.

When i started programming there was something i was eager to learn, and it was communication between systems/apps, mainly because i loved online videogames. I little after my previous post i started playing with the idea of an Android app that had comunication with my Pc, and that's where sockets help me. The application is still in development (obviously because my inactivity time xD), but i wanted to share the basic principle.

You must imagine the basic idea. You need a server app and a client app: the server could be remote or local and needs a port to listen to, and the client will send a connection request to that server, if it is accepted then you can send/receive data through. You can think of it the same way you connect to a game server. After you connect and move round the world or do stuff, you send data to the server that will process it, and the server will send back to you how the world is updated.

Here you can download two Java projects (server and client) for you to play around. At this time the client will send two messages to the local server, and the server will log them on screen. The server doesn't send back anything, although with some modification you can make the communication bidirectional. As usual, i'll let you do that part, but it is as simple as joining the code of those two together.

Download Sockets_Example project

Test it on your Pc. Run the Server_Example.class first, and then the Client_Example.class.

If you have any questions, suggestions or better code to share, comments are below. :)