java网络编程
来源:互联网 发布:java图形接口 编辑:程序博客网 时间:2024/06/16 22:11
主要内容:
把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息、共享硬件、软件、数据信息等资源。
直接或间接地通过网络协议与其它计算机进行通讯。
IP地址和端口号共同组成网络套接字
InetAddress:
InetAddress address=InetAddress.getByName("www.atguigu.com");//通过域名获取一个InetAddress对象
InetAddress local=InetAddress.getLocalHost();//获取本地的主机
System.out.println(address);//www.atguigu.com/42.121.6.2
System.out.println(address.getHostName());//www.atguigu.com hostName 域名
System.out.println(address.getHostAddress());//42.121.6.2 hostAddress ip地址
在浏览器的地址栏输入网络链接之后的访问过程:
http://www.atguigu.com----->先找本机hosts,是否有输入的域名地址-------->本地没有,DNS(域名解析器)
示意图:
Socket:
为服务端
代码示例一:客户端给服务端发送信息。服务端输出此信息到控制台上
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
// 1.创建一个Socket的对象,通过构造器指明服务端的IP地址,以及其接收程序的端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
// 2.getOutputStream():发送数据,方法返回OutputStream的对象
os = socket.getOutputStream();
// 3.具体的输出过程
os.write("我是客户端,请多关照".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 4.关闭相应的流和Socket对象
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 服务端
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
try {
// 1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
ss = new ServerSocket(9090);
// 2.调用其accept()方法,返回一个Socket的对象
s = ss.accept();
// 3.调用Socket对象的getInputStream()获取一个从客户端发送过来的输入流
is = s.getInputStream();
// 4.对获取的输入流进行的操作
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.print(str);
}
System.out.println("收到来自于" + s.getInetAddress().getHostAddress()
+ "的连接");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流以及Socket、ServerSocket的对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
代码示例二:客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送“已收到信息”给客户端
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),8989);
os = socket.getOutputStream();
os.write("我是客户端".getBytes());
//shutdownOutput():执行此方法,显式的告诉服务端发送完毕!
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(8989);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
os = s.getOutputStream();
os.write("我已收到你的情意".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
示例代码:
@Test
public void send() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
byte[] b = "你好,我是要发送的数据".getBytes();
//创建一个数据报:每一个数据报不能大于64k,都记录着数据信息,发送端的IP、端口号,以及要发送到
//的接收端的IP、端口号。
DatagramPacket pack = new DatagramPacket(b, 0, b.length,
InetAddress.getByName("127.0.0.1"), 9090);
ds.send(pack);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ds != null){
ds.close();
}
}
}
// 接收端
@Test
public void rceive() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(9090);
byte[] b = new byte[1024];
DatagramPacket pack = new DatagramPacket(b, 0, b.length);
ds.receive(pack);
String str = new String(pack.getData(), 0, pack.getLength());
System.out.println(str);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ds != null){
ds.close();
}
}
}
URL编程:
示例代码:
//1.创建一个URL的对象
URL url = new URL("http://127.0.0.1:8080/examples/HelloWorld.txt?a=b");//File file = new File("文件的路径");
//如何将服务端的资源读取进来:openStream()
InputStream is = url.openStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
is.close();
//如果既有数据的输入,又有数据的输出,则考虑使用URLConnection
URLConnection connection = url.openConnection();
OutputStream os = connection.getOutputStream();
- 【网络编程】JAVA网络编程
- java网络编程:网络基本概念
- Java网络编程-UDP编程
- Java 高层网络编程
- Java网络编程2
- Java网络编程3
- Java网络编程4
- Java网络服务器编程
- java网络编程1
- java网络编程2
- JAVA网络编程
- Java网络服务器编程
- java网络编程
- Java 高层网络编程
- Java 高层网络编程
- Java网络服务器编程
- Java网络编程
- JAVA网络编程
- OC语言笔记(五):OC封装和复合
- android的数据存储的方式
- ecshop二次开发--放大镜
- LoadRunner监控远程机器Windows资源,提示“net use failed” 解决办法
- [OpenGL] 兔子与顶点数组、拾取
- java网络编程
- MySQL基础
- 模仿系统自带的集拍照,录制视频,以及iphone6S以下的手机拍摄livephoto照片(livephoto),滑动可切换
- 【新版】Inventor二次开发学习指南
- 生成网站快捷方式
- 解决Android studio 下的APK打包失败问题
- OC语言笔记(六):OC单例、NSData和文件操作
- 神奇的排序
- 阐述一下线程与进程的区别