Skip to main content

help with mobile socket programming ????

3 replies [Last post]
viju2008
Offline
Joined: 2008-12-02
Points: 0

i am doing a project on GPRS communication

my project requires sending a string of data from PC to a mobile

the MOBILE service provider i use uses a WAP GATEWAY so i cannot connect to it

becoz the IP address of the mobile is hidden behind the gateway

so i changed it to that the mobile will connect to the PC
i am able to establish the connection and the string of data is going to PC
but the response i.e " data recieved"
should be sent but how do i send the repsone

plz tell me becoz i do not know the IP ADDRESS OF THE MOBILE ???

plz help me with code if u can becoz i am new to J2ME or plz explain me the logic atleast

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
viju2008
Offline
Joined: 2008-12-02
Points: 0

i ill try out and post the update here

sfitzjava
Offline
Joined: 2003-06-15
Points: 0

Your problem is just standard operations for the JavaME world.
But surprisingly the solution is not so JavaME specific. It comes down to standard Web based Java communication. So if you were to build a Applet or soon to come JavaFX desktop client solution you would make a HTTP call to the server and open the outputstream to write some data to the server or confirm response code, then open the inputstream and read the servers response.
Same for JavaME.

From the javadoc for HttpConnection in JavaME
void getViaHttpConnection(String url) throws IOException {
HttpConnection c = null;
InputStream is = null;
int rc;

try {
c = (HttpConnection)Connector.open(url);

// Getting the response code will open the connection,
// send the request, and read the HTTP response headers.
// The headers are stored until requested.
rc = c.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}

is = c.openInputStream();

// Get the ContentType
String type = c.getType();

// Get the length and process the data
int len = (int)c.getLength();
if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;
}
} else {
int ch;
while ((ch = is.read()) != -1) {
...
}
}
} catch (ClassCastException e) {
throw new IllegalArgumentException("Not an HTTP URL");
} finally {
if (is != null)
is.close();
if (c != null)
c.close();
}
}

What type of phone and what service provider, this can sometimes affect the process a little.

-Shawn

mspamrita
Offline
Joined: 2010-11-09
Points: 0

hi viju2008
I read your problem above and as you have mentioned that you connected mobile to the PC and established the connection and the string of data went to PC.

We are having problems with the same stuff and have no idea of networking.So can you Please provide the code for this.
hoping for a reply soon.
my email id is amrita_msp@student-partners.com