Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can not connect server socket over internet

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 › ..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic