View Single Post
  #6 (permalink)  
Old 09-28-2008, 10:03 PM
GhosT's Avatar
GhosT GhosT is offline
Member
 
Join Date: Sep 2008
Location: Dhaka, Bangladesh
Posts: 37
Rep Power: 0
GhosT is on a distinguished road
Default
i think um almost close...these are the codes
Server:
Code:
import java.lang.*;
import java.io.*;
import java.net.*;

class Server {
   public static void main(String args[]) {
      String data = "1.Warcraft.DotA.GhosT.1";
      try {
         ServerSocket srvr = new ServerSocket(1234);
         Socket skt = srvr.accept();
         System.out.print("Client has connected!\n");
         PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
         System.out.print("Sending string: " + data + "\n");
         out.print(data);
         out.close();
         skt.close();
         srvr.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}
Client Side Scanner:
Code:
import java.lang.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class LocalScan {
  public static void main(String[] args) {
   String ip = "192.168.11.217";
	for (int i=1230;i<=1240;i++){
   testPort(ip,i);
}
    System.out.println("Completed");
  }

  private static void testPort(String ip, int i) {
         String data = "";
    try {
      Socket sock = new Socket(ip, i);
		   System.out.println("Game Found!");
         BufferedReader in = new BufferedReader(new
         InputStreamReader(sock.getInputStream()));

         while (!in.ready()) {}
	   data = in.readLine();

	   StringTokenizer strTokToken = new StringTokenizer(data, ".", false);
         System.out.println("Game Information:");
         while (strTokToken.hasMoreTokens())
         {
             System.out.println(strTokToken.nextToken());
         }

	   //System.out.println(data); // Read one line and output it
         in.close();

    } catch (java.io.IOException e) {
      System.out.println("Port " + i + " on "+ip+" is not in use.");
    }
  }
}
now the problem is when the client finds the server and the data, the server quits...but i need it to run because other clients will also check the game...the server will run untill its closed by the creator. i think it will need thread...and i know nothing about it...:S any help??

Last edited by GhosT; 09-28-2008 at 11:29 PM.
Reply With Quote