n00b up for criticism or compliments

Yeah, im a complete n00b when it comes to networking and stuff, so im not really afraid to post my entire source code. I just finished programming my first server, it doesnt do much besides allow users to connect and disconnect from the server, and sends the number of clients to each client while thier connected. It doesnt work on the internet as of yet because i have some firewall issues, but It does work on my network so if you guys could give me comments or suggestions or info as to what i should learn after this, any help would be appreciated. Im not saying its good, im just saying it WORKS lol. and i guess for me right now thats a good thing.

[td][//-------------------------------------------
//Game Server (In Development)
//Programmed by: Britton Kjenner
//-------------------------------------------

import java.util.;
import java.io.
;
import java.net.*;

public class TrivialApplication
{
//Variables for Sockets
public static int [] inDat;
public static ArrayList sox;
public static ArrayList is;
public static ArrayList os;
public static ServerSocket echoServer;
//
public static Random gen;

public static void main(String args[])
{
	gen = new Random();
	inDat = new int[50];
	sox = new ArrayList();
	is = new ArrayList();
	os = new ArrayList();
	try
	{
		System.out.println(InetAddress.getByName("steve"));
		System.out.println("Creating Server Socket.....");
		
		echoServer = new ServerSocket(9999,50,InetAddress.getLocalHost());
		
		System.out.println(echoServer.toString());
		echoServer.setSoTimeout(20);
		
		//Constant Server Proccess
		//1.Check for new Users//
		//2.Recieve Data from Users//
		//3.Process the Data//
		//4.Return the new Game State//
		while (true)
		{
			checkNewUsers();
			if (sox.size()>0)
			{
				getData();
				processData();
				sendData();
			}
		}
	}
	catch (IOException e)
	{
		System.out.println(e);
	}
}

//Check ServerSocket for any incoming Transmissions
public static void checkNewUsers()
{
	Socket NewClient = null;
	try
	{
		NewClient = echoServer.accept();
		sox.add(NewClient);
		is.add(new BufferedReader(new InputStreamReader(NewClient.getInputStream())));
		os.add(new DataOutputStream(NewClient.getOutputStream()));
		System.out.println("New User Added");
				
	}
	catch (IOException e)
	{
	}
}

//Gets any sent data from the client program and puts it into an array
public static void getData()
{
	int response;
	BufferedReader temp;
	try
	{
		for (int lcv=0;lcv<is.size();lcv++)
		{
			temp = (BufferedReader)is.get(lcv);
			if (temp.ready())
			{
				response = temp.read();
			}
			else
			{
				//No Data Was Sent
				response = 0;
			}
			inDat[lcv] = response;
		}
		
	}
	catch (IOException e)
	{
	}
}

//process data gathered by the getData Method, and reset all values to 0
public static void processData()
{
	int check;
	for (int lcv=0;lcv<is.size();lcv++)
	{
		check = inDat[lcv];
		if (check==1)
		{
			//1=Close User Socket
			closeSock(lcv);
		}
		inDat[lcv]=0;
	}
}

//Close Socket,&IO sreams/ Remove from ArrayList
public static void closeSock(int spot)
{
	try
	{
		((BufferedReader)is.get(spot)).close();
		((DataOutputStream)os.get(spot)).close();
		((Socket)sox.get(spot)).close();
		is.remove(spot);
		os.remove(spot);
		sox.remove(spot);
		System.out.println("A player has left.");
	}
	catch (IOException e)
	{
	}
}

//Send Data to Users
public static void sendData()
{
	try
	{
		int numPlayers;
		numPlayers = getNumPlayers();
		DataOutputStream temp;
		for (int lcv=0; lcv< os.size();lcv++)
		{
			temp = (DataOutputStream)os.get(lcv);
			temp.writeInt(numPlayers);
		}
	}
	catch (IOException e)
	{
		System.out.println(e);
	}
}

//Method to return the number of Players
public static int getNumPlayers()
{
	int retNum;
	retNum = sox.size();
	return retNum;
}

}/td]

shorting variable and method names doesn’t realy allows for well readable(and maintainable?) code.

did you check out the java tourial on this? ServerSocket etc?

few notes:

   public static int getNumPlayers()
   {
      int retNum;
      retNum = sox.size();
      return retNum;
   }
   public static int getNumPlayers()
   {
      return sox.size();
   }
 public static void processData()
   {
      int check;
      for (int lcv=0;lcv<is.size();lcv++)
      {
         check = inDat[lcv];
         if (check==1)
         {
            //1=Close User Socket
            closeSock(lcv);
         }
         inDat[lcv]=0;
      }
   }
 public static void processData()
   {
      for (int lcv=0;lcv<is.size();lcv++)
      {
         if (inDat[lcv]==1)
         {
            //1=Close User Socket
            closeSock(lcv);
         }
         inDat[lcv]=0;
      }
   }

perhaps you had use for it before, I only have this perspective, sorry.

you perhaps want to create a separate class or instance this one and move away from the static bits. you also might wanna check if you wouldn’t want to do something wenn an exception is thrown. I would advise to follow java coding convention wenn it comes to documentating, avoiding inline comments and make squence diagrams… (ok ok perhaps overkill. xD but if you make it a habit of it doesn’t take that much time and your coworkers will love you. I’ll shut up now)

wenn you feel confident have a look at the NIO java.nio.* package.
also have a look at threads vs appearant concurrency.
and blocking vs non-blocking.

I read plenty of tutorials on ServerSockets and such, but none of them really told me much, i made this basically out of what little i did get from it.

But I have noticed that when more than one user connects, it has horrible speed when it comes to sending the data back to the clients, I think its because there ends up being an immense amount of code inside of my while loop after two users… does anybody know of a way I could fix this? ??? (I know, complete n00b question… but how else am i going to learn?) Im not exactly familiar with using threads… could that help me in any way? and if so how???.. oh god do i need help!!! lol.

-me-
(I guess ive earned that n00b rank by now)

you sure you read though this one: http://java.sun.com/docs/books/tutorial/networking/sockets/index.html

actually it’s probebly sitting idle most of the time cause accept() ‘blocks’ meaning it can only return an execption or an connected socket, it can’t return a value that indicates that no socket/client has connected yet. If you go about it the other way around, as most ppl do, a methode that blocks doesn’t return until a client has connected. while this makes it easier to grasp what blocking is, the other way helps wenn you start to study non-blocking methodes.

also it’s better to bind the 3 ArrayLists togetter; make one class that holds the (client)Socket, the (client’s)input and the (client’s)output stream.(what would be the name for that class? :-*) It not only creates clearer and less error prone code, it also more efficient wenn it comes to looking up the elements.

Yeah, i understood that the accept() method blocks, which i thought adding the echoServer.setSoTimeout(20); line, I know that line puts a time limit on how long accept can block. and i only set it to 20 milliseconds… so i doubt that would be true that my program is idleing right???

But I have accually thought about the whole arraylist of a class containing my variables thing, I do agree that it would make things simpler, but still, that doesnt solve my speed issue. Because the program just bogs down right after a second user has connected, and this is no good because im going to want at least 50 people on the same server… Please someone help me!!! until then i guess ill look through that tutorial you showed me.

oy… i just realized… i already looked @ this tutorial lol

I’ve looked though the code a few times and I don’t really see a bottleneck, or anything time consuming.

btw are you sure you have to check for 1 and not -1? -1 indicates the end of a stream. perhaps it’s throwing exceptions the whole time. I’ll load the code up into my ide later this eve… euh night. or tomorro moring I gues.

Well, im having the client program send the server an integer indicating a course of action, and 1 just happens to be close the socket etc. Im not at home this weekend, which ticks me off becaue i really want to get this working correctly… I think that the only way to find out where its getting stuck at is to put a System.out.println() statement in the front of each of the 4 methods inside the while loop, and see where it slows down when multiple users connect… Heh… well, if you can manage to help me solve this i wouldnt mind putting u in the special thanks section of my game credits lol.

try posting the client code

well, as i mentioned before im not at my home computer at the moment so i wouldnt be able to get the accual client program till monday. It was just a simple program to connect to the server and recieve from the server the number of clients connected and display it in an applet… not to difficult. God i really hope we can figure this out cuz its pissing me off! lol

FWIW, try: http://javagamesfactory.org/views/article?title=NIO%20Networking%20for%20Games

note that the site is going down once a day at the moment, and that that’s only a draft article

oy… are you telling me that after a week of programming this dam server i still got more to read before i can do it… if i gotta reprogram this whole thing im just gonna cry… :’( lol