Hallo an alle,
ich experimentiere gerade ein wenig mit der WLan Verbindung meines Handy(LG KF900) und habe das SDK-15 von LG installiert.
Als Beispiel Anwendung habe ich eine Server-Client Applikation geschrieben:
Im Simulator klappt alles soweit, doch wenn ich es auf das Handy lade, werde ich gefragt:
Bis zum Beenden zulassen?
Dieses Mal zulassen?
Dieses Mal ablehnen?
Wähle ich eine der ersten beiden Optionen bekomme ich eine IO Exception.
Woran könnte das liegen? Müsste nicht gefragt werden ob man über das Wi-Fi oder UMTS verbunden werden will ??
Server:
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Client:
--------------------------------------------------------------------------------
ich experimentiere gerade ein wenig mit der WLan Verbindung meines Handy(LG KF900) und habe das SDK-15 von LG installiert.
Als Beispiel Anwendung habe ich eine Server-Client Applikation geschrieben:
Im Simulator klappt alles soweit, doch wenn ich es auf das Handy lade, werde ich gefragt:
Bis zum Beenden zulassen?
Dieses Mal zulassen?
Dieses Mal ablehnen?
Wähle ich eine der ersten beiden Optionen bekomme ich eine IO Exception.
Woran könnte das liegen? Müsste nicht gefragt werden ob man über das Wi-Fi oder UMTS verbunden werden will ??
Server:
--------------------------------------------------------------------------------
Java:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class MulServer {
/**
* @param args
*/
private final ServerSocket server;
public MulServer(int port) throws IOException
{
server = new ServerSocket(port);
}
private void startServing()
{
while(true)
{
Socket client = null;
try
{
client = server.accept();
handleConnection(client);
}
catch(IOException e)
{
e.printStackTrace();
}
finally{
if(client != null)
try{
client.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
private void handleConnection( Socket client) throws IOException
{
InputStream in = client.getInputStream();
OutputStream out= client.getOutputStream();
int factor1 = in.read();
int factor2 = in.read();
out.write(factor1 * factor2);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
MulServer server = new MulServer( 6666);
server.startServing();
}
}
Client:
--------------------------------------------------------------------------------
Java:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Client extends MIDlet implements CommandListener{
private Command exit, start;
private Display display;
private Form form;
private TextField TF_result;
public Client() {
// TODO Auto-generated constructor stub
display = Display.getDisplay(this);
exit = new Command("Exit",Command.EXIT,1);
start = new Command("Start", Command.EXIT,1);
form = new Form("Read Write Socket");
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display.setCurrent(form);
}
public void commandAction(Command command, Displayable Displayable) {
// TODO Auto-generated method stub
if(command == exit){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}
else if(command == start){
try{
StreamConnection connection = (StreamConnection) Connector.open("socket://192.168.0.111:6666");
//PrintStream output = new PrintStream(connection.openOutputStream());
//output.println("GET /my.html HTTP/0.9\n\n");
//output.flush();
InputStream in = connection.openInputStream();
OutputStream out= connection.openOutputStream();
out.write(4);
out.write(60);
//int ch;
// while( (ch = in.read() )!= -1){
// System.out.println( (char) ch);
// }
int result = in.read();
TF_result = new TextField("Result: " + result, "", 15, TextField.ANY);
form.append(TF_result);
in.close();
//output.close();
out.close();
connection.close();
}catch( ConnectionNotFoundException error){
Alert alert = new Alert("Error", "Cannot access socket.", null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}catch( IOException error){
Alert alert = new Alert("Error", error.toString(), null, null);
alert.setTimeout(Alert.FOREVER);
alert.setType(AlertType.ERROR);
display.setCurrent(alert);
}
}
}
}