หน้าแรก
 การบ้าน
  C/C++ new
  Java new
  Windows Phone
  แจ้งเรื่องที่ต้องการ
 บทความ
  .NET
  Android
  Blackberry
  C++Builder
  Delphi
  Java
  Mobile
  Open Source
  Pocket PC
  Visual Basic
  Web
  Windows Phone
  Others
 แบบเรียนฟรี
  ASP
  ASP.NET Webmatrix
  C++
  eVB
  J2ME
  Java
  PHP
 ซอร์สโค้ด
  Android
  ASP
  C++
  VC++
  CGI-Perl
  Delphi
  Java
  jQuery
  PHP
  Visual Basic
  .NET
 บริการจากเรา
  เปิดท้าย... ขายโค้ด
  Freeware
  อัตราค่าโฆษณา
  รายชื่อลูกค้า
  สถิติผู้เข้าเว็บ
 ดาว์นโหลด
 ค้นหาข้อมูล
 กระดานสนทนา
  .NET
  C/C++
  Delphi
  Java
  Palm/PocketPC
  Visual Basic
  Web
  อื่นๆ
 กระดานงาน
 ลิงค์เว็บ
 เกี่ยวกับผม
 อัตราค่าโฆษณา
Social Network

Facebook  Twitter  YouTube
 New Article
 Webboard
 Freelanceboard
Freeware
โปรแกรมเบอร์ดี (BerDee)
โปรแกรมเบอร์ดี (Android)
เกมส์เป่ายิ้งฉุบ
เกมส์เป่ายิ้งฉุบ(Android)
WebcamMonitor
WebcamMonitor(Windows)
scSendMail
scSendMail(Windows)
MSN Caption
MSN Caption(Windows)
  Freelance comment
  ติดต่อสอบถามมาได้ตลอดนะ...
2015-07-29 13:35:58
  ถ้าผมต้องการเว็บที่พัฒน...
2015-01-18 15:33:54
  ถ้าผมต้องการเว็บที่พัฒน...
2015-01-18 15:32:51
  ติดต่อสอบถามเข้ามาได้นะ...
2014-01-06 12:47:38
  ถ้ายังหาคนสอนไม่ได้ ก็ลอ...
2013-07-06 01:04:37
  สนใจส่งขอบเขตมาคุยราคาก...
2013-03-24 18:54:20
  ถ้ายังไม่มีคนรับงานติดต...
2012-12-16 19:18:14
  สนใจคะ ติดต่อ 0905076277...
2012-11-12 11:07:46
  รับทำโปรเจ็คนักศึกษาหรื...
2012-10-29 03:10:46
  sukit_te34@hotmail.com...
2012-10-29 03:09:36
  Webboard comment
  เขียนโปรแกรมภาษาจาวา
1.โ...

2023-09-12 11:40:28
  ต้องลอง debug ดูเองครับ
ไล่...

2023-06-08 18:03:06
  ร้านค้าแห่งหนึ่ง จะต้องน...
2021-09-21 08:47:08
  จงเขียนโปรแกรมดำเนินการ...
2021-05-07 15:06:11
  จงเขียนโปรแกรมสำหรับคำน...
2021-03-09 14:21:45
  ขอดู...
2021-01-04 23:56:45
  ขอโค้ดเพื่อทำการเรียนกา...
2020-12-09 23:02:09
  แล้ว ?...
2020-12-09 10:56:34
  จ้า...
2020-10-18 16:53:21
  พอดีผมเหลื่อแค่ตัดสต๊อก...
2020-05-31 21:49:43
  Homework comment
  เขียนโปรแกรมเพื่อรับค่า...
2022-02-24 10:45:17
  จากโค้ดนี้อะครับ พอดีลอง...
2021-03-22 01:22:39
  จากโค้ดนี้อะครับ พอดีลอง...
2021-03-22 01:22:39
  โจทย์การบ้าน c++ เขียนรูปแ...
2020-04-06 11:01:33
  แบบนี้ขอเป็น ภาษา php หน่อย...
2019-09-18 14:36:34
  ผมไม่อยากให้คุณได้คะแนน...
2019-04-27 01:29:07
  อาจารย์เขาบอกแนวข้อสอบม...
2019-04-27 00:44:29
  ขอสอบถามการเขียน c++ ครับ เ...
2018-04-02 12:19:21
  โค้ดตัวอย่างศึกษาให้เข้...
2017-11-26 14:50:05
  คำนวณค่าน้ำโดยรับค่ามิเ...
2017-11-20 23:15:26
  Article comment
  เท่าที่อ่าน error คือพื้นที่...
2022-03-23 13:56:48
  เรียน สอบถามปัญหา กับ อ.คร...
2022-01-24 17:39:49
  ต้องการ ให้เขียน โปรแกรม ...
2019-12-09 11:39:58
  รับเขียน arx จาก vc++ 2017 ล็อคโปร...
2019-09-19 09:48:09
  ทำการ register ไฟล์ที่ชื่อ mswinsck.oc...
2019-09-17 14:05:56
  ใน vb 6 ไม่มี component winsock เลยค่ะ ส...
2019-09-03 10:31:02
  รบกวนขอสอบถามหน่อยนะครั...
2019-03-04 05:31:04
  สามารถนำตัวหนังสือจากภา...
2018-12-25 08:54:32
  มีcode ของ VB ไหมค่ะ ถ้ามีรบกว...
2017-09-28 16:50:02
  น่าจะได้ครับ ไม่เคยลอง
...

2017-07-11 09:59:35
  9Mcode comment

Link Exchange

อัตราค่าโฆษณา
 
 J2ME (Java Micro Edition) Tutorial  

- - - - - - - - - - - - - - ผู้ให้การสนับสนุน- - - - - - - - - - - - - -

 J2ME (Java Micro Edition)
 
 

บทที่ 7 ภาคปฏิบัติ

 

บทที่ 7 ภาคปฏิบัติ

ตัวอย่างที่ 1 DayTimeServer & DayTimeClient (J2SE)

สำหรับตัวอย่างแรกจะเป็นการโปรแกรมที่เป็น java แบบธรรมดา เพื่อแสดงการทำงานแบบ Client/Server โดยจะประกอบด้วย สองส่วนคือ ส่วนของ Server ที่ทำงานหน้าที่ รอการร้องขอ และให้ข้อมูลที่เป็นเวลา กับ Client ที่ร้องขอเข้ามา

ส่วน Client ก็จะทำหน้าที่ร้องขอข้อมูล (ในที่นี้คือ วันเวลา) เมื่อได้รับข้อมูลกลับมา จะแสดงผล ให้ผู้ใช้ได้ทราบ

สำหรับการตัวอย่างนี้ คุณสามารถเก็บไว้ใน โฟล์เดอร์ ใดๆ ที่ต้องการก็ได้ แต่สำหรับในตัวอย่างนี้ ผมจะเก็บไฟล์ทั้ง สองไว้ที่ C:\ClientServer และให้ทำการเพิ่ม Path ของ Java ไว้ใน System variables วิธีการคือ

  • ทำการคลิกขวาที่ My Computer ที่อยู่บน Desktop แล้วเลือก Property
  • คลิกที่แท็บ Advance เลือกคลิกเลือกที่ปุ่ม Environment Variables
  • ในช่อง System Variable ให้ดับเบิ้ลคลิกที่ Path แล้วป้อน ;C:\j2sdk1.4.1_02\bin ลงต่อท้ายในช่อง Variable Value
  • คลิกปุ่ม OK เพื่อยืนยัน ทั้งสองหน้าต่าง

เริ่มสร้างไฟล์ทั้งสองกันได้เลยครับ หรือจะทำการ Download ไปก็ได้เช่นกัน

DayTimeServer.java

Download Code Download Code
import java.net.*;
import java.io.*;
import java.util.Date;

public class DayTimeServer{
  public final static int daytimePort = 5050;
  public static void main(String[] args){
    ServerSocket theServer = null;
    Socket theConnection;
    DataOutputStream p;
    try{
      theServer = new ServerSocket(daytimePort);
	while(true){
	  theConnection = theServer.accept();
	  p = new DataOutputStream(theConnection.getOutputStream());
	  System.out.println(theConnection.getInetAddress());
	  p.writeUTF(""+new Date());
	  p.flush();
	  theConnection.close();
	}
    }
    catch(IOException e){
	System.err.println(e);
    }
    
    
  
  }
}

DayTimeClient.java

Download Code Download Code
/* * DayTimeClient.java * * Created on 13 ??????? 2546, 16:38 ?. */
      
        import java.net.*;
        import java.io.*;
      /**
        * @author sup98
        * @version
        */
        public class DayTimeClient{
		public static void main(String[] args){
		  Socket theSocket;
		  DataInputStream theTimeStream;
		  try{
		    theSocket = new Socket("localhost",5050);
		    theTimeStream = new DataInputStream(theSocket.getInputStream());
		    String theTime = theTimeStream.readUTF();
		    System.out.print("It is "+theTime);
		  }
		  catch(UnknownHostException e){
		    System.err.println(e);
		  }
		  catch(IOException e){
		    System.err.println(e);			
		  }
		}          
        }

ผลการ Run

  • ที่หน้าต่างของ Command Prompt ให้ทำการ Compile ไฟล์ ทั้งสอง โดยใช้คำสั่ง javac C:\ClientServer\*.java ดังรูป

Compilation
รูปที่ 55 แสดงหน้าต่าง Command Prompt ขณะคอมไพล์ โค้ด

  • ทำการเปลี่ยน Directory เข้าไปใน C:\ClientServer
  • สั่ง Run Server ด้วยคำสั่ง java DayTimeServer ดังรูป

Running Server
รูปที่ 56 แสดงหน้าต่าง Command Prompt ขณะ Run Server

  • ทำการเปิดหน้าต่าง Command Prompt อีกหน้าต่าง
  • ทำการเปลี่ยน Directory เข้าไปใน C:\ClientServer
  • สั่ง Run Client ด้วยคำสั่ง java DayTimeClient ดังรูป

Running Clients
รูปที่ 57 แสดงหน้าต่าง Command Prompt ขณะ Run Client

อธิบาย: จากตัวอย่าง จะเห็นได้ว่า ทางด้าน Client จะได้รับ ข้อมูลวันที่ ที่ทาง Server ส่งมาให้


ตัวอย่างที่ 2 HttpDemo

สำหรับในตัวอย่างนี้ จะแบ่งเป็น 2 ส่วนคือ ส่วนของไฟล์ ASP และ ไฟล์ Java โดยไฟล์ ASP จะเป็นไฟล์ที่ทำหน้าที่ ดึงเวลาจากเครื่อง Server ขึ้นมาแสดง และ ไฟล์ Java จะเป็นไฟล์ที่เป็น MIDlet ที่จะ ทำงานบนมือถือ เพื่อมาอ่านข้อมูล ดังกล่าว แล้วนำมาแสดงผล

สำหรับการติดตั้ง IIS Web Server เพื่อใช้งาน Web Server บนเครื่องของคุณ คุณสามารถหาอ่านได้จาก บทความ ในหัวข้อ Web ของเว็บ http://www.sourcecode.in.th

ASPDate.asp

Download Code Download Code
DateTime: <%=now()%>

HttpDemo.java

Download Code Download Code
/* * HttpDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */
      
        import java.io.*;
        import javax.microedition.io.*;
        import javax.microedition.midlet.*;
        import javax.microedition.lcdui.*;
      /**  
        * @author sup98
        * @version
        */
        public class HttpDemo extends MIDlet {

	  private Display display;
	  String url = "http://localhost/ASPDate.asp";
	  

        
        
        public HttpDemo() {
		display = Display.getDisplay(this);
        }
        
        /**
        * Start up the Hello MIDlet by creating the TextBox and associating
        * the exit command and listener.
        */
        public void startApp() {
		try{
			invokeASP(url);
		}catch(IOException e){
			System.out.println("IOException " + e);
			e.printStackTrace();
		}		
        }
        
        /**
        * Pause is a no-op since there are no background activities or
        * record stores that need to be closed.
        */
        public void pauseApp() {
        }
        
        /**
        * Destroy must cleanup everything not handled by the garbage collector.
        * In this case there is nothing to cleanup.
        */
        public void destroyApp(boolean unconditional) {
        }
        
	  void invokeASP(String url) throws IOException{
		HttpConnection c = null;
		InputStream is = null;
		StringBuffer b = new StringBuffer();
		TextBox t = null;
		try{
			c = (HttpConnection)Connector.open(url);
			c.setRequestMethod(HttpConnection.GET);
			c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");
			c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");
			is = c.openDataInputStream();
			int ch;
			while((ch = is.read())!= -1){
				b.append((char) ch);
				//System.out.println((char) ch);
			}
			t = new TextBox("First HTTP Demo", b.toString(), 1024, 0);

		}finally{
			if(is != null){
				is.close();
			}
			if(c != null){
				c.close();
			}			
		}
		display.setCurrent(t);
	  }
        
        }

ผลการ Run

HttpDemo MIDlet
รูปที่ 58 แสดงหน้าต่าง Emulator ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างในการแสดง ถึงการใช้ MIDlet ติดต่อไปขอข้อมูลจาก Web Servers


ตัวอย่างที่ 3 HttpPostDemo

สำหรับในตัวอย่างนี้ จะแบ่งเป็น 2 ส่วนคือ ส่วนของไฟล์ ASP และ ไฟล์ Java โดยไฟล์ ASP จะเป็นไฟล์ที่ทำหน้าที่ แสดงข้อมูล ที่ทาง MIDlet ได้ทำการส่งไป และ ไฟล์ Java จะเป็นไฟล์ที่เป็น MIDlet ที่จะ ทำงานบนมือถือ เพื่อส่งข้อมูล ที่ผู้ใช้ทำการเลือก แล้วจะนำผลที่ได้รับจาก Web Server มาแสดง

สำหรับการติดตั้ง IIS Web Server เพื่อใช้งาน Web Server บนเครื่องของคุณ คุณสามารถหาอ่านได้จาก บทความ ในหัวข้อ Web ของเว็บ http://www.sourcecode.in.th

HttpPostDemo.asp

Download Code Download Code
Server: <%=request.Form("lang")%>

HttpPostDemo.java

Download Code Download Code
/* * HttpPostDemo.java * * Created on 13 ??????? 2546, 16:38 ?. */
      
        import java.io.*;
        import javax.microedition.io.*;
        import javax.microedition.midlet.*;
        import javax.microedition.lcdui.*;
      /**  
        * @author sup98
        * @version
        */
        public class HttpPostDemo extends MIDlet implements CommandListener{

	  private Display display;
	  private Command exitCommand;
	  private Alert alert = new Alert("Your choice", "", null, null);
	  String url = "http://localhost/HttpPostDemo.asp";
	  private List l1;
	  private String choice[] ={"Java", "Pascal" ,"C++" ,"Fortran"};

        
        public HttpPostDemo() {
		display = Display.getDisplay(this);
		exitCommand = new Command("Exit", Command.SCREEN, 1);
        }
        
        /**
        * Start up the Hello MIDlet by creating the TextBox and associating
        * the exit command and listener.
        */
        public void startApp() {
		l1 = new List("Language", Choice.IMPLICIT);
		for(int i=0; i<choice.length; i++){
			l1.append(choice[i],null);
		}
		l1.addCommand(exitCommand);
		l1.setCommandListener(this);
		display.setCurrent(l1);
        }
        
        /**
        * Pause is a no-op since there are no background activities or
        * record stores that need to be closed.
        */
        public void pauseApp() {
        }
        
        /**
        * Destroy must cleanup everything not handled by the garbage collector.
        * In this case there is nothing to cleanup.
        */
        public void destroyApp(boolean unconditional) {
        }
	  
	  void invokeASP(String url, String lang) throws IOException{
		HttpConnection c = null;
		OutputStream out = null;
		InputStream is = null;
		StringBuffer b = new StringBuffer();
		TextBox t = null;
		try{
			c = (HttpConnection)Connector.open(url);
			c.setRequestMethod(HttpConnection.POST);
			c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:18:14 GMT");
			c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
			c.setRequestProperty("Content-Language", "en-US");
			c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			String postString = "lang="+lang;
			c.setRequestProperty("Content-Length", String.valueOf(postString.length()));
			out = c.openOutputStream();
			out.write(postString.getBytes());
			out.flush();
		
			is = c.openDataInputStream();
			int ch;
			while((ch = is.read()) != -1){
				b.append((char) ch);				
			}
			alert.setString(b.toString());

		}finally{
			if(is!=null){
				is.close();
			}
			if(c!=null){
				c.close();
			}
		}
		display.setCurrent(alert, l1);
	  }		

	  public void commandAction(Command c, Displayable s){
		if(c==exitCommand){
			destroyApp(false);
			notifyDestroyed();
		}else if(c==List.SELECT_COMMAND){
			try{
				invokeASP(url, choice[((List)s).getSelectedIndex()]);
			}catch(IOException e){
				System.out.println("IOException " + e);
				e.printStackTrace();
			}
		}
	  }       
        }

ผลการ Run

HttpPostDemo MIDlet
รูปที่ 59 แสดงหน้าต่าง Emulator ที่ run MIDlet ที่เขียนขึ้น

อธิบาย: โค้ดข้างต้น เป็นตัวอย่างในการแสดง ถึงการใช้ MIDlet ติดต่อ ส่งข้อมูลแบบ Post ไปยัง Server และรับผลที่ได้จาก Web Server มาแสดง

 

BACK

คลิปสอนเขียนโปรแกรม
เรียนเขียนโปรแกรมดอทเน็ต
เรียนเขียนโปรแกรมแอนดรอยด์
เรียนเขียนโปรแกรมเบล็คเบอร์รี่
เรียนเขียนโปรแกรมซี ซีพลัสพลัส
เรียนเขียนโปรแกรมจาวา
เรียนการใช้งานลินุกส์
เรียนการเขียนโปรแกรมวินโดว์โฟน
เรียนการเขียนโปรแกรมพีเฮชพี มายเอสคิวเอล
อัตราค่าโฆษณา
 
Tutorial
eVB Tutorial
ASP.NET Webmatrix Tutorial
J2ME Tutorial
C++  Tutorial
Java  Tutorial
PHP Tutorial
ASP Tutorial
 
แบบสำรวจ

Home - Article - Tutorial - Sourcecode - Dev Handbook - Search - WebBoard - Links - About Us

สงวนลิขสิทธิ์ ห้ามคัดลอก ทำซ้ำ แก้ไข ดัดแปลง ไม่ว่าในรูปแบบใด โดยไม่ได้รับอนุญาตเป็นลายลักษณ์อักษร
เลขทะเบียนพาณิชย์อิเล็กทรอนิกส์จากกระทรวงพาณิชย์: 7100503002000
Copyright 2001 SourceCode.in.th