JAVA 网络编程的常用类

本文提供了TCP和UDP网络通信的基本示例,包括简单的客户端-服务器交互、多客户端并发处理及UDP的数据包发送与接收实现。通过这些代码示例,读者可以了解如何建立网络连接并进行数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、简单Client 输入(TCP)   
public class TestServer {   
    public static void main(String []args)throws Exception{   
        ServerSocket ss=new ServerSocket(1024);   
        System.out.println("Server");   
        Socket so=ss.accept();   
        DataInputStream dis =new DataInputStream(so.getInputStream());   
        DataOutputStream dos =new DataOutputStream(so.getOutputStream());   
        while(true){   
            String temp=dis.readUTF();   
            if(temp.equalsIgnoreCase("bye"))break;   
            dos.writeUTF("server:"+temp);   
        }   
        dis.close();   
        dos.close();   
        so.close();   
        ss.close();   
    }   
}   
  
public class TestClient {   
    public static void main(String[] args) throws Exception {   
        Socket so = new Socket("127.0.0.1", 1024);   
        System.out.println("Client");   
        DataInputStream dis = new DataInputStream(so.getInputStream());    
        DataOutputStream dos = new DataOutputStream(so.getOutputStream());         
        InputStreamReader isr=new InputStreamReader(System.in);      
        BufferedReader br=new BufferedReader(isr);    
        while(true){   
            String temp= br.readLine();     
            dos.writeUTF(temp);   
            if(temp.equalsIgnoreCase("bye"))break;   
            System.out.print(dis.readUTF());   
        }          
        br.close();    
        dis.close();   
        dos.close();   
        so.close();   
    }   
}

2、多个客户端 服务器并发处理(TCP)
public class TestClient {   
    public static void main(String[] args) throws Exception {   
        Socket so = new Socket("127.0.0.1", 1024);   
        System.out.println("Client");   
        DataInputStream dis = new DataInputStream(so.getInputStream());    
        DataOutputStream dos = new DataOutputStream(so.getOutputStream());         
        InputStreamReader isr=new InputStreamReader(System.in);      
        BufferedReader br=new BufferedReader(isr);    
        while(true){   
            String temp= br.readLine();     
            dos.writeUTF(temp);   
            if(temp.equalsIgnoreCase("bye"))break;   
            System.out.print(dis.readUTF());   
        }          
        br.close();    
        dis.close();   
        dos.close();   
        so.close();   
    }   
}   
public class TestServer {   
    public static void main(String[] args) throws Exception {   
        ServerSocket ss = new ServerSocket(1024);   
        System.out.println("Server");   
        while (true) {   
            Socket so = ss.accept();   
            new ServerThread(so).start();   
        }   
        //ss.close();服务器用不关闭   
    }   
}   
public class ServerThread extends Thread {   
    private Socket so;   
    private static String word="";   
    public ServerThread(Socket so) {   
        this.so = so;   
    }   
    public void run() {   
        try {   
            DataInputStream dis = new DataInputStream(so.getInputStream());   
            DataOutputStream dos = new DataOutputStream(so.getOutputStream());   
            while (true) {   
                String temp = dis.readUTF();   
                word=word+temp+"\n";   
                if (temp.equalsIgnoreCase("bye"))   
                    break;   
                dos.writeUTF(word);   
            }   
            dis.close();   
            dos.close();   
            so.close();   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
    }   
} 

3  (UDP)
IP的设置方法
public class TestInetAddress {
	public static void main(String[] args) throws Exception {
		InetAddress ia = InetAddress.getLocalHost();
		System.out.println(ia);
		byte[] b = ia.getAddress();
		for (byte bb : b)
			System.out.println(bb);
		// ////////////
		// 192.168.1.200
		byte[] by = { 192 - 256, 168 - 256, 1, 200 - 256 };
		InetAddress ia2 = InetAddress.getByAddress(by);
		System.out.println(ia2);
	}
}

自己写的
public class Receive {
	public static void main(String[] args) throws Exception {
		System.out.println("Receive:");
		DatagramSocket ds = new DatagramSocket(8848);
		byte[] b = new byte[2048];
		DatagramPacket dp = new DatagramPacket(b, b.length);
		while (true) {
			ds.receive(dp);// 停住
			String temp = new String(b, 0, dp.getLength());
			if (temp.equalsIgnoreCase("bye"))
				break;
			System.out.println(temp);
		}
		ds.close();
	}
}
public class Send {
	public static void main(String[] args) throws Exception {
		System.out.println("Send:");
		byte[] b = new byte[2048];
		DatagramPacket dp = new DatagramPacket(b, b.length, InetAddress
				.getLocalHost(), 8848);
		DatagramSocket ds = new DatagramSocket();

		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		while (true) {
			String temp = br.readLine();
			dp.setData(temp.getBytes());
			ds.send(dp);
			if (temp.equalsIgnoreCase("bye"))
				break;
		}
		br.close();
		ds.close();
	}
}

老师写的
public class Receive {
	public static void main(String[] args) throws Exception {
		DatagramSocket ds = new DatagramSocket(8848); // 邮递员
		byte[] b = new byte[2048];
		DatagramPacket dp = new DatagramPacket(b, b.length);// 邮筒
		while (true) {
			ds.receive(dp);// 停住,直到收到信
			String temp = new String(b, 0, dp.getLength());
			System.out.println(temp);
			if (temp.equals("bye"))
				break;
		}
		ds.close();
	}
}
public class Send {
	public static void main(String[] args) throws Exception {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		DatagramSocket ds = new //邮递员 
		DatagramSocket();
		while (true) {
			String temp = br.readLine();
			byte[] b = temp.getBytes();//写信
			DatagramPacket dp = new //装好信
			DatagramPacket(b, b.length, InetAddress.getLocalHost(), 8848);
			ds.send(dp);//发信
			if (temp.equals("bye"))
				break;
		}
		ds.close();
	}
}

4、public class TestURL {   
    //读网页的内容,很重要。面试要考试   
    public static void main(String[] args) throws Exception {   
        URL url = new URL("http://bbs.tarena.com.cn");   
        URLConnection uc = url.openConnection();   
        InputStreamReader isr = new InputStreamReader(uc.getInputStream());   
        BufferedReader br = new BufferedReader(isr);   
        while (true) {   
            String temp = br.readLine();   
            if (temp == null)   
                break;   
            System.out.println(temp);   
        }   
        br.close();   
    }   
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值