????????????????????????????????????????????
????TCP???????????
????try{
????Boolean endFlag=false;
????ServerSocket ss=new ServerSocket(12345);
????while(!endFlag){
????//????????????
????Socket s=ss.accept();
????BufferedReader input=new BufferedReader(newInputStreamReader(s.getInputStream()));
????//??????????????true???????flush???????????????????output.flush()
????PrintWriter output=newPrintWriter(s.getOutputStream()??true);
????String message=input.readLine();
????Log.d("Tcp Demo"??"message from Client:"+message);
????output.println("message received!");
????//output.flush();
????if("shutDown".equals(message)){
????endFlag=true;
????}
????s.close();
????}
????ss.close();
????}catch(UnknownHostException e){
????e.printStackTrace();
????}catch(IOException e){
????e.printStackTrace();
????}
????TCP????????
????try{
????Socket s=new Socket("localhost"??12345);
????//outgoing stream redirect to socket
????OutputStream out=s.getOutputStream();
????//??????????????true???????flush???????????????????out.flush()
????PrintWriter output=new PrintWriter(out??true);
????output.println("Hello IdeasAndroid!");
????BufferedReader input=new BufferedReader(newInputStreamReader(s
????.getInputStream()));
????//read line(s)
????String message=input.readLine();
????Log.d("Tcp Demo"??"message From Server:"+message);
????s.close();
????}catch(UnknownHostException e){
????e.printStackTrace();
????}catch(IOException e){
????e.printStackTrace();
????}
???????????????UDP??


????UDP???????

????UDP???????????
????//UDP??????????????
????Integer port=12345;
????//?????????С??????????????????????????С
????byte[]message=new byte[1024];
????try{
????//????Socket????
????DatagramSocket datagramSocket=new DatagramSocket(port);
????DatagramPacket datagramPacket=new DatagramPacket(message??
????message.length);
????try{
????while(true){
????//???????????
????datagramSocket.receive(datagramPacket);
????Log.d("UDP Demo"??datagramPacket.getAddress()
????.getHostAddress().toString()
????+":"+new String(datagramPacket.getData()));
????}
????}catch(IOException e){
????e.printStackTrace();
????}
????}catch(SocketException e){
????e.printStackTrace();
????}
????UDP????????
????public static void send(String message){
????message=(message==null?"Hello IdeasAndroid!":message);
????int server_port=12345;
????DatagramSocket s=null;
????try{
????s=new DatagramSocket();
????}catch(SocketException e){
????e.printStackTrace();
????}
????InetAddress local=null;
????try{
????//???????????IP
????local=InetAddress.getByName("localhost");
????}catch(UnknownHostException e){
????e.printStackTrace();
????}
????int msg_length=message.length();
????byte[]messagemessageByte=message.getBytes();
????DatagramPacket p=new DatagramPacket(messageByte??msg_length??local??
????server_port);
????try{
????s.send(p);
????}catch(IOException e){
????e.printStackTrace();
????}
????}
?????????????????????????????????????????????????