Android??????TCP??UDP????
???????????? ???????[ 2015/3/18 15:05:32 ] ????????Android TCP UDP ????
????????????????????????????????????????????
????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();
????}
????}
?????????????????????????????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11