Archive for category Programming

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.

, ,

2 Comments

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.

, ,

No Comments

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.

No Comments

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();

No Comments

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.

1 Comment

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.

, ,

1 Comment

Clickbank Analytic Software

There’s a site called cb-analytics.com which has always been a great resource for information on Clickbank products. However, I’ve always found the site hard to navigate and I wanted a site that showed some of the “hot” clickbank products. So, I’ve written a site called cbniches.com which I hope will rectify these issues.

The site shows all the latest products in each category and shows gravity and earnings per sale. It also has a graph for each product to show gravity over time. I think this will be pretty helpful to affiliate markets looking for new products to promote on Clickbank. Check it out at http://cbniches.com. I wrote it with the latest version of my LavaPHP framework, another product I’ve been developing as open source. LavaPHP can be found on github, but it’s still in early development, look at how to give good customer interactions.

, , , ,

No Comments

Gnome 3 + Netbeans Revisited

I wrote at some point in the past about a small bug in Gnome 3 where Netbeans menus behaved strangely. My solution then was to switch to Gnome classic. Well I started using Cinnamon recently and found that it has the same issue. So I wanted to find a better solution.

I came across a strange way to fix the issue. I tried it out, and it worked beautifully, even if it is weird.

Unmaximize Netbeans so that it is in a Window on the desktop. Grab the top left corner of the Window and move it all the way to the top left side of the screen. Now, maximize it. The menus work again.

It’s weird, but I’m glad it works. I spend most of my time in Netbeans, and I’m really digging Cinnamon.

, ,

No Comments

Introducing LavaPHP – Yet Another PHP Framework

I’ve used quite a few PHP framework to varying degrees, and like many other PHP developers, I’ve decided to make my own. Of all the current frameworks available, I prefer CodeIgniter, because it is easy to use, has great documentation, and generally stays out of your way.

Everyone has their opinion of the best PHP framework, but I like the ones that let me write PHP and don’t throw a lot of features that I don’t need into the mix. I found that no matter what PHP framework I was using, I was always creating a table for users. I was always creating login functionality for users. I was always creating email confirmation functionality for users. I was always creating an admin interface for working with my configuration. I was always creating classes that helped me work with web services/REST APIs. I always need a small web service of my own for Ajax functionality. I always needed to add curl functionality just in case the hosting provider had fopen disabled (which most do). A Managed Cloud VPS would be great for such purposes

Those were the things I needed. ORMs are great and all, but I really didn’t want to learn proper YAML syntax just so I could setup automatic object models for my database tables. Creating models for my database isn’t that much of a chore. Creating a complete user system can be.

So, I set out to create a framework that I can use for my own projects and have all the functionality that I find I usually need right out of the box.

Another thing about frameworks is that they are designed to make enterprise level sites. They aren’t designed to create software system which can be distributed. By that, I mean I wanted to create a software package that could be installed by end users and used by them to create their own websites (custom CMS system with a specific purpose). A normal framework doesn’t work well in this area because of the way views are usually handled. Mainly, I wanted third parties to be able to create themes for my CMS systems without much effort. With something like CodeIgniter, I could use a templating engine via a plugin or Codeigniter’s own minimalist template engine, but I don’t like take one piece of software and adding on a bunch of plugins.

First you have to learn how to use the plugin. Then you have to hope that there isn’t a bug in the plugin that will spring up in your app. Then if there is some small customization that needs to be made to the plugin, you could spend days trying to figure out a way to make it work with your system, when it would have taken less time to just write your own. Using plugins also feels a bit like cheating to me, as well. I want to know every little part of my system, so that if a bug comes up, I’ll know right where to look or at least have a decent idea where to look.

With all that said, I’m announcing my PHP framework. I’ve written it completely from scratch and I’m hosting it on Github. I also have purchased the dot com for it. I’m calling it LavaPHP. The motto will be “LavaPHP – Add a little lava to your LAMP” and it will have a lava lamp as it’s mascot/logo. If you’d like to help with the initial development, hit me up and fork the project here: https://github.com/lpcustom/LavaPHP

, ,

No Comments

lwjgl.dll: Can’t load IA 32-bit .dll on a AMD 64-bit platform

If you encounter this error while trying to write a game on a Windows 64 bit system using Slick2d, I can give you some insight into how to fix the issue. I’d ran into this issue some time ago, but I don’t remember if I posted about it or not. I solved the problem then by using a recompiled version of slick2d. It worked well, but I did something different this time around.

I downloaded the latest version of LWJGL and used it instead. The process of importing the libs is much the same except you use the new LWJGL and its natives instead of the ones included with slick2d. If you need help getting the imports correct, leave a comment and I’ll go into it in more detail.

, ,

1 Comment