一、网络基础(TCP/IP详解)

1、IP协议(Internet Protocol):网络层,支持网间数据报通信。无连接数据报传送,数据报路由选择和差错控制。

IPv4 32位(4字节),IPv6 128位(16字节)。P

ping ICMP协议

2、TCP协议、UDP协议

(1)TCP(transmission control protocol 打电话):专门设计用于在不可靠的因特网上提供可靠的、端到端的字节流通信的协议。它是一种面向连接的协议。有三次握手。慢

(2)UDP(user data protocol 寄信):提供了一种发送封装的原始IP数据报的方法、并且发送时无需建立连接,是一种不可靠的连接。快

二、Socket

两个Java应用程序可以通过一个双向的网络通信连接实现数据交换,这个双向链路的一端称为一个Socket。

Socket通常用来实现client-server连接。

java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向连接(TCP连接)的client和server端。

建立连接时所需的寻址信息为远程计算机的IP地址和端口号(Port number)。端口号2字节,可以区分不同的应用程序。端口号又分TCP端口和UDP端口,每个都是65536个端口。

例如:

收邮件 POP3 110

STMP 25

FTP 21

HTTP 80

1、TCP Socket通信模型

这只是练习,实际上的网络编程都是异步式的。System.in,accept(),readUTF()都是阻塞式的(非重点)

例1

import java.net.*;
import java.io.*;
public class TCPServer{
public static void main(String args[]) throws Exception{
ServerSocket ss = new ServerSocket(6666);//端口号6666
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();
s.close();
}
}
}
import java.net.*;
import java.io.*;
public class TCPClient{
public static void main(String args[]) throws Exception{
Socket s = new Socket("127.0.0.1",6666);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("hello server!");
dos.flush();
dos.close();
s.close();
}
}

例2

import java.io.*;
import java.net.*;
public class TestServer{
public static void main(String[] args){
try{
ServerSocket s = new ServerSocket(8888);//服务器端口号8888
while(true){
Socket s1 = s.accept();
OutputStream os = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("Hello,"+s1.getInetAddress()+"port#"+s1.getPort()+" byebye!");//客户端的IP地址和端口号
dos.close();
s1.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestClient{
public static void main(String[] args){
try{
Socket s1 = new Socket("127.0.0.1",8888);//服务器地址和端口号
InputStream is = s1.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
dis.close();
s1.close();
}catch(ConnectException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

例3

import java.io.*;
import java.net.*;
public class TestServer{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
try{
ServerSocket ss = new ServerSocket(5888);//设置端口号
Socket s1 = ss.accept();
in = s1.getInputStream();
out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
DataInputStream dis = new DataInputStream(in);
String s = null;
if((s=dis.readUTF())!=null){
System.out.println(s);
System.out.println("from: "+s1.getInetAddress());
System.out.println("Port: "+s1.getPort());
}
dos.writeUTF("hi,hello");
dis.close();
dos.close();
s1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestClient{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
try{
Socket s1 = new Socket("localhost",5888);//服务器地址和端口号
in = s1.getInputStream();
out = s1.getOutputStream();
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
dos.writeUTF("hey");
String s = null;
if((s=dis.readUTF())!=null){
System.out.println(s);
}
dos.close();
dis.close();
s1.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

练习4:(by myself)

import java.io.*;
import java.net.*;
public class TestServer{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
String si = null;
String so = "";
try{
ServerSocket ss = new ServerSocket(5888);//设置端口号
while(true){
Socket s1 = ss.accept();
in = s1.getInputStream();//接收数据
out = s1.getOutputStream();//发送数据
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
//从键盘读入
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader br2 = new BufferedReader(isr2);
while(!so.equals("exit")){
if((so=br2.readLine())!=null&&!so.equals("exit")){
System.out.println("Server:"+so);
dos.writeUTF(so);
}
if((si=dis.readUTF())!=null){
System.out.println("Client:"+si);
}
}
dis.close();
br2.close();
dos.close();
s1.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class TestClient{
public static void main(String[] args){
InputStream in = null;
OutputStream out = null;
String si = null;
String so= "";
try{
Socket s1 = new Socket("127.0.0.1",5888);//服务器地址和端口号
in = s1.getInputStream();//接收
out = s1.getOutputStream();//发送
DataInputStream dis = new DataInputStream(in);
DataOutputStream dos = new DataOutputStream(out);
//从键盘读入
InputStreamReader isr2 = new InputStreamReader(System.in);
BufferedReader br2 = new BufferedReader(isr2);
while(!so.equals("exit")){
if((si=dis.readUTF())!=null){
System.out.println("Server:"+si);
}
if((so=br2.readLine())!=null&&!so.equals("exit")){
System.out.println("Client:"+so);
dos.writeUTF(so);
}
}
dis.close();
br2.close();
dos.close();
s1.close();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}

2、UDP Socket通信模型

没有server,client的概念,不区分两者的socket。receive()方法也是阻塞式的。

例1

import java.net.*;
public class TestUDPServer{
public static void main(String args[]) throws Exception{
byte buf[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true){
ds.receive(dp);
System.out.println(new String(buf, 0, dp.getLength()));
}
}
}
import java.net.*;
public class TestUDPClient{
public static void main(String args[]) throws Exception{
byte[] buf = (new String("Hello")).getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress("127.0.0.1", 5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}

例2

import java.net.*;
import java.io.*;
public class TestUDPServer{
public static void main(String args[]) throws Exception{
byte buf[] = new byte[1024];
ByteArrayInputStream bais = new ByteArrayInputStream(buf);//从字节数组读数据
DataInputStream dis = new DataInputStream(bais);
DatagramPacket dp = new DatagramPacket(buf, buf.length);
DatagramSocket ds = new DatagramSocket(5678);
while(true){
ds.receive(dp);
long l = dis.readLong();
System.out.println(l);
}
}
}
import java.net.*;
import java.io.*;
public class TestUDPClient{
public static void main(String args[]) throws Exception{
long n = 10000L;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(n);
byte[] buf = baos.toByteArray();
System.out.println(buf.length);
DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",5678));
DatagramSocket ds = new DatagramSocket(9999);
ds.send(dp);
ds.close();
}
}

JAVA笔记26-网络编程(不等于网站编程)的更多相关文章

  1. Java笔记 - Socket编程

    两个Java应用程序可以通过一个双向的网络通讯连接实现数据交换,这个双向链路的一端称为一个Socket.java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向链路的c ...

  2. Java 学习之网络编程案例

    网络编程案例 一,概念 1,网络编程不等于网站编程 2,编程只和传输层打交道,即TCP和UDP两个协议 二,案例 1,TCP实现点对点的聊天 Server端:两个输入流:读客户端和控制台,一个输出端: ...

  3. 大数据学习笔记——Java篇之网络编程基础

    Java网络编程学习笔记 1. 网络编程基础知识 1.1 网络分层图 网络分层分为两种模型:OSI模型以及TCP/IP网络模型,前者模型分为7层,是一个理论的,参考的模型:后者为实际应用的模型,具体对 ...

  4. JAVA自学笔记26

    JAVA自学笔记26 1.网络编程 1)用来实现网络互联的不同计算机上运行的程序可以进行数据交换 2)网络模型一般泛指 OSI:(Open System Interconnection)开放系统互联参 ...

  5. Java学习之网络编程实例

    转自:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616413.html 多谢分享 网络编程 网络编程对于很多的初学者来说,都是很向往的一 ...

  6. Java进阶之网络编程

    网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...

  7. java第九节 网络编程的基础知识

    /** * * 网络编程的基础知识 * 网络协议与TCP/IP * IP地址和Port(端口号) * 本地回路的IP地址:127.0.0.1 * 端口号的范围为0-65535之间,0-1023之间的端 ...

  8. 20165324 Java实验五 网络编程与安全

    20165324 Java实验五 网络编程与安全 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:何春江 学号:20165324 指导教师:娄嘉鹏 实验日期:2018年5月28日 实 ...

  9. Java网络编程 -- NIO非阻塞网络编程

    从Java1.4开始,为了替代Java IO和网络相关的API,提高程序的运行速度,Java提供了新的IO操作非阻塞的API即Java NIO.NIO中有三大核心组件:Buffer(缓冲区),Chan ...

随机推荐

  1. caffe-----使用C++ 提取网络中间层特征数据

    最近实验,想要在c++下知道网络中间某一层的特征数据情况,查找了相关资料,记录一下. 其实在caffe框架里面是包含这种操作的,可以模仿tools/extract_features.cpp中的操作来得 ...

  2. Stream流实现斐波那契数列

    1.前言 我们都知道斐波那契数列有很多种实现方法,在jdk1.8以前没有流操作,只能通过递归或者迭代等其他方式来实现斐波那契数列, 但是jdk1.8以后,有了流操作,我们就可以使用流来实现斐波那契数列 ...

  3. 记一次nginx配置伪静态规则

    server { listen 80; server_name sss.cn; root "root/"; location / { index index.html index. ...

  4. 【Qt开发】Qt在QLabel(QWidget)鼠标绘制直线和矩形框

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://devbean.blog.51cto.com/448512/243546 说实话, ...

  5. DataGridView中EnditCommit()调用之后,单元格的内容被全选了,每次输入都要鼠标点击定位到最后才能继续输入

    因为某些需求,DataGridView在输入一次内容,就要调用ECommitEdit(DataGridViewDataErrorContexts.Commit)来将内容提交,但是这样做之后,控件就会当 ...

  6. Solrcloud单机伪集群部署

    线上有一套双节点的Solrcloud节点,因机器性能较老,环境搭建于2013年,原节点有数百个已经被unload的collections,考虑以后可能还会需要,所以搭建一套和原节点相同的solrclo ...

  7. PHP手册在7.1迁移页面给出了替代方案,就是用OpenSSL取代MCrypt.

    /**  * [AesSecurity aes加密,支持PHP7.1]  */ class AesSecurity {     /**      * [encrypt aes加密]      * @p ...

  8. 高效编程之 concurrent.future

    背景 我们知道 Python 中有多线程threading 和多进程multiprocessing 实现并发, 但是这两个东西开销很大,一是开启线程/进程的开销,二是主程序和子程序之间的通信需要 序列 ...

  9. 09.AutoMapper 之自定义类型转换器(Custom Type Converters)

    https://www.jianshu.com/p/47054d92db2a 自定义类型转换器(Custom Type Converters) 有时需要完全控制一种类型到另一种类型的转换.这一般发生在 ...

  10. 常见SMT极性元器件识别方法

    极性元件在整个PCBA加工过程中需要特别注意,因为方向性的元件错误会导致批量性事故和整块PCBA板的失效,因此工程及生产人员了解SMT极性元件极为重要. 1.片式电阻(Resistor)无极性 2.电 ...