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]