In my last post we saw how to use a file system to store and retrieve information in some of the Siemens modems. I think that it was quite interesting. This time we will look at something that I think is even more interesting and the truth is that there aren’t many examples out there (I haven’t found any). Therefore I think this post will be widely read.
Well as I do regularly, I use the Google server’s IP to do tests. What you see in the Java program below is how to make a TCP/IP connection to a server with the IP address 216.239.59.147 and port 80 (HTTP port). In this program, as well as making a TCP/IP connection, once the connection is established (asked on the homepage) data is sent to the server through the socket and data (HTML data) is also received at the socket too. It’s very simple but I think that it illustrates the mechanics well and it could be useful for you at some point.
Below you have an example of a program. I’ve written enough comments so that you can follow it easily but let me know if you want to get in contact with me. So there you go, I’ve made you a cable.

package src;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import com.siemens.icm.io.ATCommand;
import com.siemens.icm.io.ATCommandFailedException;
import com.siemens.icm.io.ATCommandListener;
public class Connection extends MIDlet
{
ATCommand ATC;
ATCommandListener LIS;
int dataReceived=0;
//Class constructor
public Connection()
{
//We initiate the ATCommand class and the Listener
try
{
LIS = new ATListener();
ATC=new ATCommand(false);
ATC.addListener(LIS);
}
catch (IllegalStateException e){e.printStackTrace();}
catch (ATCommandFailedException e) {e.printStackTrace();}
//We initiate the TCP connection
initiateConnection();
}
//We definte the ATListener class
class ATListener implements ATCommandListener
{
//We will only control one URC in this example
//and it will be to know what we receive data
public void ATEvent(String Event)
{
//We remove the event that occurred:
System.out.println(“Event: “+ Event);
//If the URC corresponds with the data that has come …
if (Event.indexOf(“SISR”)>0)
{
//We send data from the socket to read
System.out.println(“Read TCP data”);
readSocket();
}
}
public void RINGChanged(boolean Event) {}
public void DCDChanged(boolean Event) {}
public void DSRChanged(boolean Event) {}
public void CONNChanged(boolean Event) {}
}
protected void startApp() throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void destroyApp(boolean arg0) throws
MIDletStateChangeException {}
//Method for connecting to remote server
public void initiateConnection()
{
//Connect to Google
IPaddress string = “216.239.59.147″;
Port string = “80″;
R string=”";
System.out.println(“Initiating GPRS connection sequence”);
try
{
r=ATC.send(“AT^SICS=1,conType,GPRS0\r”);
Thread.sleep(100);
r=ATC.send(“at^sics=1,user,CLIENTE\r”);
Thread.sleep(100);
r=ATC.send(“at^sics=1,passwd,AMENA\r”);
Thread.sleep(100);
r=ATC.send(“at^sics=1,apn,internet\r”);
Thread.sleep(100);
r=ATC.send(“AT^SICI=1\r”);
Thread.sleep(100);
r=ATC.send(“AT^SISS=1,srvType,socket\r”);
Thread.sleep(100);
r=ATC.send(“AT^SISS=1,conId,1\r”);
Thread.sleep(100);
r=ATC.send(“AT^SISS=1,address,socktcp://” + IPaddress + “:” + port + “\r”);
Thread.sleep(100);
r=ATC.send(“AT^SISO=1\r”);
System.out.println(r);
Thread.sleep(3000);
System.out.println(“Sending data …”);
//We send a request to Google’s homepage
writeSocket(“GET / HTTP/1.1\r\n\r\n”);
System.out.println(“Data sent”);
//We keep waiting for something to be received via TCP
System.out.println(“Waiting to receive a TCP data package…”);
while (receivedData==0);
//Close the socket
r=ATC.send(“AT^SISC=1\r”);
Thread.sleep(100);
//Stop the program
notifyDestroyed();
}
catch (Exception e){System.out.println(“Error.”);}
}
//Method for reading data from the socket
public void readSocket()
{
try
{
//We send the AT command to read whatever there is in the socket..
ATC.send(“AT^SISR=1,1500\r”);
Thread.sleep(1000);
//We associate the input data stream
InputStream is = ATC.getDataInputStream();
//If there is data available we read it …
if (is.available() > -1)
{
//We create a buffer that is the size of the data received
byte[] buffer = new byte[is.available()];
//We read the data and load it into the buffer
is.read(buffer, 0, is.available());
//We show the data through the standard output …
System.out.println(new String(buffer));
//We activate the flag so that we are not still waiting
receivedData=1;
}
//Managing exceptions
} catch (IllegalStateException e) {e.printStackTrace();
} catch (IllegalArgumentException e) {e.printStackTrace();
} catch (ATCommandFailedException e) {e.printStackTrace();
} catch (InterruptedException e) {e.printStackTrace();
} catch (IOException e) {e.printStackTrace();}
}
//Method for writing data in the socket
public void writeSocket(String)
{
OutputStream os;
try
{
System.out.println(“Sent: ” + string);
//We associate the output stream to the ATCommand class
os = ATC.getDataOutputStream();
//We send the sending command AT^SISW
ATC.send(“AT^SISW=1,” + string.length() + “,0,0\r”);
Thread.sleep(500);
//We write the data through the socket
os.write(string.getBytes());
Thread.sleep(500);
}
//Managing exceptions
catch (IOException e) {e.printStackTrace();}
catch (IllegalStateException e) {e.printStackTrace();}
catch (IllegalArgumentException e) {e.printStackTrace();}
catch (ATCommandFailedException e) {e.printStackTrace();}
catch (InterruptedException e) {e.printStackTrace();}
}
}