English 中文(简体)
为什么在我执行这一申请时,我会碰到太长的例外?
原标题:Why I am getting the bad length exception when I am running this application?
  • 时间:2011-01-17 21:38:09
  •  标签:
  • java-me
  • udp

我愿向使用UDP的J2me台服务器传送。 然而,当我接手时,我却遇到了一个严重的例外。 我的守则和产出如下。

客户守则

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.DatagramConnection;
import java.io.IOException;

public class DatagramTest extends MIDlet
implements CommandListener, Runnable
{
private static final int BUF_SIZE = 1024;
private static Command exit = new Command("Exit", Command.EXIT, 1);
private static DatagramTest instance;
private Display display;
private TextBox dgramText;

private DatagramConnection conn;

private Datagram dgram;

private String address = "datagram://myip:9876";


public DatagramTest()
{
super();
instance = this;
}


public DatagramTest(String service)
{
this();
address = service;
}
/**
Returns the single instance of this class. Calling
this method before constructing an object will return
a null pointer.
@return an instance of this class.
*/
public static DatagramTest getInstance()
{
return instance;
}
public void startApp()
{
display = Display.getDisplay(this);
dgramText = new TextBox("Datagram contents",
null,
2048,
TextField.ANY);
dgramText.setCommandListener(this);
display.setCurrent(dgramText);
System.out.println("Starting run....");
run();
System.out.println("Stopping run....");
}


public void run()
{
     System.out.println("In run....");
try
{
int maxLength;

conn = (DatagramConnection)Connector.open(address);
maxLength = conn.getMaximumLength();
dgram = conn.newDatagram(1024);

dgram.reset();

conn.send(dgram);

conn.receive(dgram);

byte[] data = dgram.getData();
// Extract the response string.
String str = new String(data);

System.out.println(str);

dgram.reset();
System.out.println("Exit run....");

}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
ioe.printStackTrace();
quit();
}
return;
}
public void pauseApp()
{
}
void quit()
{
destroyApp(true);
notifyDestroyed();
}
public void destroyApp(boolean destroy)
{
try
{
conn.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
public void display()
{
Display.getDisplay(this).setCurrent(dgramText);
}
public void commandAction(Command c, Displayable d)
{
if (c == exit)
{
quit();
}
}
}

服务器代码

import java.io.*;
import java.net.*;

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
      }
}

客户的产出

Starting run.... In run.... Bad datagram length java.io.IOException: Bad datagram length at com.sun.midp.io.j2me.datagram.Protocol.receive(Protocol.java:367) at hello.DatagramTest.run(DatagramTest.java:89) at hello.DatagramTest.startApp(DatagramTest.java:69) at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:43) at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:374) at com.sun.midp.main.Main.runLocalClass(Main.java:466) at com.sun.midp.main.Main.main(Main.java:120) Stopping run....

我为什么会这么长时间的例外情况,我如何去掉?

最佳回答

你们需要尝试的是,以单独的形式发送和接收数据表。

数据图Connection.receive()的文件指出:“在收到数据表之前,这一方法区块”

You are calling it from inside MIDlet.startApp().

限制使用管理系统,即“App(App)”是坏的做法。

问题回答

暂无回答




相关问题
add text in http request string url

ok i made a midlet through which i can connect to server pages and get soem information as response. For example i made a midlet through which i acced url: http://example.com/?u=nepal&t=1 Now i ...

Do I have to do a setSize() on a Vector before using it?

Given private final Vector v = new Vector(); //instance variable the following 3 lines are in an instance method in the same class. 1. int length = v.capacity(); 2. int size = v.size(); ...

Is the situation with Java ME improving?

It seems to be the consensus that developing for Java ME is not as cross platform as you might expect, particularly compared to say java SE, but it is difficult to assess how the situation is evolving....

Privileged operations in netbeans mobility

I m writing a Java ME app that will use privileged operations such as messaging. By default the user is prompted to confirm each of these operations, but I would like to run it as a background ...

ClassFormatError: 56 while using hessian in j2me

I am trying to use the hessian j2me implementation @ http://hessian.caucho.com/ using java me sdk 3.0. http://hessian.caucho.com/doc/hessian-overview.xtp#Hessian%20Client%20for%20a%20cell-phone ...

热门标签