boysna
27-01-2007, 18:29
Ragazzi come dico nel titolo vorrei creare una applicazione Client/Server dove il client è un telefonino e il servere un pc.
Sul cellulare ovviamente girerà una midlet.
Il programma Java per il pc l'ho gia fatto:
import java.io.IOException;
public class ServerTest {
public static void main(String[] args){
FileServerx f=new FileServerx();
try {
f.action();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import javax.microedition.io.*;
import java.io.*;
import javax.bluetooth.*;
import javax.obex.*;
public class FileServerx extends ServerRequestHandler
//implements ActionListener{
{
public FileServerx(){}
//Bluetooth Service name
private static final String myServiceName = "MyBtService";
// Bluetooth Service UUID of interest
private static final String myServiceUUID = "2d26618601fb47c28d9f10b8ec891363";
private UUID MYSERVICEUUID_UUID = new UUID(myServiceUUID, false);
public void action() throws IOException {
// Define the server connection URL
String url = "btgoep://localhost:" +MYSERVICEUUID_UUID.toString()+";name=FTP;authenticate=false;master=false;encrypt=false";
//String connURL = "btspp://localhost:"+MYSERVICEUUID_UUID.toString()+";name="+myServiceName+";";
// Create a server connection (a notifier)
//StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(connURL);
SessionNotifier scn = (SessionNotifier)Connector.open(url);
// Accept a new client connection
System.out.println("[server:] Now waiting for a client to connect");
//StreamConnection sc = scn.acceptAndOpen();
//StreamConnection conn = scn.acceptAndOpen();
scn.acceptAndOpen(this);
System.out.println("[server:] The client has created an OBEX session");
// RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);
//System.out.println("New client connection... " +
// rd.getFriendlyName(false));
/*
try {
java.io.InputStream is = conn.openInputStream();
System.out.println("Got data bytes " + is.available() + " name "
+ conn.getReceivedHeaders().getHeader(HeaderSet.NAME) +
" type " + conn.getType());
File f =
new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
FileOutputStream fos = new FileOutputStream (f);
byte b[] = new byte[1000];
int len;
while (is.available() > 0 && (len = is.read(b)) > 0) {
fos.write (b, 0, len);
}
fos.close();
System.out.println("[server:] Wrote data to " + f.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
//return ResponseCodes.OBEX_HTTP_OK;
//}*/
}
public int onConnect(HeaderSet request, HeaderSet reply) {
System.out.println("[server:] The client has created an OBEX session");
return ResponseCodes.OBEX_HTTP_OK;
}
public void onDisconnect (HeaderSet req, HeaderSet resp) {
System.out.println("[server:] The client has disconnected the OBEX session");
}
public int onPut (Operation op) {
try {
java.io.InputStream is = op.openInputStream();
System.out.println("Got data bytes " + is.available() + " name "
+ op.getReceivedHeaders().getHeader(HeaderSet.NAME) +
" type " + op.getType());
File f =
new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
FileOutputStream fos = new FileOutputStream (f);
byte b[] = new byte[1000];
int len;
while (is.available() > 0 && (len = is.read(b)) > 0) {
fos.write (b, 0, len);
}
fos.close();
System.out.println("[server:] Wrote data to " + f.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
return ResponseCodes.OBEX_HTTP_OK;
}
}
Per quanto riguarda la midlet ho solo fatto una semplice connessione ma ovviamente mi da un errore, ed è qui che non capisco il perchè:
Ecco il codice della midlet:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ClientMidlet extends MIDlet implements CommandListener{
public Display display;
public Form form;
public Command connetti;
public void startApp(){
display = Display.getDisplay(this);
form = new Form("Client Application");
connetti = new Command("connetti", Command.ITEM, 0);
form.addCommand(connetti);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){
notifyPaused();
}
public void destroyApp(boolean b){
notifyDestroyed();
}
public void commandAction(Command comando, Displayable display){
if(comando==connetti){
AvetanaConnection conn = new AvetanaConnection(Display.getDisplay(this));
conn.start();
}
}
}
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import java.io.*;
public class AvetanaConnection extends Thread{
private Display display=null;
private Connection conn = null;
private InputStream is = null;
private OutputStream os = null;
ClientSession con = null;
HeaderSet hdr = null;
public AvetanaConnection(Display d){
this.display = d;
}
public void run(){
try{
Connection conn = Connector.open("btgoep://0009DD104AB2:1;authenticate=false;master=false;encrypt=false");
Form f = new Form("nuovo form");
f.append("we");
display.setCurrent(f);
}
catch(IOException e){
Form f = new Form("form errore");
f.append("errore");
display.setCurrent(f);}
}
}
Sul cellulare ovviamente girerà una midlet.
Il programma Java per il pc l'ho gia fatto:
import java.io.IOException;
public class ServerTest {
public static void main(String[] args){
FileServerx f=new FileServerx();
try {
f.action();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import javax.microedition.io.*;
import java.io.*;
import javax.bluetooth.*;
import javax.obex.*;
public class FileServerx extends ServerRequestHandler
//implements ActionListener{
{
public FileServerx(){}
//Bluetooth Service name
private static final String myServiceName = "MyBtService";
// Bluetooth Service UUID of interest
private static final String myServiceUUID = "2d26618601fb47c28d9f10b8ec891363";
private UUID MYSERVICEUUID_UUID = new UUID(myServiceUUID, false);
public void action() throws IOException {
// Define the server connection URL
String url = "btgoep://localhost:" +MYSERVICEUUID_UUID.toString()+";name=FTP;authenticate=false;master=false;encrypt=false";
//String connURL = "btspp://localhost:"+MYSERVICEUUID_UUID.toString()+";name="+myServiceName+";";
// Create a server connection (a notifier)
//StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(connURL);
SessionNotifier scn = (SessionNotifier)Connector.open(url);
// Accept a new client connection
System.out.println("[server:] Now waiting for a client to connect");
//StreamConnection sc = scn.acceptAndOpen();
//StreamConnection conn = scn.acceptAndOpen();
scn.acceptAndOpen(this);
System.out.println("[server:] The client has created an OBEX session");
// RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);
//System.out.println("New client connection... " +
// rd.getFriendlyName(false));
/*
try {
java.io.InputStream is = conn.openInputStream();
System.out.println("Got data bytes " + is.available() + " name "
+ conn.getReceivedHeaders().getHeader(HeaderSet.NAME) +
" type " + conn.getType());
File f =
new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
FileOutputStream fos = new FileOutputStream (f);
byte b[] = new byte[1000];
int len;
while (is.available() > 0 && (len = is.read(b)) > 0) {
fos.write (b, 0, len);
}
fos.close();
System.out.println("[server:] Wrote data to " + f.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
//return ResponseCodes.OBEX_HTTP_OK;
//}*/
}
public int onConnect(HeaderSet request, HeaderSet reply) {
System.out.println("[server:] The client has created an OBEX session");
return ResponseCodes.OBEX_HTTP_OK;
}
public void onDisconnect (HeaderSet req, HeaderSet resp) {
System.out.println("[server:] The client has disconnected the OBEX session");
}
public int onPut (Operation op) {
try {
java.io.InputStream is = op.openInputStream();
System.out.println("Got data bytes " + is.available() + " name "
+ op.getReceivedHeaders().getHeader(HeaderSet.NAME) +
" type " + op.getType());
File f =
new File((String)op.getReceivedHeaders().getHeader(HeaderSet.NAME));
FileOutputStream fos = new FileOutputStream (f);
byte b[] = new byte[1000];
int len;
while (is.available() > 0 && (len = is.read(b)) > 0) {
fos.write (b, 0, len);
}
fos.close();
System.out.println("[server:] Wrote data to " + f.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
return ResponseCodes.OBEX_HTTP_OK;
}
}
Per quanto riguarda la midlet ho solo fatto una semplice connessione ma ovviamente mi da un errore, ed è qui che non capisco il perchè:
Ecco il codice della midlet:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class ClientMidlet extends MIDlet implements CommandListener{
public Display display;
public Form form;
public Command connetti;
public void startApp(){
display = Display.getDisplay(this);
form = new Form("Client Application");
connetti = new Command("connetti", Command.ITEM, 0);
form.addCommand(connetti);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp(){
notifyPaused();
}
public void destroyApp(boolean b){
notifyDestroyed();
}
public void commandAction(Command comando, Displayable display){
if(comando==connetti){
AvetanaConnection conn = new AvetanaConnection(Display.getDisplay(this));
conn.start();
}
}
}
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import java.io.*;
public class AvetanaConnection extends Thread{
private Display display=null;
private Connection conn = null;
private InputStream is = null;
private OutputStream os = null;
ClientSession con = null;
HeaderSet hdr = null;
public AvetanaConnection(Display d){
this.display = d;
}
public void run(){
try{
Connection conn = Connector.open("btgoep://0009DD104AB2:1;authenticate=false;master=false;encrypt=false");
Form f = new Form("nuovo form");
f.append("we");
display.setCurrent(f);
}
catch(IOException e){
Form f = new Form("form errore");
f.append("errore");
display.setCurrent(f);}
}
}