网络编程

package com.jp.ip;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpDemo {
	public static void main(String[] args) throws UnknownHostException {
		InetAddress localHost = InetAddress.getLocalHost();//获取本地主机的ip地址
		
//		System.out.println(localHost.getHostName());
//		System.out.println(localHost.getHostAddress());
		
		InetAddress byName = InetAddress.getByName("www.baidu.com");
		System.out.println(byName.getHostName());
		System.out.println(byName.getHostAddress());//61.135.169.125
	}
}

package com.jp.tcp;

import java.io.OutputStream;
import java.net.Socket;

public class ClientDemo {
	public static void main(String[] args) throws Exception {
		System.out.println("客户端已开启");
		//创建客户端socket对象
		Socket s = new Socket("192.168.0.100", 8888);
		
		
		//获取输出流对象
		OutputStream out = s.getOutputStream();
		
		//发送数据
		String data = "hello,你好,兄弟!!!";
		out.write(data.getBytes());
		
		//关闭流
		out.close();
	}
}

package com.jp.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
	public static void main(String[] args) throws IOException {
		System.out.println("服务端已开启...");
		//创建服务端socket对象
		ServerSocket ss = new ServerSocket(8888);
		
		//监听(阻塞)
		Socket s = ss.accept();
	
		//获取读取流对象
		InputStream in = s.getInputStream();
		
		byte[] b = new byte[1024];
		int len = 0;
		while((len = in.read(b)) != -1){
			String data = new String(b, 0, len);
			System.out.println(data);
		}
		
		
		
		//关闭流对象
		s.close();
	}
}

package com.jp.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ReceiveDemo {
	public static void main(String[] args) throws IOException {
		
		System.out.println("接收端已开启......");
		//创建接收端socket对象
		DatagramSocket ds = new DatagramSocket(8888);
		
		byte[] buf = new byte[100];
		int length = buf.length;
		
		//数据包
		DatagramPacket p = new DatagramPacket(buf, length);
		
		//接收数据(阻塞)
		ds.receive(p);
		
		byte[] data = p.getData();
		System.out.println(new String(data,0,length));
		//关闭流对象
		ds.close();
	}
}

package com.jp.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class SendDemo {
	public static void main(String[] args) throws IOException {
		System.out.println("发送端已开启......");
		//创建发送端socket对象
		DatagramSocket ds = new DatagramSocket();
		
		String data = "你好啊,兄弟!!!";
		byte[] buf = data.getBytes();
		int length = buf.length;
		InetAddress address = InetAddress.getByName("192.168.0.100");
		int port = 8888;
		
		//数据包
		DatagramPacket p = new DatagramPacket(buf, length, address, port);
		
		//发送数据
		ds.send(p);
		
		//关闭流对象
		ds.close();
	}
}

package com.jp.url;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class UrlDemo {
	public static void main(String[] args) throws IOException {
		URL url = new URL("http://192.168.0.100:8080/xx/aa.txt?name=zs");
//		System.out.println(url.getProtocol());
//		System.out.println(url.getFile());
//		System.out.println(url.getHost());
//		System.out.println(url.getPort());
//		System.out.println(url.getQuery());
		
		InputStream in = url.openStream();
		byte[] b = new byte[20];
		int read = in.read(b);
		System.out.println(new String(b));
		
		URLConnection c = url.openConnection();
		c.getInputStream();
		c.getOutputStream();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值