letavino
Goto Top

Verbindung PC zu Handy über Midlet schlägt fehl

Hallo,
ich probe gerade die Programmierung mit J2ME, spezieller bin ich gerade dabei ein Midlet zu schreiben, dass über TCP eine Verbindung mit dem PC herstellen soll.

Auf dem Rechner (Windows Vista Professional) wurde das Midlet mit WTK simuliert und die Verbindung zu einem Java Server nachgestellt.
Das funktioniert auch einwandfrei.
Nun habe ich das Programm auf das Handy ( Samsung Monte ) übertragen und mit WLAN mit dem Netzwerk verbunden.
Hier schlägt die Verbindung beim testen fehl.
Das Handy hat folgende Ausgabe:

Status: javax.microedition.io.ConnectionNotFoundException: error 0 in socket::open

Kann man daraus irgendwelche Hinweise gewinnen?
Was könnte die Verbindung genau stören?

Der Code:
import java.io.*;
import java.util.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;

public class Network extends MIDlet implements CommandListener
{
	private Display  display;          
	private Form form;	
	private TextField txfCommand;
	
	private TextField txfIp1;
	private TextField txfIp2;
	private TextField txfIp3;
	private TextField txfIp4;
	private TextField txfPort;
	
	private StringItem status;
	private StringItem answer;
	
	private Command exit, submit, again;
	
	public Network()
	{
		display = Display.getDisplay(this);
	}
	protected void startApp()
	{
		form = new Form("TCP Sender");  
		
		
		txfCommand = new TextField("Kommando:","Test 123",20,0);  
		
		txfIp1 = new TextField("IP:","192",3,TextField.NUMERIC);  
		txfIp2 = new TextField(null,"168",3,TextField.NUMERIC);  
		txfIp3 = new TextField(null,"0",3,TextField.NUMERIC);  
		txfIp4 = new TextField(null,"105",3,TextField.NUMERIC);  
		
		txfPort = new TextField(null,"",5,TextField.NUMERIC);  
		
		status = new StringItem("Status: ","Noch nichts gesendet!");  
		answer = new StringItem("Antwort: ","");  

		txfIp1.setLayout(Item.LAYOUT_2); 
		txfIp2.setLayout(Item.LAYOUT_2); 
		txfIp3.setLayout(Item.LAYOUT_2); 
		txfIp4.setLayout(Item.LAYOUT_2); 
		
		form.append(txfCommand);
		form.append(txfIp1);
		form.append(txfIp2);
		form.append(txfIp3);
		form.append(txfIp4);
		form.append(txfPort);
		
		form.append(status);
		form.append(answer);
	    
		exit = new Command("Ende", Command.EXIT, 1);  
        submit = new Command("Start", Command.SCREEN, 2);  
        
        form.addCommand(submit);
        form.addCommand(exit);
	    display.setCurrent(form);
	    form.setCommandListener(this);
	}
	protected void pauseApp(){}
	
	protected void destroyApp( boolean unconditional )	{}
  
	public void exitMIDlet(){}
	public void commandAction(Command c, Displayable d) 
	{		
        if (c == exit) 
        {
            destroyApp(true);
            notifyDestroyed();
        } 
        else if (c == submit || c == again) 
        {
        	Thread tServer = new Thread() {
        		public void run() {
        			server();
        	    }};
        	
        	Thread tClient = new Thread() {
        		public void run() {
        			client();
        	    }};
        	
        	tServer.start();
        	tClient.start();  
        }
        
    }
	private void server() 
	{
       	try
		{
		  ServerSocketConnection ssc = (ServerSocketConnection) 
		  Connector.open("socket://:9004");  
		  StreamConnection sc = null;
		  InputStream is = null;
		  try{
		    sc = ssc.acceptAndOpen();
		    is = sc.openInputStream();
		    int ch = 0;
		    StringBuffer sb = new StringBuffer();
		    while ((ch = is.read()) != -1){
		      sb.append((char)ch);
		    }
		    answer.setText("> "+sb.toString());  
		    System.out.println("Server: "+sb.toString());  
		  } finally{
		      ssc.close();
		      sc.close();
		      is.close();
		  }
		} catch (IOException x) {
		    x.printStackTrace();
		}	
	}
	private void client() 
	{	     
		status.setText("");  
		answer.setText("");  
		
        String command = txfCommand.getString();
        String ip = txfIp1.getString()+"."+txfIp2.getString()+"."+txfIp3.getString()+"."+txfIp4.getString();  
        String port = txfPort.getString();
        System.out.println("socket://"+ip+":"+port);  
        try{
        	  SocketConnection sc = (SocketConnection)Connector.open("socket://"+ip+":"+port);  
        	  OutputStream os = null;
        	  try{
        	    os = sc.openOutputStream();
        	    byte data = command.getBytes();
        	    os.write(data);
        	  } finally{
        	      sc.close();
        	      os.close();
        	      status.setText("Erfolgreich gesendet!");  
        	  }
        	} catch (IOException x){
        		x.printStackTrace();
        		status.setText(x.toString());
        		//status.setText("Senden fehlgeschlagen!"); 
        }
    }
}

Der Java Server:
import java.util.Scanner;
 
import java.io.*; 
import java.net.*; 
 
public class HandyServer 
{ 
  private static void handleConnection( Socket client ) throws IOException 
  { 
    Scanner     in  = new Scanner( client.getInputStream() ); 
    PrintWriter out = new PrintWriter( client.getOutputStream(), true ); 
 
    String command 	= in.nextLine(); 
     		
    System.out.println(command);
   
  } 
  public static void main( String args ) throws IOException 
  { 
    ServerSocket server = new ServerSocket( 9003 ); 
 
    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 ) { } 
      } 
    } 
  } 
}

Lässt sich daraus irgendetwas schließen?
Muss ein Port gesondert geöffnet werden?
Oder was kann ich probieren?

Gruß, Florian

Content-Key: 190014

Url: https://administrator.de/contentid/190014

Printed on: April 25, 2024 at 06:04 o'clock