After asking so many question about android socket programming and getting valuable answers from stackoverflow’s members i could do a well working program using sockets to connect two device over wifi.
Thanks to all.
But still i am having some problem..
i have done the program in which
** Data can be sent from client and received at serverSocket**
But still i am not getting how to Send data from server which can be received at client
Code for Server Socket
private OnClickListener bt_sendListner = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String msg=et_msg.getText().toString();
Log.d("Msg", msg);
Thread threadsendmsg = new Thread(new Threadsendmsg(msg));
threadsendmsg.start();
}
};
public class Threadsendmsg implements Runnable{
String msg;
public Threadsendmsg(String msg) {
// TODO Auto-generated constructor stub
this.msg=msg;
}
public void run() {
// TODO Auto-generated method stub
try {
Looper.prepare();
Log.d("Msg", "Inside the thread");
//connected = true;
while (true) {
try {
Log.d("Msg", "Msg to be sent");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(serverSocket.accept().getOutputStream())), true);
// where you issue the commands
out.println("Client: "+msg);
Log.d("Msg", "Msg sent"+out.toString());
break;
} catch (final Exception e) {
handler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
tv_chatbox.setText("S: Error= "+ e.getMessage());
Log.d("Msg", e.getMessage());
}
});
}
}
// socket.close();
// console.append("\nC: Closed.");
} catch (final Exception e) {
handler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
tv_chatbox.setText("S: Error= "+ e.getMessage());
Log.d("Msg", e.getMessage());
// TODO Auto-generated method stub
// console.append("\nC: Error= "+ e.getMessage());
}
});
// connected = false;
}
}
}
public class ServerThread implements Runnable {
public void run() {
try {
Looper.prepare();
if (SERVERIP != null) {
handler.post(new Runnable() {
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString()
, Toast.LENGTH_LONG).show();
serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString());
}
});
Toast.makeText(getApplicationContext(), serverSocket.getLocalSocketAddress().toString()
, Toast.LENGTH_LONG).show();
serverStatus.append("\n"+serverSocket.getLocalSocketAddress().toString());
while (true) {
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable() {
public void run() {
serverStatus.setText("Connected.");
}
});
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
final String myline=new String(line);
handler.post(new Runnable() {
public void run() {
tv_chatbox.setText("Client said:="+myline);
// do whatever you want to the front end
// this is where you can be creative
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (final Exception e) {
handler.post(new Runnable() {
public void run() {
serverStatus.setText("Error"+e.getMessage());
}
});
e.printStackTrace();
}
}
}
*There is no method as ServerSocket.getOutputStream() in ServerSocket class.*Which i have used client socket…
Both client and server use the same class Socket. But client creates his socket instance manually and connects to server. Server on the other hand listens at some port and when client connects, socket for server is created and returned from method accept().
In your code you can use