It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.


Resource Guide

Code Listing: Number Server

import java.io.*;
import java.net.*;
import java.lang.*;
 
public class NumberServer
{
     public static void main(String args[])
     {
       DatagramSocket receiveSocket = null;
       try
       {
         byte[] receiveBytes = new byte[1];
         InetAddress receiveAddr = InetAddress.getByName("localhost");
         int receivePort = 9000;
 
         receiveSocket = new DatagramSocket(receivePort, receiveAddr);
     
         while (true)
         {
           DatagramPacket receivePacket = new DatagramPacket(receiveBytes,               receiveBytes.length);
           receiveSocket.receive(receivePacket);
           String data = new String(receivePacket.getData(), 0,               receivePacket.getLength());
           byte[] reply = receivePacket.getData();
           String replyString = new String(reply);
           if (replyString.equals("4"))
               sendResultsBack(true, receivePacket.getAddress());
           else
               sendResultsBack(false, receivePacket.getAddress());
        }
       }
       catch (Exception e)
       {
          System.err.println("Exception " + e);
       }
       finally
       {
         if (receiveSocket != null)
         {
           try
           {
              receiveSocket.close();
           }
           catch (Exception e)
           {
             System.err.println("Exception " + e);
           }
         }
       }
     }
   
     // Send results back to the phone on port 9001
     public static void sendResultsBack(boolean rightanswer, InetAddress        sendAddr)
     {
       byte results[] = new byte[1];
       if (rightanswer)
         results[0] = (byte)'Y';
       else
         results[0] = (byte)'N';
       DatagramSocket sendSocket = null;
       int sendPort = 9001;
       try
       {
          sendSocket = new DatagramSocket();
          DatagramPacket sendPacket = new DatagramPacket(results,1, sendAddr,              sendPort);
          sendSocket.send(sendPacket);
       }
       catch (Exception e)
       {
         System.out.println("Exception " + e);
       }
       finally
       {
         if (sendSocket != null)
         {
           try
           {
              sendSocket.close();
           }
           catch (Exception e)
           {
             System.err.println("Exception " + e);
           }
         }
       }
     }
}

________________________________________________________

[Back To] Number Server


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service