ورود

View Full Version : مشکل در دریافت پیامک



zacaria
یک شنبه 23 مرداد 1390, 14:11 عصر
سلام
من تلاش می کنم یک بازی شطرنج توسط اس ام ام بنویسم
به این صورت که (با فرض سفید بودن من و سیاه بودن شما) بعد از انجام حرکت اول توسط من برنامه، شماره مربع منبع و شماره مربع مقصد حرکت من را توسط اس ام اس به شما ارسال کند و برنامه سمت شما این پیام را دریافت و اجرا کند.
مشکل این است که برنامه سمت من بعد از ارسال پیام هنگ می کند و در پنجره output نت بینز پیغام [INFO] [sms ] ## javacall: SMS sending... به نمایش د رمیاید(در گوشی هم هنگ می کند)
البته برنامه سمت شما پیام را همین دریافت و اجرا می کند!!!

من بخشی از کدها را که مربوط به ارسال است اینجا میزارم:
کدهای کلاس ارسال کننده پیامک:


import java.io.IOException;
import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;


public class jcSendSMS extends Thread{

public static boolean recieve;
static String address = "";
public static String msgReceived = "";
public String payloadText;

public jcSendSMS(String mobile){
address = "sms://" + mobile + ":5000";
}

public void run(){

}

public void start(){
sendSMS(payloadText);
}

public boolean sendSMS(String payloadText){
try{
MessageConnection clientConn = (MessageConnection) Connector.open(address);
TextMessage tmsg = (TextMessage)clientConn.newMessage(MessageConnecti on.TEXT_MESSAGE);
tmsg.setAddress(address);
tmsg.setPayloadText(payloadText);
clientConn.send(tmsg);
clientConn.close();
}
catch (IOException ioe) {
System.out.println("ioe: " + ioe.toString());
return false;
} catch (IllegalArgumentException iae) {
System.out.println("iae: " + iae.toString());
return false;
} catch (SecurityException se) {
System.out.println("se: " + se.toString());
return false;
} catch (Exception e){
System.out.println("e: " + e.toString());
return false;
}
return true;
}
}



این هم متد ارسال کننده پیامک که شیی از کلاس بالا را نمونه ساز یکرده استارت می کند:


private void send(String str, int gameType){
try{
switch (gameType){
case jcGame.playAgainstHuman_withBluetooth :
break;
case jcGame.playAgainstHuman_withSMS :
if (jcGame.player[GameBoard.GetCurrentPlayer()] == jcGame.Human){
sms.payloadText = str;
sms.start();
}
break;
}
}catch(Exception e){System.out.println(e.toString());}
}



این هم متدی که متد بالا را فراخوانی می کند:


// A simple loop getting moves from the current player until the game is over
public boolean RunGame() throws Exception
{
try{
jcPlayer CurrentPlayer;
jcMove Mov;
CurrentPlayer = Players[ GameBoard.GetCurrentPlayer() ];
Mov = CurrentPlayer.GetMove( GameBoard );
if (Mov.isAccepted){
// Change the state of the game accordingly
SMSmov = Mov;
send(Integer.toString(SMSmov.SourceSquare) + Integer.toString(SMSmov.DestinationSquare), jcGame.game_type);
GameBoard.ApplyMove( Mov );
GameBoard.Print();
return true;
}
return false;
}catch(Exception e){System.out.println("E in runing game: " + e.toString());return false;}
}



به شدت محتاح کمک هستم
ممنون

dr_jacky_2005
دوشنبه 24 مرداد 1390, 10:23 صبح
قسمت run و start توی کلاس اول فکر میکنم نباید اینطوری باشه.
بیان تابع
sendSMS(payloadText);
رو توی run بنویسید
و واسه استفاده از تابع:
ترد رو بسازید و start رو ازش صدا بزنید.

zacaria
دوشنبه 24 مرداد 1390, 13:13 عصر
قسمت run و start توی کلاس اول فکر میکنم نباید اینطوری باشه.
بیان تابع
sendSMS(payloadText);
رو توی run بنویسید
و واسه استفاده از تابع:
ترد رو بسازید و start رو ازش صدا بزنید.

سلام
چطور میشه نخی که اجرا شده رو دوباره اجرا کرد؟
فرض کنیم

public class jcGame extends Thread
{
// Constructor
public jcGame()
{
}

public void run(){
try{
IsStart = true;
synchronized(this){
RunGame();
while(isSuspend)
wait();
}
}catch(Exception e){System.out.println(e.toString());}
}
}


با نمونه سازی از کلاس بالا

public static jcGame theGame;
.
.
.
theGame = new jcGame();
.
.
.
theGame.start();
theGame.join();
.
.
.
و حالا یخواهیم دوباره

theGame.start();
theGame.join();
که اینجا استثنا زیر صادر میشود


java.lang.IllegalThreadStateException: 110

dr_jacky_2005
دوشنبه 24 مرداد 1390, 13:32 عصر
باید ترد رو ببندید.
دقیق یادم نیست تابع اش چیه
ولی یکی از ایناست
.close
.invoke

zacaria
دوشنبه 24 مرداد 1390, 15:37 عصر
باید ترد رو ببندید.
دقیق یادم نیست تابع اش چیه
ولی یکی از ایناست
.close
.invoke

دقیقا مشکل من هم همینه که این متدها در j2me نیستند...
بنا به گفته سان متدهای suspen و stop هم که بدلیل مشکلاتی که داشتند حذف شده اند.
الان موندم برای بستن ترد چکار کنم!
میشه از متد interrupt استفاده کرد؟

dr_jacky_2005
سه شنبه 25 مرداد 1390, 10:43 صبح
همون،باید با یک متغیر بولین،توی یک While ،که توی run() هستند،قضیه رو هندل کنید.

این مثال کامل رو نگاه کنید:


import java.io.IOException;
import java.util.Vector;
import javax.wireless.messaging.*;

// A message receiver waits for messages to arrive
// on a message connection and then uses a separate
// thread to receive the actual message.

public class MessageQueuer
implements Runnable, MessageListener {

// A data structure representing either a
// connection-message pair (if a message was read)
// or a connection-exception pair (if an error
// occurred during the reading).

public static class Entry {
MessageConnection connection;
Message message;
IOException exception;
}

private Vector incoming = new Vector();
private Vector outgoing;
private boolean stop;

// Pass the outgoing vector into the constructor.

public MessageQueuer( Vector outgoing ){
this.outgoing = outgoing;
new Thread( this ).start();
}

// Called whenever a message arrives for the
// given connection. Queues the connection object
// and wakes up the thread to actually receive
// the message.

public void notifyIncomingMessage(
MessageConnection conn ){
if( !stop ){
synchronized( incoming ){
incoming.addElement( conn );
incoming.notify();
}
}
}

// Waits for incoming connection objects to be
// queued. When one arrives, pulls it off the queue
// and receives the messags. Queues the resulting
// connection-message pair on the outgoing
// queue and wakes anyone listening to the queue.

public void run(){
while( !stop || !incoming.isEmpty() ){
MessageConnection conn = null;

synchronized( incoming ){
while( !stop && incoming.isEmpty() ){
try {
incoming.wait();
}
catch( InterruptedException e ){
}
}

if( !incoming.isEmpty() ){
conn = (MessageConnection)
incoming.elementAt( 0 );
incoming.removeElementAt( 0 );
}
}

if( conn == null ) continue;

Entry entry = new Entry();
entry.connection = conn;

try {
entry.message = conn.receive();
}
catch( IOException e ){
entry.exception = e;
}

synchronized( outgoing ){
outgoing.addElement( entry );
outgoing.notify();
}
}
}

// Stops the processing of messages.

public void stop(){
synchronized( incoming ){
stop = true;
incoming.notify();
}
}
}