Socket programing

Quickly share your Android Code Snippets here...

Socket programing

Postby nomi » Wed Feb 13, 2013 4:38 am

I want to create the android application in which i send the data from desktop application to android device using wifi devices. I am not confirmed but i think that it can be possible with the UDP socket programming. so please help me with code.
nomi
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 13, 2013 4:34 am

Top

Re: Socket programing

Postby christusvalerian » Fri Feb 22, 2013 7:08 am

public class AndroidClient extends Activity {

EditText textOut;
TextView textIn;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}

Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;

try {
socket = new Socket("192.168.1.101", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}};
}

This code help You for Socket programming
http://android-er.blogspot.in/2011/01/s ... using.html

Regards
Android developer
Trinay Technology Solutions
www.trinaytech.com
5705750475
christusvalerian
Developer
Developer
 
Posts: 28
Joined: Sat Jun 18, 2011 8:03 am


Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 2 guests