Java ServerSocket Close Accept() Throws Socket Closed Exception

So, I am creating a simple multi-threaded game server (generic client/server) app in Java. I ran into a problem with threading that I thought I would post about. It may help someone else save a few hours of their time.

I have a class called TCPServer which implements Runnable (used for Java threading) and serves as a socket listener for my server. It waits for connections and then adds those connections to a connection manager. Each connection starts its own thread. The thing about threads is that they are easy to make, but melt my mind when it comes to managing them correctly. I thought I had a good handle on it. I did actually, but it made me think I was crazy for a few hours none-the-less.

So in my main class I have a command that starts the TCPServer. It creates a new one and then in the constructor, I start the thread. Well, as you may know, Thread.stop() is a deprecated and shunned method. Don’t use it. I thought I was going to have to, but I forced myself to find the real reason for my issue.

Let me discuss the issue. The thread starts and a while loop checks a variable called receivingConnections. Theoretically, if this variable is set to false, it should break out of the thread and everything should be hunky dorey. I created a stop method which set this variable to false and then closed the ServerSocket.

Here’s where the fun began. When I issued the command to execute the stop method, I kept getting a “socket closed” exception. This was being caught on the accept() method of ServerSocket. I couldn’t figure out why it was throwing the exception. The socket was closed, true… but why did it keep trying to use the accept() method after the socket is closed.

I thought that it was due to the receivingConnections variable not being volatile. I changed this but still ran into the same issue.

The answer is rather crazy. You see, ServerSocket’s accept() method just waits for a connection. The while loop that it was in was just stalled waiting on a new connection. Closing the socket triggered the exception. Even though right before the accept() method was called, I was checking if the socket was closed. My guess is that the accept() method simply starts its own infinite loop and waits on the new connection. As far as I can see, there’s no way to break out of that.

The solution… set your flag variable to false. In my case it was the receivingConnections variable. Create a new client socket connection to the server and close it immediately. Then you can safely close the ServerSocket. This fools the accept() method, which forces an iteration of your while loop, which sees that your flag is now false, breaking out of the loop.

LAMP Web Development Tutorials

I recently started creating a series of online courses for LAMP web development called “LAMP Web Development from the Ground Up”. These courses are available at Udemy.com. The first one concentrates on HTML, CSS, and Javascript. It’s an introductory course. The second course is in development and will focus on Linux. If you are interested in learning how to develop web applications in a LAMP environment check it out. The first course can be found here: LAMP Web Development From the Ground Up Course 1.

Visual Studio Won’t Create a New C++ Project

I had a problem with Visual Studio after a service pack update. I was unable to create a C++ project. The project dialog would just disappear and then come right back. I’m not sure if the SP1 update caused the issue, if it was a combination of SP1 and a custom installation directory of Visual Studio, or something entirely unrelated. I searched for a fix all over the internet and came up blank. I decided to try uninstalling and reinstalling Visual Studio. However, once the uninstaller opened I decided to give the repair a try. After repairing the installation and rebooting, I can now create C++ projects again. So if you are having a similar problem, give that a shot. It worked for me. Sometimes it’s a simple fix.

GM48 #6 Submission – Ant Agonists

Last week I decided once and for all that I’m going to get into game development. I have a lot to learn, don’t get me wrong, but it’s a real fun thing to get into and I’m nearing the point where I have the skills to do it. My main problem is my art skills are lacking quite a bit at the moment. I’m concentrating on sprite creation for 2D games and modeling/texturing for 3D games. Monday of last week (April 8, 2013) I found out about Unity3D. I fell in love with it. Up until this point, I’ve been working with Java to make mini-games, as a learning experience, but I don’t really like mucking about with setting up Java. I dislike working with class paths, and I don’t particularly care for the distribution side of software that is created with Java. I may still venture into that but I want to turn out some actual games first.

Unity3D allows you to create your games in a GUI environment and it handles scripting quite well using your choice of C# and Javascript. It is actually quite nice. I was amazed that I could write some C# and the variables turned into interface options inside Unity itself. The problem for me at this point is that I don’t have great modeling skills, but I’ve started watching Blender tutorials to develop that skill. So for a couple of days I was working on Blender. Then I happened upon a post about Game Maker. I’ve always been interested in many types of games 2D as well as 3D, and I wanted to get my hands dirty with some 2D sprite making. It occurred to me that it would be a good idea to make a few 2D games before diving into 3D.

I tried out the free versions of both Unity3D and Game Maker Studio. At this point I should point out another feature of both of these products that really drives me toward utilizing them instead of hand-coding games from scratch. They both offer exporting to various platforms. With Game Maker you have to pay for the ability, but you can write your game once and export to Windows, Windows 8, Linux, Mac OSX, Android, and HTML5. With Unity3D you can export to a ton of things with the free version, including XBox360, Windows, Mac, Android, and iOS. However, there are some things you don’t get with the free version like some shaders. The cost of the non-free version of Unity3D is also $1500. For some reason I think that price point is a little high for their maximum profit. I could be wrong, but if Unity went for $200, I would have already bought it, and I’m sure there would be thousands of others who would fork over that much for it.

At any rate, after using Game Maker for a couple of hours I decided it is something I want to devote some time with, so I purchased the pro version on Thursday (April 11). The next day I found out that there was going to a 48 hour Game Maker competition on the /r/gamemaker subreddit. I entered it and after 48 hours of writing a game with a tool that I had just bought, this is what I came up with. I give you Ant Agonists.

How to Make a Game for Free

I’m completely floored by the quality of some free (as in beer) software these days. Some of it is open source, but I’m not concentrating on that in this post. I’ll be concentrating on how to save a ton of money and still create a high quality game. So if you are interested in making a game, and you don’t want to shell out a lot of money for the tools to do so, keep reading or start playing online casino games you can learn in https://funlinx.to/introduction-to-online-casino-match-bonus-offers/ everything about gambling.

Game development can be a tricky endeavor. Being able to write code and do simple math are definitely a prerequisite. However, some game developing environments and frameworks take a lot of the busy work out of the coding experience and let you focus on the game more directly. Perhaps the easiest language to start developing games with is Java. It’s also very powerful. Minecraft is a very successful Java game. There is, however, according to school playground markings company an easier way to make games, and for some reason I’m just now discovering it. It’s called Unity3D.

There are two versions of Unity3D: free and pro. The free version has all the features you need to create a game, but Pro does have its advantages. The pro version costs $1500, however, and that is not within the budget of most small independents. So, a good plan is to start with the free version, get really good with it, and create a game which sells well. From your profit you can purchase the pro version. I’ve been learning Unity3D for the last two days and I’m amazed at its features. I’ve developed small games in Java, so I’m not a terrible coder. I usually shy away from applications which try to make game development easier (for instance Dark Basic). I stay away from them for the same reasons that I don’t use a GUI interface to create web layouts, you end up being limited by the ease of use.

I was afraid that Unity3D fit into this stereotype, but I have to say that it’s a great piece of software. The GUI is smart. If you create a script for an object, the GUI automatically knows what to do with the object properties you create in the script. For instance, lets say you create a character object and add a script to control his movement. You may create a class variable like:

boolean isWalking = false;

If you save this change in your script and then go back to the Unity3D inspector you’ll see a new checkbox called Is Walking. It’s small little helper features like that which make it a joy to work with.

So, in case you didn’t catch on, my first piece of free software is Unity3D. Even if you are a game developing guru, this will make your life much easier. It also has the ability to export to Android, iOS, Windows, Linux, Mac, PS3, Xbox360, and Wii.

Unity3D doesn’t handle all your work though. You still need to create your assets in other programs or buy them from the Unity asset store. Personally, I want to make my own assets so I have full legal right to them and they aren’t just generic assets which others can have in their games as well. So, you need models, textures, and sounds. There may be other types of assets that you’ll need but these are the assets on which I want to focus.

Models can be created in Blender. Blender is comparable to high-dollar applications such as Maya and 3DSMax, except it’s completely free. I’ve never had any training on how to make models, but when a piece of software like this is available for free, I can’t help but want to dive in and learn how to use it. With Blender, you can create full length animated movies as well. Want to learn how to use it? Check out these Blender video tutorials. They are awesome and will help you master Blender in no time. Modeling is no simple task, especially if you are adding rigging and such, but it’s very rewarding. You can also create assets in Blender which can be sold on the Unity Asset Store.  So if your passion is not to make games but to make in game objects, you can profit from selling your work.

Textures are added to models to make them look like real world objects. These can be created with the Gimp, which is a free image manipulation application. Some people say that The Gimp is no where as powerful as Photoshop, but I tend to lean toward the side that says you can do anything you want with either, you just have to know your way around the tool that you are using. With the Gimp you can many things, but the two we are interested it is textures and spritesheet (in case you are making a 2D game).

Finally, you need sound effects and music. You could purchase both, but again I want to create all of my game content. So, I suggest looking at Audacity. It’s a great multi-track recording studio for audio. You could make music with it, but you’re looking at a higher investment when you have to purchase music gear. For sound effects, it is only limited by your imagination. I’ve not decided what I’m going to use for music yet. I have a decent midi controller, so I may create nice midi backing tracks using something like MultitrackStudio, which will do midi and audio. I just found it, so I’m not sure how good it is. It looks promising from the screen shots.

A Year of Double-edged Razor Usage in Review

There are a few things that I’m passionate about. I’ve been a computer nut since I was a kid. I used to look through the JCPenney and Sears Christmas catalogs for toys, and I remember seeing computers in there way back in the 80’s. After computers, I’ve always loved music. I started playing guitar in 1992, but I had a few piano lessons around 1986.  Here lately, I’ve picked up two more things to be passionate about: mechanical keyboards and double-edged safety razors.
I know, those sound like pretty strange things to be passionate about. I’m passionate about mechanical keyboards because I spend a large portion of most of my days at a keyboard, and I love the response of Cherry switches. I may have to get some dampeners for my Black Widow Ultimate. I think I prefer blue’s with rubber o-rings.

But let me get to the subject of this post. I’ve been using double-edged razors for almost a year and a half. I wanted to update my blog and describe what I’ve found in that time. This may provide helpful hints to others who are thinking about switching to a double-edge.

Razors

First of all, I’ve tried three double-edge razors: a Merkur 180, a Edwin Jagger DE89, and a 1962 Gilette Fat Boy. From this experience, I can say that it would be hard to beat the Edwin Jagger DE89. It has a great balance. Though it isn’t adjustable like the Fat Boy, it gives me the best shave. I highly recommend this razor.

Shaving Soaps

Next, I’ve learned a bit about shaving soaps. I’ve tried Williams Mug Shaving Soap, Proraso, and Taylor of Old Bond Street. I am not a fan of Proraso, though it’s probably middle of the road between the other two when it comes to lather. Williams is cheap. It’s good to have a few of these stored in case you run out of your favorite soap. They cost around one or two dollars per bar and last about four to six months of normal use. Taylor of Old Bond Street is my favorite so far. It’s more expensive than Williams and Proraso, but it lathers like crazy, and I love the sandlewood smell. Also, 5.2 oz of Taylor of Old Bond Street will only last about three or four months of normal use, if that. Whereas 1.7 oz of Williams will last longer. Like I said, keep some cheap Williams Mug around just in case you run out of your favorite. It provides a great shave, it just take more time to get a good lather with it.

Double-edge Razor Blades

Finally, I want to talk about blades. Most people would suggest that you get a variety pack of different brands of blades and try each for yourself. I agree. The different brands of blades behave very differently. I’ve used Derby, Dorco, Astra, and Feather. I started out with a five-pack of Derby blades and bought one hundred Dorco blades for around $10. I could tell right away that I preferred the Derby blades. I’ve still have over 60 of the Dorcos. I also have a supply of around 30 Feather blades, 50 Astra, and 90 Derby blades stocked in my bathroom. This will be enough blades to last me a few years. My best estimate is around five years. I have about $40 invested in that. Compare that to the price of an 8-pack of the multi-bladed razors, which would only last about a month for me.

Dorco blades seem to be thicker than the others. They are also slightly less sharp. They are a good beginner blade. Trust me on that. Feather blades are extremely sharp, almost too sharp for me. They are thin and seem to bend/flex more. This has caused me some pain in the past. I like Feather blades, but my overall favorites are the Astra and Derby blades. Of those two, I slightly favor Derby. This is mainly because I have used Derby more. I need to break out my Astra collection and test them more as a matter of fact.

My first thought when reading reviews of blades was that I wanted the sharpest blade I could get. I’m not sure if Feathers are sharper or if they are just more prone to cut me. I’d like to hear opinions from others.

What You Need

Get a good badger hair brush, a nice shaving mug, an Edwin Jagger DE89, a variety pack of blades (or 100 Derby blades), a couple bars of Williams Mug Shaving Soap, some Taylor of Old Bond Street shaving soap, and a bar of alum (for any nicks you may give yourself at first). All of that shipped will probably run around $90 – $100, and it’ll last for at least one year before you have to buy more soap and blades. After that, the annual cost will be well below $40 which, for me, was about a month’s worth of blades when I used Mach3 and Fusion razors.

PHP Singletons

It has been quite some time since my last post. However, I will be posting a few object oriented PHP tips over the next few days, especially in regard to design patterns. My first post on the subject will deal with Singletons. As a side note, don’t overuse any of these design patterns. They have their purpose and even though you may be tempted to use them in everything after you learn them, notice their benefits and work with them where they can benefit your overall system design.

Singletons are useful when you want only one instance of a class. Thus their name. You may scratch your head and wonder why you’d want to use this design pattern. There are some advantages to it. Firstly, you will reduce resource requirements for your application. It guarantees that you will only have one object taking up memory. Secondly, a singleton can be useful for simulating global variables. You can set a property of the object and access it anywhere in your application.

In a PHP application this can come in handy when you are using controllers in your own MVC framework, when you have a helper class that you only need a single instance of ever, and in any other case where only one object of a class is needed/wanted.

Here is an example of a PHP Singleton:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
  class Singleton {
    private static $_instance;
    private function __construct() {}
    public static function getInstance() {
      if(!self::$_instance) {
        self::$_instance = new self();
      }
      return self::$_instance;
    }
  }
?>

We have a private static property called $_instance. This will hold our single instance of the class. The constructor for the class is private so it can never be called from outside of the class. Thus, it can not be called directly. We call the constructor inside the public static method getInstance(). The whole purpose of this static method is to check to see if there is an instance of the class and if not, create it. It then returns that single instance. So the $_instance property of the class holds the single instance of itself. The recursion can be confusing.

In order to use the singleton, you simply call the static getInstance() method and assign the reference to a variable, or if you only need to execute a single method, you can call the method in a chain. Here is an example each:

1
$mySingleton = Singleton::getInstance();
1
Singleton::getInstance()->someMethod();

Penzu Personal Journal

I randomly happened upon a site called Penzu.com (you have to love the obscure names that come about from lack of domain availability). It is a personal journal site that basically gives you an encrypted online diary, learn what you need to know about a paystub. It also has ssl security on the connection. So you can write your daily journal online, and you won’t have to worry about anyone else reading it.

Wait, let me repeat that. You can write in your journal online daily and never have to worry about anyone else reading it. First of all, letting other people ready something you’ve written is one of the major benefits of the web. Why would you want to post anything online and then keep everyone else from reading it? I mean I can see maybe a few people using this who don’t know any better, but Penzu actually offers a “Pro” version that’s $19.00 per year. So you have a personal blog that no one will be able to read and you have to pay 19 bucks a year for the upgrade version which includes themes and other unnecessary things. Good luck to Penzu and all, but I just don’t see that business model as being sustainable, just get yourself a hobby. Let me take a moment to explain why, I recommend you look into getting a virtual assistant for your business.

First of all, private journals are possible in many different forms that don’t require an internet connection. There’s physical diaries. There’s notebooks. There’s personal computers. All three of these do not require an SSL connection to keep your information safe. If you are saving your journal on your personal computer, you may want to utilize encryption, however. A good free program for that is TrueCrypt. Though it isn’t the only one. There are many. All three of these methods for keeping a diary are much safer than posting anything online, they don’t require an internet connection, and they don’t cost a dime. So why would anyone want to post their journal this way?

I think what led to this idea is the notion that blogs are too public. People may not feel like they can speak their mind on a blog because anyone and everyone will read it. This has the potential of keeping someone from getting a job or getting fired from their current job. So maybe someone looked at this problem and thought, “What we need is a site that lets people make private journal posts”, which honestly would have been a good idea before the invention of books, pencils/pens, or personal computers. Unfortunately, today it’s just not a great idea, certainly not one worth $19 a year.

Everyone is in a race for the next big internet idea. Penzu.com is an example of a well implemented bad idea. The site is beautiful. It has wonderful design and coding. Unfortunately, the idea behind this wonderful site is terrible. If I had to guess, a rich “idea person” decided to put a lot of money into one of his brilliant ideas and out popped penzu.com. I don’t fault the developers who made the site. They were doing work to pay their bills, and they actually did an awesome job.

I do have some thoughts on how to make this idea a little better, however.

First ditch the “pro” version idea completely. Meaning get rid of a payment of any kind. The service needs to be completely free for the user. Make your money through advertisements within the interface.

Secondly, make this site more social. Think about copying Google+ circles. Allow users to make a post public, private, or restricted to certain friends or friend groups.

Penzu has to realize that their idea goes against what makes Web 2.0 great, and they aren’t doing anything groundbreaking with this idea. But enough of my ranting…

404 The entry or revision was not found in the repository. Git Repo in Redmine

If you are getting this error when trying to add a Git repository to your Redmine installation, you could be running into the same problem that had recently. The quick and easy solution for the exact issue I was running into is as follows…

The path was incorrect. I’ll give a quick example.

Let’s say the path to your Git repository is /home/user/repo, when you add it to the redmine installation use: /home/user/repo/.git as the path. It’s that simple. This is here as a simple reminder to myself of the correct way to set this up.

New Session Cookie Created on Every Page Refresh in CodeIgniter

CodeIgniter’s way of handling session data is slick, and I use it a lot. However on my current project, I went overboard on my configuration changes and accidentally caused a problem that had me scratching my head for a few minutes. I noticed that session data wasn’t persisting and that my sessions table (I opted for database storage of my session data) was filling up with new rows of session data every time I reloaded a page in my project. This prevented my login functionality from working.

The solution to my problem was a configuration detail. I had set $config[‘cookie_domain’] to the domain name I will eventually use for the site. CodeIgniter didn’t like this because my development environment is not on that domain. So it was creating new cookie/session data every time I loaded a page. The problem made sense after I thought about it for a bit. I remembered that I had set a few extra settings in the config, and sure enough, that was the winner.

The problem can happen when other settings are incorrect as well. So pay close attention to those settings, and look there first if you notice that sessions are being created on every page load.