I'm starting to learn MIDlet and I need a little help with a project. I have a MIDlet which would be a client. This client has to connect to a database - it will send some information to it and also receive an answer back. I have to use Socket for the connection. My problem is that the Server won't see the Client connected even though I use the same port.
So here is the code for the server:
public class Server {
public static void main(String[] args) {
try{
while(true){
ServerSocket ss=new ServerSocket(5000);
System.out.println("Server started...");
Socket s=ss.accept();
s.getReceiveBufferSize();
System.out.println("Client connected");
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
String mesaj="";
while(!mesaj.equals("end")){
mesaj=in.readLine();
System.out.println(mesaj);
}
}
}catch(Exception e){e.printStackTrace();}
}
}
The Client:
public class Client extends MIDlet implements CommandListener,Runnable{
private boolean isPaused;
private static Display display;
private Form f;
private StringItem si;
private TextField tf1;
private TextField tf2;
private boolean stop;
private Command sendCommand = new Command("Send", Command.ITEM, 0);
private Command exitCommand = new Command("Exit", Command.EXIT, 0);
InputStream is;
OutputStream os;
SocketConnection sc;
private Client client;
//Sender sender;
public Client(){
display = Display.getDisplay(this);
f = new Form("Socket Client");
si = new StringItem("Comanda:", " ");
tf1 = new TextField("Nr zbor:", "", 30, TextField.ANY);
tf2 = new TextField("Nr bilete:","",30,TextField.ANY);
f.append(si);
f.append(tf1);
f.append(tf2);
f.addCommand(exitCommand);
f.addCommand(sendCommand);
f.setCommandListener(this);
display.setCurrent(f);
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
sc = (SocketConnection)Connector.open("socket://localhost:5000");
si.setText("Connected to server");
is = sc.openInputStream();
os = sc.openOutputStream();
I didn't put the rest of the code cause it's long and the connection is the only problem I have. I need the server to recognize the client. Don't tell me to use Http or what else there is cause it's school assignment and I have to use sockets.
Thanks in advance!