Friday, 6 September 2013

Send and Receive Message Through WiFi / Socet in Android

Send and Receive Message Through WiFi / Socet in Android

I am creating a basic WiFi messanger application for internal purpose. In
my Async Class I have following code
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import android.os.AsyncTask;
import android.util.Log;
public class Threading extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
String check = "";
//Create client socket
Socket s = new Socket("192.168.0.100", 888);
//to send data to the server
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//to read data coming from the server
BufferedReader br = new BufferedReader(new
InputStreamReader(s.getInputStream()));
//to read data from the key board
//BufferedReader kb = new BufferedReader(new
InputStreamReader(System.in));
String str="",str1="";
if(br.readLine() == null) {
check = "1";
}
else {
check = br.readLine();
}
//repeat as long as exit is not typed at client
while(!(str = check).equals("exit"))
{
str1 = br.readLine(); //receive from server
dos.writeBytes("R : "+str1+" \n"); //send to server
System.out.println(str1);
}
//close connection.
dos.close();
br.close();
//kb.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("SERVER ERROR","SERVER IS DOWN");
e.printStackTrace();
} catch (NullPointerException e) {
Log.e("NULL POINTER","ERROR");
e.printStackTrace();
}
return null;
}
}
In the main class, I have created a button click event which execute this
method. The code successfully works to receive the message from the
another client. but not able to send the messages to another one. ! Can
any one help me? :( to solve this ?

No comments:

Post a Comment