This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Can not connect server socket over internet Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Can not connect server socket over internet" Watch "Can not connect server socket over internet" New topic
Author

Can not connect server socket over internet

A Bu
Greenhorn

Joined: Sep 10, 2011
Posts: 1

Hi,

I have a server PC and mobile client. I tried to send message to this server but server didn't receive this message when I used ip adress. It works fine on emulatır when I use "localhost" or "127.0.0.1" but it doesn't work on emulator when I use IP address. What should I do?

Here is server code

---

import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args)throws IOException{
ServerSocket providerSocket;
providerSocket=new ServerSocket(9002);
Socket s1=providerSocket.accept(); // Wait and accept a connection
// Get a communication stream associated with the socket
OutputStream s1out = s1.getOutputStream();
InputStream is=s1.getInputStream();
DataOutputStream dos = new DataOutputStream (s1out);
DataInputStream dis=new DataInputStream(is);
// Send a string!
dos.writeUTF("Hi there");
// Close the connection, but not the server socket
System.out.println(dis.readUTF());
dos.close();
s1out.close();
s1.close();
}
}

Here is client code

----

import javax.microedition.midlet.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.lcdui.*;
/**
* @author Oguz
*/
public class Client extends MIDlet {
Display display;
Form form = new Form("Deneme");
static final Command sendCommand =new Command("Send", Command.SCREEN, 0);
static final Command exitCommand =new Command("Exit", Command.EXIT, 0);
DataOutputStream dos=null;
StringItem si=new StringItem("Metin","");
public Client(){
display = Display.getDisplay(this);
form.addCommand(sendCommand);
form.addCommand(exitCommand);
form.append(si);
}
public void startApp() {
display.setCurrent(form);
try{
SocketConnection sc = (SocketConnection)Connector.open("socket://78.176.4.37:9002");
DataInputStream dis = null;
try{
dis = sc.openDataInputStream();
dos=sc.openDataOutputStream();
String st = new String (dis.readUTF());
System.out.println(st);
si.setText(st);
dos.writeUTF("Selam");
} finally{
sc.close();
dos.close();
dis.close();
}
} catch (IOException x){
x.printStackTrace();
}

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c,Displayable d) {
if(c==exitCommand){
destroyApp(true);
notifyDestroyed();
}
}
}

And client side throws this exception when I use ip address but it works fine when I use "localhost" or "127.0.0.1"

javax.microedition.io.ConnectionNotFoundException: ConnectionNotFound error in socket:pen : error = 10061\n
at com.sun.midp.io.j2me.socket.Protocol.open0(), bci=0
at com.sun.midp.io.j2me.socket.Protocol.connect(), bci=124
at com.sun.midp.io.j2me.socket.Protocol.open(), bci=125
at com.sun.midp.io.j2me.socket.Protocol.openPrim(), bci=4
at javax.microedition.io.Connector.openPrim(), bci=327
at javax.microedition.io.Connector.open(), bci=3
at javax.microedition.io.Connector.open(), bci=3
at javax.microedition.io.Connector.open(), bci=2
at Client.startApp(Client.java:29)
at javax.microedition.midlet.MIDletTunnelImpl.callStartApp(), bci=1
at com.sun.midp.midlet.MIDletPeer.startApp(), bci=7
at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=269
at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26
javacall_lifecycle_state_changed() lifecycle: event is JAVACALL_LIFECYCLE_MIDLET_SHUTDOWNstatus is JAVACALL_OK
Coloring the background og label › ..
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Can not connect server socket over internet
 
Similar Threads
How to read text file in memory card from MIDlet?
Client and Server Sockets with Threads
How to Change the File Name for Each Uploaded Files to the Socket Server?
A j2me program for bandwidth test
import javax.servlet.*; problem