android socket

image

 

 

image

 

 

 

class StartSocketListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            new ServerThread().start();
        }
        
    }
    
    class ServerThread extends Thread{
        /*public void run(){
            //声明一个ServerSocket对象
            ServerSocket serverSocket = null;
            try {
                //创建一个ServerSocket对象,并让这个Socket在4567端口监听
                serverSocket = new ServerSocket(4567);
                //调用ServerSocket的accept()方法,接受客户端所发送的请求
                Socket socket = serverSocket.accept();
                //从Socket当中得到InputStream对象
                InputStream inputStream = socket.getInputStream();
                byte buffer [] = new byte[1024*4];
                int temp = 0;
                //从InputStream当中读取客户端所发送的数据
                while((temp = inputStream.read(buffer)) != -1){
                    System.out.println(new String(buffer,0,temp));
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        
        }*/
        public void run(){
            try {
                //创建一个DatagramSocket对象,并指定监听的端口号
                DatagramSocket socket = new DatagramSocket(4567);
                byte data [] = new byte[1024];
                //创建一个空的DatagramPacket对象
                DatagramPacket packet = new DatagramPacket(data,data.length);
                //使用receive方法接收客户端所发送的数据
                socket.receive(packet);
                String result = new String(packet.getData(),packet.getOffset(),packet.getLength());
                System.out.println("result--->"   result);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

 

public class TCPClient {
    public static void main(String[] args) {
        try {
            //创建一个Socket对象,指定服务器端的IP地址和端口号
            Socket socket = new Socket("192.168.1.104",4567);
            //使用InputStream读取硬盘上的文件
            InputStream inputStream = new FileInputStream("f://file/words.txt");
            //从Socket当中得到OutputStream
            OutputStream outputStream = socket.getOutputStream();
            byte buffer [] = new byte[4*1024];
            int temp = 0 ;
            //将InputStream当中的数据取出,并写入到OutputStream当中
            while((temp = inputStream.read(buffer)) != -1){
                outputStream.write(buffer, 0, temp);
            }
            outputStream.flush();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

public class UDPClient {
    public static void main(String[] args) {
        try {
            //首先创建一个DatagramSocket对象
            DatagramSocket socket = new DatagramSocket(4567);
            //创建一个InetAddree
            InetAddress serverAddress = InetAddress.getByName("192.168.1.104");
            String str = "hello";
            byte data [] = str.getBytes();
            //创建一个DatagramPacket对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
            DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,4567);
            //调用socket对象的send方法,发送数据
            socket.send(packet);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
posted on 2012-07-08 21:19  马德华  阅读(471)  评论(0编辑  收藏  举报