网络编程

局域网:一般IP地址范围192.168.0.0 - 192.168.255.255
本机:127.0.0.1,域名localhost

java内使用InetAddress类代表IP地址

该类将构造方法私有化了,所以说不能直接new对象
创建对象:InetAddress  p = InetAddress.getByName("域名或者IP")
p.getHostName()		返回p的域名
p.getHostAddress()	返回p的IP地址

获取本机IP地址:InetAddress.getLocalHost()
将socket对象转换为InetAddress对象:socket.getInetAddress()

端口号:0~65535。前1024个被预先占用了

课程内的截图
TCP:三次握手连接,四次挥手断开


客户端,服务器

TCP实现一个最简单的通信,先运行服务器代码,在运行客户端代码

客户端

public class test {
	public static void main(String[]args) throws IOException{
		InetAddress ia = InetAddress.getByName("127.0.0.1");
		try(Socket so = new Socket(ia , 12000);			向服务器的端口号发起连接
			OutputStream ou = so.getOutputStream();){
			
			ou.write("你好,我是客户端".getBytes());
			
		}
		catch(IOException e) {
			System.out.println("出错了");
		}

	}
}

服务器

public class sermain {
	public static void main(String[]args) throws IOException {
		
		ServerSocket sr = new ServerSocket(12000);			服务器一直等待客户端的连接
		try(Socket so = sr.accept();						获取连接的socket对象
			InputStream is = so.getInputStream();
			InputStreamReader ir = new InputStreamReader(is , "utf-8");){   使用转化流,避免出现乱码
			char[] by = new char[2];
			int in = ir.read(by);
			while(in != -1) {
				String s = new String(by , 0 , in);
				System.out.print(s);
				in = ir.read(by);
			}
		}
		catch(Exception e) {
			System.out.println("结束1");
		}
	}
}

代码2.0
socket的输入或者输出不会自己中断,需要手动中断
shutdownOutput,shutdownInput
客户端

public class test {
	public static void main(String[]args) throws IOException{
		InetAddress ia = InetAddress.getByName("127.0.0.1");
		try(Socket so = new Socket(ia , 12000);
			OutputStream ou = so.getOutputStream();
			BufferedInputStream bi = new BufferedInputStream(new FileInputStream("./hhh.txt"));
			InputStream it = so.getInputStream();
			BufferedInputStream bit = new BufferedInputStream(it);){
			byte[] by = new byte[10];
			int len = bi.read(by);
			while(len != -1) {
				ou.write(by , 0 , len);
				len = bi.read(by);
			}
			so.shutdownOutput();		就这里,需要中断写之后才可以读
			byte[] lcy = new byte[100];
			int qwert = bit.read(lcy);
			String sts = new String(lcy , 0 , qwert);
			System.out.println("结束啦啦啦啦啦啦啦啦啦啦啦啦啦啦");
			System.out.println(sts);
		}
		catch(IOException e) {
			System.out.println(e.getMessage());
		}

	}
}
public class sermain {
	public static void main(String[]args) throws IOException {
		
		ServerSocket sr = new ServerSocket(12000);
		try(Socket so = sr.accept();
			InputStream is = so.getInputStream();
			InputStreamReader ir = new InputStreamReader(is , "utf-8");
			BufferedWriter buf = new BufferedWriter(new FileWriter("bea.txt"));
			OutputStream out = so.getOutputStream();
			BufferedOutputStream bout = new BufferedOutputStream(out)){
			char[] by = new char[5];
			int in = ir.read(by);
			while(in != -1) {
				buf.write(by , 0 , in);
				in = ir.read(by);
			}
			so.shutdownInput();			不写也可以,只客户端写就行了
			bout.write("服务器收到".getBytes());
			
		}
		catch(Exception e) {
			System.out.println("结束1");
		}
		
	}
}

UDP实现简单的通信:

客户端

package book;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

//客户端
public class UDPclient {
    public static void main(String[]args) throws Exception{
        DatagramSocket socket = new DatagramSocket();

        try(InputStreamReader input = new InputStreamReader(System.in);
            BufferedReader bfa = new BufferedReader(input);){

            String str = bfa.readLine();
            byte[] by = str.getBytes();

            InetAddress inet = InetAddress.getByName("localhost");
            DatagramPacket pack = new DatagramPacket(by , 0 , by.length , inet , 12000);

            socket.send(pack);
        }
        socket.close();
    }
}

服务器

package book;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

//服务器端
public class UDPservice {
    public static void main(String[]args) throws Exception{

        DatagramSocket socket = new DatagramSocket(12000);

        byte[] buffer = new byte[100];
        DatagramPacket packet = new DatagramPacket(buffer , 0 , buffer.length);

        socket.receive(packet);

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

        socket.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值