I'm trying to develop an app for instant messaging (like whatsapp). I'm very confused about what type of connection i should use: socket, http or other? Do i have to use non-standard java API? For server i use java and for client i use andoid (java). I have already tried socket connection but without success. I don't get error but nothing happens because the device and the server are not connected(i don't know why).
This is my client class (android) that sends messages:
public class SendMsg implements Runnable {
private Socket socket;
String msg;
public SendMsg(String msg){
this.msg=msg;
}
@Override
public void run() {
try {
socket = new Socket("ip_globale_server",5000);
socket.setSoTimeout(5000);
BufferedWriter writer= new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write(msg);
writer.flush();
}
catch (IOException e){
System.err.println("Connection error");
}
finally{
if (socket!=null) {
try{socket.close();}
catch(IOException e){}
}
}
}
}
this is my java server class that waits for connection:
public class ReceiveMsg implements Runnable {
ServerSocket server= null;
Socket connection=null;
InputStream in;
InputStreamReader inr;
StringBuilder smsg;
public void run() {
System.out.print("Server has started");
try {
server = new ServerSocket(5000);
System.out.println("I'm waiting for connection");
connection = server.accept();
System.out.println("I'm connected with "+ connection.toString());
in = connection.getInputStream();
inr = new InputStreamReader(in,"ASCII");
for (int c= inr.read();c!=-1; c= inr.read()){
smsg.append((char)c);
}
//this class is for storing the message
new Store (smsg.toString(),1,2);
}
catch(IOException ex){}
finally {
try{
if (server != null)
server.close();
if (server != null)
server.close();
}
catch(IOException e){
}
}
Could you help me to get start please. Thank you in advance.