2 Replies - 940 Views - Last Post: 09 January 2012 - 06:23 AM Rate Topic: -----

#1 ob22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-January 12

i have to design a multiplayer network quiz application in java

Posted 09 January 2012 - 04:37 AM

Am stuck with the connection, i intended to go with socket connection with multithreaded capabilities. The problem is how to make the server send question and client able to send answers.. can you please help me and send me some tutorials and other relative information
Is This A Good Question/Topic? 0
  • +

Replies To: i have to design a multiplayer network quiz application in java

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: i have to design a multiplayer network quiz application in java

Posted 09 January 2012 - 04:54 AM

Simple one

http://www.dreaminco...snippet1917.htm

A simple chat room

http://www.dreaminco...r-gui-optional/
Was This Post Helpful? 0
  • +
  • -

#3 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: i have to design a multiplayer network quiz application in java

Posted 09 January 2012 - 06:23 AM

Usually for something like this you'd design a simple packet system.

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import network.exception.PacketNotFoundException;


public abstract class AbstractPacket
{
	public abstract boolean sendPacketData(DataOutputStream dataWriter) throws IOException;
	public abstract boolean readPacketData(DataInputStream dataReader) throws IOException;
	
	public abstract int getPacketLength();
	public abstract int getPacketID();
	
	public abstract String packetDebugData();
	
	public String debugPrint()
	{
		StringBuffer sBuff = new StringBuffer();
		
		sBuff.append("Packet ID: " + getPacketID() + "\n");
		sBuff.append("Packet Length: " + getPacketLength() + "\n");
		sBuff.append("Packet Data: \n" + packetDebugData());
		
		return sBuff.toString();
	}
	
	
	public static boolean sendPacket(AbstractPacket pckt, DataOutputStream dataWriter) throws IOException
	{
		dataWriter.writeInt(pckt.getPacketID());
		dataWriter.writeInt(pckt.getPacketLength());
		return pckt.sendPacketData(dataWriter);
	}
	
	public static AbstractPacket readPacket(DataInputStream dataReader) throws IOException
	{
		int packetType = -1;
		int packetLength = -1;
		
		packetType = dataReader.readInt();
		packetLength = dataReader.readInt();
		
		if(packetType != -1 && packetLength != -1)
		{
			AbstractPacket currentPacket;
			
			try
			{
				currentPacket = PacketMap.packetFromID(packetType);
				
				currentPacket.readPacketData(dataReader);
				
				return currentPacket;
			}
			catch(PacketNotFoundException pException)
			{
				// Skip the packet's data if we can't find a handler for it.
				// this is so that the packet handler doesn't flip out if it 
				// can't find a proper handler, this will help to keep the
				// packets flowing without a complete reset of the streams.
				dataReader.skip(packetLength);
				
				return null;
			}
			
			
		}
		else
		{
			return null;
		}
	}

	
	protected static String readString(DataInputStream dataReader) throws IOException
	{
		byte[] sBytes = new byte[dataReader.readShort()];
		dataReader.read(sBytes, 0, sBytes.length);
		return new String(sBytes);
	}
	
	protected static void writeString(DataOutputStream dataWriter, String wString) throws IOException
	{
		dataWriter.writeShort(wString.length());
		dataWriter.write(wString.getBytes());
	}

}



At least, thats how I handle this kind of thing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1