mardi 24 mars 2015

sending two different messages on click off Togglebutton


I have a ToggleButton. When that button is clicked once, the Byte[] containing the message {(byte)0x01,(byte)0x02} has to be sent to server and when the same button is clicked again the Byte[] containing the msg {(byte)0xFE,(byte)0x01} has to be sent to the server. My client code is like this.


Public void onClick(View v) {


if(v.getId()==R.id.btncon) {



if(con.isChecked())
{

new Thread(new Runnable(){

public void run()
{

try{

InetAddress in=InetAddress.getByName(host);
s=new Socket(in,port);
PrintStream pw=new PrintStream(s.getOutputStream());
pw.println(message);

InputStream inp=new InputStream(s.getInputStream());
BufferedReader br=new BufferedReader(inp);
String user=br.readline();
Log.d("home",user);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}).start();


}


else {


try{



PrintStream pw1=new PrintStream(s.getOutputStream());

pw.Println(message1);

s.close();

}


catch(IOException e1)



{

e1.printStackTrace();

}}}}


my server code is


public void run() throws Exception {


String MESSAGE;

ServerSocket ss=null; PrintStream pw=null;



try
{
ss=new ServerSocket(8002);
}


catch(IOException e1) { e1.printStackTrace(); }


while(true) { try {



sock=ss.accept();

in=new InputStreamReader(sock.getInputStream());

BufferedReader br=new BufferedReader(in);

MESSAGE=br.readLine();

System.out.println("client sent:"+MESSAGE);

if(MESSAGE!=null)
{

pw=new PrintStream(sock.getOutputStream());

pw.println("server received the message"+":"+ MESSAGE);

pw.flush();

}}


catch(IOException e) {



e.printStackTrace();


}


finally


{


in.close();


pw.close();


sock.close();


}}}


When message is send from the client, when server recieves and prints the value i am getting a different value.I want the output to be displayed as it is sent. please help.Thanks in advance.


A code for both server and client would be helpful





Aucun commentaire:

Enregistrer un commentaire