network(二)

1、windows系统手动设置ip地址(ipv4)很容易。ipv6用16个字节表示ip地址。

443端口主要用于https服务。

2、java网络编程
2.1、

// 服务器端
public ServerSocket(int port) throws IOException {} // 指定服务器端监听的端口号
public Socket accept() throws IOException {} // 监听连接
// 客户端
public Socket(InetAddress address, int port) throws IOException {} // 客户端指定服务器地址
getInputStream(),getOutputStream()获取socket中的流和服务器进行IO通信。

2.2、

public void shutdownOutput() throws IOException{} // 关闭java.net.Socket的输出流

2.3、模拟文件上传

public class TCPServer { // 可以每次开启一个线程处理请求,如用上while(true){}
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("src/output.jpg");
		byte[] buff = new byte[1024];
		int len = 0;
		ServerSocket ss = new ServerSocket(7891);
		Socket socket = ss.accept();		
		InputStream is = socket.getInputStream();
		while ((len = is.read(buff)) != -1) {
			fos.write(buff, 0, len);
		}
		
		OutputStream os = socket.getOutputStream();
		os.write("上传成功".getBytes());
		
		fos.close();
		socket.close();
		ss.close();
	}
}
public void clientTest() throws IOException{
		FileInputStream fis = new FileInputStream("src/test.jpeg");
		byte[] buff = new byte[1024];
		int len = 0;
		Socket socket = new Socket("127.0.0.1", 7891);		
		OutputStream os = socket.getOutputStream();
		while ((len = fis.read(buff)) != -1) {
			os.write(buff, 0, len);
		}
		socket.shutdownOutput(); // 写一个结束标记!!!否则与服务器交互时将阻塞在IO处。
		System.out.println("22222222");
		
		InputStream is = socket.getInputStream();
		while ((len = is.read(buff)) != -1) {
			System.out.println(new String(buff, 0, len));
		}
		
		fis.close();
		socket.close();
	}

2.4、B/S架构,浏览器充当客户端的角色;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值