aspose file tools*
The moose likes Java in General and the fly likes J2se server and J2me client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "J2se server and J2me client" Watch "J2se server and J2me client" New topic
Author

J2se server and J2me client

jawad arshad
Greenhorn

Joined: Jan 04, 2009
Posts: 1
Hi !
I am making an application for wireless communication between a pc and mobile.
Kindlylet me know that which api I have to use at J2se side.

Thanks for help.
Regards
Rana Jawad
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Check out javax.microedition.io.SocketConnection on the client side and java.net.ServerSocket on the server side.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
feri guretno
Greenhorn

Joined: Jan 12, 2009
Posts: 7
Hi, I'm also doing a project in j2me j2se wireless connection using N96.

I'm trying your suggestion using socket server connection and it works well in simulator (server prints:"Hello from socket").However it didn't when i try it from my N96.Here is the code I'm using:

------------------------------------------------------------------------------
J2ME client:

try{
SocketConnection sc = (SocketConnection)
Connector.open("socket://10.217.141.150:4444");
StringBuffer sb = new StringBuffer();
OutputStream os = null;

os = sc.openOutputStream();
byte[] data = "Hello from a socket!".getBytes();
os.write(data);

os.close();
sc.close();

// }
} catch (IOException x){
x.printStackTrace();
}


J2SE server:
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}

// BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
PrintWriter out = new PrintWriter(
clientSocket.getOutputStream(), true);

BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
StringBuffer sb = new StringBuffer();
int ch = 0;

while ((ch = in.read()) != -1){
sb.append((char)ch);

//
}
in.close();

clientSocket.close();
serverSocket.close();

---------------------------------------------------------------------
PLease help if anybody know the solution.Thanks!

Best Regards,
feri guretno
Greenhorn

Joined: Jan 12, 2009
Posts: 7
Hi,

Besides, I also try using http connection instead of server socket connection as in http://developers.sun.com/mobility/midp/articles/tutorial2/

Again, it works well in simulator but when I deploy it to N96 i'm getting java.io.IOexception:-33 ( I've changed the url from http://localhost:8080/midp/hits into http://10.217.141.150:8080/midp/hits)

However it can display the html scripts response when i change the url into www.google.com or www.yahoo.com.

Is it the problem with the url?

Thanks for your help in advance!

Best Regards,
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1451
    
    6

I have faced the similar kind of problems with nokia devices , try to encode the url and see if it works

your url

http://10.217.141.150:8080/midp/hits

encoded url

http://10.217.141.150%3A8080/midp/hits


Swastik
feri guretno
Greenhorn

Joined: Jan 12, 2009
Posts: 7
Hi,

thanks for the reply.

I've tried using the encoded url in the http connection and it still produce the same exception.

I also tried using http://10.217.141.150%3A4444 for the socket connection and it get "system error, application will be closed" . previously, using http://10.217.141.150:4444 , i get java.io.IOException:SymbianOS error=-191:Internet:Unable to connect to server.

any other solution?

:roll:

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1451
    
    6

I have worked with http and https connection with encoded url and it has worked fine. Not very sure about socket connection.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

You should not have to encode the ":" that separates the server address from the port in a URL. In fact, I'm surprised that it works.

The problem is that when using a mobile device to access a 10.x.x.x IP address, the device is attempting to locate a non-public IP address via the public internet gateway provided by the phone service provider.

The private-use IP addresses are reserved for use outside the public Internet, and unlike the public IP addresses, they are not unique. I have some 10.x.x.x addresses in use on my own network used for various virtual machines to communicate with each other. These addresses cannot be see by anyone outside my LAN.


Customer surveys are for companies who didn't pay proper attention to begin with.
feri guretno
Greenhorn

Joined: Jan 12, 2009
Posts: 7
Hi, thanks for the reply

FYI, I connect to the wireless in offline mode, with no SIM card inserted. So without the phone service provider, how do the phone know it'spublic internet gateway?is it hardcoded in the phone itself?

And i connect using a wireless router inside my own network, so i thought it should be able to access a non-public ip address( 10.x.x.x) just like if we do it through PC with LAN connection.

So does it mean that the j2se server have to be made to public ip address ( can be accessed outside LAN network) to enable the j2me client to connect to it through http or socket connetion?

Best Regards,
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

If you don't have a SIM card installed, your phone is offline. so it can't access any part of the Internet through the phone service provider.

But it sounds like you have a smartphone with WiFi, USB, and/or Bluetooth networking.

Each of those networks has its own network adapter, and each network adapter has its own IP address. Usually, that address is assigned by a DHCP server accessed from whatever gateway device the adapter is communicating with.

This is no different than a desktop unit with multiple NICs in it, or a laptop with both ethernet and WiFi in operation. The core network software includes routing tables that are used to select an interface to communicate.

Yes, if you have a WiFi, you'll be able to access whatever parts of the network any device connecting through that WiFi access point can access.

Of course, if you don't have the WiFi turned on and connected to the access point, the phone won't route, any more that a PC can route if the cable isn't connected.

That's just the basics. If you have paranoid network engineers, they might be firewalling the particular nodes and ports you want to talk to. In that case, you have to convince them to open up the network for you.
feri guretno
Greenhorn

Joined: Jan 12, 2009
Posts: 7
That's just the basics. If you have paranoid network engineers, they might be firewalling the particular nodes and ports you want to talk to. In that case, you have to convince them to open up the network for you.


I set up a tomcat server in my pc as in http://developers.sun.com/mobility/midp/articles/tutorial2/ .It can be accesses from other computer through LAN connection by accessing http://10.217.141.150:8080. However it cannot be accesses from mobile phone. So by saying "to open up the network for you", do you mean to open the port so that it can be accessed from outside LAN network?

because there is a server with address 192.x.x.x which the port have been opened to be accessed from outside LAN network, and I can connect to that server using the mobile phone.

Thanks in advance!!

Best Regards,
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

Port 8080 may be blocked, either on the server machine or in one of the routers between your mobile and the server machine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: J2se server and J2me client