网络编程作业

本文介绍了两个网络编程作业,包括使用UDP协议实现一个监听8001端口的接收程序,接收到的数据为'Hello,world'。另一部分是使用TCP协议构建服务器,服务器在8002端口监听,连接客户端后发送'Hello,world',客户端接收到后显示出来。" 120523435,1147440,Excel分组内相对位置取数:VLOOKUP与MAX(IF..)挑战,"['Excel公式', '数据处理', 'Python编程', 'pandas库', 'esProc']

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

1.使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello,world”。


package test;

import java.net.*;

public class example01 {
	public static void main(String[] args) throws Exception{
			byte[] buf = new byte[1024];
			DatagramSocket ds = new DatagramSocket(8001);
			DatagramPacket dp = new DatagramPacket (buf,1024);
			System.out.println("等待接收数据");
			ds.receive(dp);
			String str = new String(dp.getData(),0,dp.getLength()) ;
			System.out.println(str);
			ds.close();
	}
}

package test;
import java.net.*;
public class example02 {
	public static void main(String[] args) throws Exception{
		DatagramSocket ds = new DatagramSocket(3000);
		String str = "Hello, world";
		DatagramPacket dp = new DatagramPacket (str.getBytes(),str.length(),
				InetAddress.getByName("localhost"),8001);
				System.out.println("发送信息");
				ds.send(dp);
				ds.close();
	}
}




2.使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello,world”,客户端收到数据后打印输出。


package test; 
  
import java.io.DataInputStream;  
import java.io.DataOutputStream;  
import java.io.OutputStream;  
import java.net.ServerSocket;  
import java.net.Socket;  
  
public class TCPServer {  
  
    public static void main(String[] args) throws Exception {  
        {  
            // TODO Auto-generated method stub  
            ServerSocket ss = new ServerSocket(8881);  
            while (true) {  
                Socket s = ss.accept();  
                // System.out.println("A client connect");  
                // DataInputStream dis = new  
                // DataInputStream(s.getInputStream());//数据输入流  
                // System.out.println(dis.readUTF());//输出读取的信息  
                // dis.close();  
                OutputStream os = s.getOutputStream();// 输出流  
                DataOutputStream dos = new DataOutputStream(os);// 数据输出流  
                dos.writeUTF("Hello Server!");// 想文本写出流  
                dos.flush();//  
                s.close();  
            }  
        }  
    }  
}  

[java] view plain copy
package test;  
  
import java.io.DataInputStream;  
import java.io.DataOutputStream;  
import java.io.OutputStream;  
import java.net.Socket;  
  
public class TCPCliant {  
    public static void main(String[] args) throws Exception{  
        // TODO Auto-generated method stub  
        Socket s = new Socket("127.0.0.1", 8881);  
        DataInputStream dis = new DataInputStream(s.getInputStream());//数据输入流(二合一)  
        System.out.println(dis.readUTF());//输出读取的信息  
        dis.close();  
        //OutputStream os = s.getOutputStream();//输出流  
        //DataOutputStream dos= new DataOutputStream(os);//数据输出流  
        //dos.writeUTF("Hello Server!");//想文本写出流  
        //dos.flush();//  
    //  dos.close();  
        s.close();  
    }  
  
}  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值