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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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-ID: 190014

Url: https://administrator.de/forum/verbindung-pc-zu-handy-ueber-midlet-schlaegt-fehl-190014.html

Ausgedruckt am: 08.04.2025 um 09:04 Uhr