码迷,mamicode.com
首页 > Web开发 > 详细

netWork

时间:2015-12-09 01:54:27      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

1.获得页面代码:URL和URLConnection类对于希望建立与HTTP服务器的连接来获取信息

 

public class UrlConnection2
{
    public static void main(String[] args) throws Exception
    {
        URL url = new URL("http://www.csdn.net");
        
//        URLConnection conn = url.openConnection();
//        
//        InputStream is = conn.getInputStream();
        
        InputStream is = url.openStream();//与上面两个是等价的,都可以获取URL的内容信息
        
        
        OutputStream os = new FileOutputStream("infoq.html");
        
        byte[] buffer = new byte[2048];
        
        int length = 0;
        
        while(-1 != (length = is.read(buffer, 0, buffer.length)))
        {
            os.write(buffer, 0, length);
        }
        
        is.close();
        os.close();
        
        

 

2.

public class InetAddressTest
{
    public static void main(String[] args) throws Exception
    {
        InetAddress address = InetAddress.getLocalHost();//返回象征本机的inetAddress对象
        
        System.out.println(address);//PP-PC/192.168.5.189
        
        address = InetAddress.getByName("www.sohu.com");
        
        System.out.println(address);//www.sohu.com/117.34.8.50
    }
}

3.TCP/IP的socket(套接字):Socket是连接运行在网络上的两个程序间的双向通讯的端点。

(1)TCP

技术分享

public class TcpServer
{
    public static void main(String[] args) throws Exception
    {
        ServerSocket ss = new ServerSocket(2000);
        
        Socket socket = ss.accept();
        
        InputStream is = socket.getInputStream();
        byte[] buffer = new byte[200];
        
        int length = is.read(buffer);
        
        
        
        OutputStream os = socket.getOutputStream();
        
        os.write("welcome".getBytes());
        
        
        System.out.println(new String(buffer, 0 ,length));
        
        
        
//        int length = 0;
        
//        while(-1 != (length = is.read(buffer,0, buffer.length)))
//        {
//            String str = new String(buffer, 0, length);
//            
//            System.out.println(str);
//        }
        
        
        is.close();
        os.close();
        socket.close();
    }
}
public class TcpClient
{
    public static void main(String[] args) throws Exception
    {
        Socket socket = new Socket("192.168.1.158", 2000);//参数:主机、端口
        
        InputStream is = socket.getInputStream();//输入端
        
        byte[] buffer = new byte[200];
        
        int length = is.read(buffer);
        
        
        
        
        OutputStream os = socket.getOutputStream();
        
        os.write("hello world".getBytes());
        
        ;
        
        System.out.println(new String(buffer, 0 ,length));
        
//        int length = 0;
//        
//        while(-1 != (length = is.read(buffer,0, buffer.length)))
//        {
//            String str = new String(buffer, 0, length);
//            
//            System.out.println(str);
//        }
        
        is.close();
        os.close();
        socket.close();
    }
}

 

 

(2)UDP采用数据报

技术分享

public class UdpTest1
{
    public static void main(String[] args) throws Exception
    {
        //服务器端
        //生成数据包套接字
        DatagramSocket socket = new DatagramSocket();
        
        
        //发送数据
        String str = "hello world";

        DatagramPacket packet = new DatagramPacket(str.getBytes(),
                str.length(), InetAddress.getByName("localhost"), 7000);//数据包:传送的数据、数据长度、主机号、端口号
        
        socket.send(packet);
        
        
        //接受数据
        byte[] buffer = new byte[1000];
        
        DatagramPacket packet2 = new DatagramPacket(buffer,100);//接受数据:接受传过来的数据、数据长度
        
        socket.receive(packet2);
        
        
        
        System.out.println(new String(buffer, 0, packet2.getLength()));
        
        socket.close();
    }
}
public class UdpTest2
{ 
    public static void main(String[] args) throws Exception
    {
        //客户端
        //生成数据包套接字,
        DatagramSocket socket = new DatagramSocket(7000);//参数:端口为7000
        
        //接受数据
        byte[] buffer = new byte[1000];

        DatagramPacket packet = new DatagramPacket(buffer, 1000);//参数:数据、数据长度

        socket.receive(packet);

        System.out.println(new String(buffer, 0, packet.getLength()));

        //发送数据
        String str = "welcome";

        DatagramPacket packet2 = new DatagramPacket(str.getBytes(),
                str.length(), packet.getAddress(), packet.getPort());
        
        socket.send(packet2);
        
        socket.close();
    }
}

 

netWork

标签:

原文地址:http://www.cnblogs.com/liu-Gray/p/5031652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!