线程和socke


注意:网络通讯要开线程,收发各开一个


线程:

http://blog.csdn.net/leege100/article/details/37878575 

更新UI的线程:(异步)

http://www.cnblogs.com/zhaoyanjun/archive/2016/05/11/5483221.html 

http://blog.csdn.net/qwe19860/article/details/48519237 

线程锁:

http://blog.csdn.net/a992036795/article/details/51372272  

http://blog.csdn.net/dawanganban/article/details/29383783 



UDP:

http://www.cnblogs.com/pbq-dream/p/5366300.html 

http://blog.csdn.net/tianxuhong/article/details/45337103 


TCP:

tcp除非有心跳包,否则很难检测是否连接。所以客户端要可控地在服务端启动的时候连接。发送数据就无所谓了。

实时视频播放:(简单的)

http://blog.csdn.net/shenpibaipao/article/details/70176038

接受:http://www.cnblogs.com/lijiongquan/p/4729445.html 

发送:http://blog.csdn.net/liuhanhan512/article/details/8489077 


TCP动态更新图片:

http://www.cnblogs.com/lijiongquan/p/4729445.html 

http://www.cnblogs.com/android-blogs/p/5737596.html  

http://www.oschina.net/code/snippet_212511_37899?winzoom=1  

问题:

http://blog.csdn.net/maxwell_nc/article/details/49081105


:可以尝试 BitmapFactory.decodeStream(inputstream)转换为byte再转换为FILE再转换为bitmap

或者inputstream来先转换再为file再为bitmap





例程:UDP

1. UDP类定义,其继承于线程可做线程启动对象。线程为启动和接受,静态方法为发送

public class UDPsocket implements Runnable{

    private static  final int msglength = 1024;

    private  static DatagramSocket datagramSocket = null;
    private static byte[] message = new byte[msglength];

    @Override
    public void run() {

        try {
            datagramSocket = new DatagramSocket(Password.UDPPort);
            DatagramPacket Packet = new DatagramPacket(message,
                    message.length);

            while (true){
                datagramSocket.receive(Packet);

 //               Password.TCPIP = Packet.getAddress().getHostAddress().toString();
                Log.d("UDP Demo", Packet.getAddress()
                        .getHostAddress().toString()
                        + ":" + new String(Packet.getData()));
                Log.d("UDP Demo", "tcp IP:" + Password.TCPIP);


            }

        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static void UdpSend(String message){

        if(null == datagramSocket){
            return;
        }


        InetAddress local = null;
        try {
            // 换成服务器端IP
            local = InetAddress.getByName(Password.UDPIP);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }


        int msg_length = message.length();
        byte[] messageByte = message.getBytes();
        DatagramPacket p = new DatagramPacket(messageByte, msg_length, local,
                Password.UDPPort);
        try {
            datagramSocket.send(p);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    

}
2. Activity启动UDP的run方法
        UDPsocket udPsocket = new UDPsocket();
        new Thread(udPsocket).start();
3. 某个按钮事件中启动线程调用UDP类的发送方法

            case R.id.button1:

                new  Thread(new Runnable() {
                    @Override
                    public void run() {
                        UDPsocket.UdpSend(Password.UDPstr);
                    }
                }).start();

                break;



例子:TCP客户端

1. TCP类,类似上面的UDP构造

public class TCPControl implements Runnable {

    private   static Socket mSocket = null;
    private  static  SocketAddress socAddress = null;
    private static BufferedReader in = null;
    private static PrintWriter out = null;

    private  PrintWriter output = null;


    @Override
    public void run() {

        try {
            mSocket = new Socket();
            socAddress = new InetSocketAddress(Password.TCPIP, Password.TCPControlPort);
            mSocket.connect(socAddress, 3000);//超时3秒
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public  static void TCPControlsend(String msg)  {


        Log.d("Tcp Demo", "TCP msg:" + msg + "," + Password.TCPIP);

        try {

            //加自动换行
 //           BufferedWriter out = new BufferedWriter(new OutputStreamWriter(mSocket.getOutputStream()));
 //           out.write(msg+"\n");//防止粘包
 //           out.flush();//不加这个flush会怎样?

            //不加自动换行
            byte[] buf = msg.getBytes();
            OutputStream os   = mSocket.getOutputStream();
            os.write(buf);
            os.flush();

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

    }
}
2. 某个按钮事件,第一下按下启动线程连接TCP,之后按下都是启动另一线程发送数据

case R.id.button2:

                switch (Password.TCPflag) {
                    case 0:
                        tcpControl = new TCPControl();
                        new Thread(tcpControl).start();
                        Password.TCPflag = 1;
                        TCPbutton.setText("send");
                        break;
                    case 1:
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                TCPControl.TCPControlsend(Password.TCPstr1);
                            }
                        }).start();
                        Password.TCPflag = 1;
                        TCPbutton.setText("close");
                        break;
                    
                }
                break;








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值