×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    15

    Send multiple message from client to server

    Hi all.

    I have a problem with reconnection from client to server.
    I have a midlet that implements a client. This client sends a message to the server. Server accepts one connection at time.
    When client send the message, my application close the connection and the streams at the server side and at the client side too.
    If I want to send a new message to server from the same client, an IOException occurs. I repeat that before a new connection from the same client, i close connection and streams.
    I think the problem is in the client beacause if i run from WTK a new client, this new client can send a message!.
    Please help me!!!!!
    I paste my simple code below:
    ----
    ServerMIDlet.java
    ----
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import java.util.Vector;
    import javax.microedition.io.*;
    import javax.bluetooth.*;

    public class ServerMIDlet extends MIDlet
    {
    public Display display;
    public Form addressForm;
    public LocalDevice ld=null;
    public DiscoveryAgent da=null;

    public String connectionURL = null;
    private UUID[] uuids;
    private String serv="8421";
    private int[] attrSet = {0,3,4,0x100};
    public String serviceURL ;

    public ServerMIDlet()
    {
    display=Display.getDisplay(this);
    addressForm=new Form("BT MultiHop");

    try
    {
    ld=LocalDevice.getLocalDevice();
    ld.setDiscoverable(DiscoveryAgent
    }
    catch(BluetoothStateException e){}

    uuids = new UUID[1];
    uuids[0] = new UUID(serv,true);
    serviceURL = "btspp://localhost:"+uuids[0].toString()+";name=BTServer";

    }

    public void startApp()
    {
    display.setCurrent(addressForm);
    new BTServer(this);
    }
    public void pauseApp()
    {
    }
    public void destroyApp(boolean b)
    {
    notifyDestroyed();
    }

    }

    -----
    BTServer.java
    ------
    import javax.bluetooth.*;
    import java.util.Vector;
    import javax.microedition.io.*;
    import java.io.*;


    public class BTServer extends Thread
    {
    private StreamConnection con=null;
    private StreamConnectionNotifier service= null;
    private DataInputStream datainputstream=null;
    private ServerMIDlet midlet=null;

    public BTServer(ServerMIDlet m)
    {
    midlet=m;
    this.start();
    }

    public void run()
    {
    activeServer();
    }

    public void activeServer()
    {
    String str="";
    int counter=0;

    //I listen to only 10 connections
    while(counter<10)
    {
    str=acceptConnection();
    counter++;
    System.out.println("Recevied message:"+str);
    }

    }

    public String acceptConnection()
    {
    String s="";

    try
    {
    service = (StreamConnectionNotifier)Connector.open(midlet.serviceURL);
    midlet.addressForm.append("\nListen..");
    con = service.acceptAndOpen();
    datainputstream = con.openDataInputStream();
    midlet.addressForm.append("\n\nConnection accepted");

    s=datainputstream.readUTF();
    datainputstream.close();
    con.close();
    service.close();

    }//close "try"
    catch(IOException e)
    {
    System.out.println(e);
    }
    return s;
    }

    }
    ----
    import java.io.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.*;
    import java.util.Vector;
    import javax.microedition.io.*;
    import javax.bluetooth.*;


    public class ClientMIDlet extends MIDlet implements CommandListener,DiscoveryListener
    {
    public Display display;
    public List elenco;
    private Command back,send;
    public Form addressForm;
    public LocalDevice ld=null;
    public DiscoveryAgent da=null;

    public Vector device=null;
    public String connectionURL = null;
    public ValidURL destinatari=null;
    public String selected_device=null;

    public String my_address=null;
    public String my_name=null;

    private UUID[] uuids;
    private int[] attrSet = {0,3,4,0x100};
    private String serv="8421";

    public ClientMIDlet()
    {
    display=Display.getDisplay(this);
    device=new Vector();
    addressForm=new Form("BT Client");
    elenco=new List("Dispositivi trovati",List.IMPLICIT);
    destinatari=new ValidURL();

    try
    {
    ld=LocalDevice.getLocalDevice();
    ld.setDiscoverable(DiscoveryAgent
    }
    catch(BluetoothStateException e){}

    my_address=ld.getBluetoothAddress();
    my_name=ld.getFriendlyName();
    uuids = new UUID[1];
    uuids[0] = new UUID(serv,true);
    elenco.setCommandListener(this);
    addressForm.setCommandListener(this);
    }

    public void startApp()
    {
    send=new Command("Send",Command.OK,0);
    back=new Command("Back",Command.BACK,0);
    addressForm.addCommand(back);
    addressForm.addCommand(send);
    display.setCurrent(addressForm);
    findDevice();
    }

    public void commandAction(Command c,Displayable d)
    {
    if(c==send)
    {
    elenco.addCommand(back);
    display.setCurrent(addressForm);
    display.setCurrent(elenco);

    elenco.deleteAll();
    for(int i=0;i<destinatari.sizeOf();i++)
    elenco.append("Device#"+i+":"+destinatari.getElementMAC(i),null);

    display.setCurrent(elenco);
    selected_device= destinatari.getElementMacAndBridge(0);
    new BTClient(this);
    }

    if(c==back)
    {
    if(d==addressForm)
    {
    display.setCurrent(elenco);
    }

    addressForm.addCommand(send);
    display.setCurrent(addressForm);
    }

    }
    public void pauseApp()
    {
    }
    public void destroyApp(boolean b)
    {
    notifyDestroyed();
    }

    public void findDevice()
    {
    stopSearching();
    searchForDevices();
    searchForServices();
    }

    public void stopSearching()
    {
    da.cancelInquiry(this);
    }
    public synchronized void searchForDevices()
    {

    elenco.append("WAIT...",null);
    try
    {
    da.startInquiry(DiscoveryAgent.GIAC,this);
    }
    catch(Exception e)
    {
    System.out.println("Error");
    }
    try
    {
    synchronized(this)
    {
    this.wait();
    }
    }
    catch(InterruptedException e)
    {
    System.out.println(e);
    }
    System.out.println("\nDevices found = " + device.size());
    }
    public synchronized void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
    {
    //System.out.println("New device");
    try
    {
    device.addElement(btDevice);
    }
    catch(Exception e)
    {
    System.out.println(e);
    }

    }
    public synchronized void inquiryCompleted(int discType)
    {
    //System.out.println("in inquiry completed");
    elenco.deleteAll();
    //elenco.removeCommand(search);
    elenco.addCommand(back);
    if(device.size()<=0)
    {
    elenco.append("Nessun dispositivo trovato",null);
    display.setCurrent(elenco);
    }

    this.notify();
    }
    public synchronized void searchForServices()
    {
    int transactionid=0;
    for(int i = 0; i< device.size();i++)
    {
    try
    {
    transactionid = da.searchServices(attrSet,uuids,(RemoteDevice)device.elementAt(i),this);
    }
    catch(BluetoothStateException e)
    {
    System.out.println("Errore nella ricerca dei servizi");
    }
    try
    {
    if(transactionid != -1)
    {
    synchronized(this)
    {
    this.wait();
    }
    }
    }
    catch(InterruptedException e)
    {
    System.out.println(e);
    }
    }// end of forloop
    }


    public synchronized void servicesDiscovered(int transID,ServiceRecord[] servRecords)
    {
    for(int i=0;i<servRecords.length;i++)
    {
    String servName = (String)((DataElement)servRecords[i].getAttributeValue(0x100)).getValue();
    //System.out.println("Service Name : "+servName);
    connectionURL = servRecords[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false);
    //System.out.println("Connection url :" + connectionURL);

    if(connectionURL.startsWith("btspp"))
    {
    destinatari.addURL(connectionURL);

    }

    if(connectionURL != null)
    {
    synchronized(this)
    {
    this.notify();
    }
    }
    else
    System.out.println("Nessun servizio disponibile...");
    }
    }

    public synchronized void serviceSearchCompleted(int transID, int respCode)
    {
    if(respCode==SERVICE_SEARCH_ERROR)
    System.out.println("\nSERVICE_SEARCH_ERROR\n");

    if(respCode==SERVICE_SEARCH_COMPLETED)
    System.out.println("\nSERVICE_SEARCH_COMPLETED\n");

    if(respCode==SERVICE_SEARCH_TERMINATED)
    System.out.println("\n SERVICE_SEARCH_TERMINATED\n");

    if(respCode == SERVICE_SEARCH_NO_RECORDS)
    {
    synchronized(this)
    {
    this.notify();
    }
    System.out.println("\n SERVICE_SEARCH_NO_RECORDS\n");
    }

    if(respCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
    {
    System.out.println("\n SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
    synchronized(this)
    {
    this.notify();
    }
    }
    }


    }
    ----
    BTClient.java
    ----
    import java.io.*;
    import javax.bluetooth.*;
    import java.util.Vector;
    import javax.microedition.io.*;

    public class BTClient extends Thread
    {

    private StreamConnection client_con=null;
    private DataOutputStream dataoutputstream=null;
    ClientMIDlet midlet=null;

    public BTClient(ClientMIDlet m)
    {
    midlet=m;
    this.start();
    }

    public void run()
    {
    sendData();
    }

    public void sendData()
    {
    String msg="TEST;

    String bridge_url=(midlet.selected_device.substring(12));

    startConnection(bridge_url);
    send2Stream(msg);
    }

    public void startConnection(String url)
    {
    //boolean exit_flag=true;
    System.out.println(midlet.my_address+" chiama la connessione con "+url);
    int counter=0;

    while(counter<5)
    {
    try
    {
    client_con=(StreamConnection)Connector.open(url);
    dataoutputstream = client_con.openDataOutputStream();
    System.out.println("Connection OK");
    //exit_flag=false;
    break;
    }

    catch(IOException e)
    {
    midlet.addressForm.append("Unni parrari cchiù ;-(");
    System.out.println("Errore apertura connessione: "+e);
    counter++;
    }
    }
    }

    public void send2Stream(String stringa)
    {
    byte b[]=new byte[stringa.length()];
    b=stringa.getBytes();

    try
    {
    for(int j=0;j<b.length;j++)
    dataoutputstream.write((int)b[j]);

    dataoutputstream.flush();
    dataoutputstream.write(1);
    dataoutputstream.flush();
    dataoutputstream.close();
    client_con.close();

    }
    catch(IOException e)
    {
    System.out.println (e);
    }
    }

    }

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    4,105

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    15

    Re: Send multiple message from client to server

    yeah! I believe this post is most clear than previous post. isn't it?

Similar Threads

  1. problem to send a service loading message push for 8310
    By alinesimon in forum General Messaging
    Replies: 1
    Last Post: 2002-05-06, 12:39
  2. Problem to send a service loading push message for 8310
    By alinesimon in forum General Messaging
    Replies: 1
    Last Post: 2002-05-06, 11:19
  3. Problem to send a service loadinfd push message for 8310
    By alinesimon in forum Smart Messaging
    Replies: 1
    Last Post: 2002-05-03, 10:49
  4. Is there a way that allow a wap server to send smart message
    By grant8411 in forum Smart Messaging
    Replies: 1
    Last Post: 1970-01-01, 02:00

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×