第十六周Java作业
来源:互联网 发布:linux修改bashprofile 编辑:程序博客网 时间:2024/06/04 23:36
1.题目
使用UDP协议编写一个网络程序,设置接收端程序的监听端口是8001,发送端发送的数据是“Hello, world”。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
2.代码
public class UDPreceive {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
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()) + "from "
+ dp.getAddress().getHostAddress() + ": " + dp.getPort();
System.out.println(str);
ds.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPsend {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
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();
}
}
3.截图
1.题目
2. 使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello, world”,客户端收到数据后打印输出。
2.代码
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPreceive {
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
new TCPClient().connect();
}
}
class TCPClient{
private static final int PORT = 8002;
public void connect() throws UnknownHostException, IOException{
Socket client = new Socket(InetAddress.getLocalHost(), PORT);
InputStream is = client.getInputStream();
byte[] buf = new byte[1024];
int len = is.read(buf);
System.out.println(new String(buf, 0, len));
client.close();
}
}
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPsend {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
new TCPServer().listen();
}
}
class TCPServer{
private static final int PORT = 8002;
public void listen() throws IOException{
ServerSocket serverSocket = new ServerSocket(PORT);
Socket client = serverSocket.accept();
OutputStream os = client.getOutputStream();
System.out.println("开始与客户端交互数据");
String str = "Hello,world";
os.write(str.getBytes());
System.out.println("结束与客户端交互数据");
os.close();
client.close();
}
}
- 第十六周java作业
- 第十六周Java作业
- 12.13第十六周Java作业
- 第十六周java作业--网络编程
- 第十六周作业 1.0
- 第十六周作业 2.0
- 第十六周作业 1
- 第十六周作业 2
- 第十六周作业1
- 第十六周作业2
- 第十六周作业3
- 第十六周作业
- 第十六周作业任务一
- 算法第十六周作业01
- 第十六周作业:网络编程
- 算法作业_26(2017.6.6第十六周)
- 算法作业_27(2017.6.8第十六周)
- 算法作业_28(2017.6.8第十六周)
- 周日记
- Vue路由(vue-router)配置实战——动态路由,重定向,工程非根目录
- Android中父View和子view的点击事件
- cesium 坐标转换
- spring mvc 请求参数
- 第十六周Java作业
- ZYNQ启动系统是总是停止在mousedev: PS/2 mouse device common for all mice i2c /dev entries driver
- 完美解决隐藏Listview和RecyclerView去掉滚动条和滑动到边界阴影的方案
- [CSDN_Markdown] 使用LaTeX写矩阵
- The type java.lang.Object cannot be resolved.It is indirectly referenced from required .class file
- 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?
- nginx + uwsgi 部署python django web服务
- 球面数据拟合算法简介
- Windows下mysql-python安装