网络编程的目的:
直接或间接地通过网络协议与其他计算机实现数据交换,进行通讯。
网络编程中有两个主要的问题
1.如何准确地定位网络上一台或多态主机(IP),定位主机上的特定的应用(端口号)。
2.找到主机后如何可靠高效地进行数据传输。(网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理链路层))。
ip:唯一的表示Internet上的计算机(通信实体)
在java中使用InetAddress类代表ip
ip的分类方式:ip4和ip6 ;万维网和局域网
域名: www.baidu.com www.mi.com www.sina.com
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) {
try {
InetAddress inet1=InetAddress.getByName("192.168.10.14");
System.out.println(inet1);
InetAddress inet2=InetAddress.getByName("www.atguigu.com");
System.out.println(inet2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
本地回路地址:127.0.0.1
对应着本地的主机localhost
如何实例化InetAddress:两个方法:getByName(String host)、getLocalHost()
两个常用方法:getHostName()主机域名、getHostAddress()主机ip地址
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) {
try {
InetAddress inet1=InetAddress.getByName("192.168.10.14");
System.out.println(inet1);
InetAddress inet2=InetAddress.getByName("www.atguigu.com");
System.out.println(inet2);
//本机的ip地址
InetAddress inet3=InetAddress.getByName("127.0.0.1");
System.out.println(inet3);
InetAddress inet4=InetAddress.getLocalHost();
System.out.println(inet4);
//getHostName()主机域名
System.out.println(inet2.getHostName());
//getHostAddress()主机ip地址
System.out.println(inet2.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
TCP与UDP有何不同
实现TCP的网络编程
例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class Demo {
public static void main(String[] args) {
server();
client();
}
//客户端
public static void client(){
InetAddress inet= null;
Socket socket=null;
OutputStream os=null;
try {
//1.创建Socket对象,指明服务器端的ip和端口号
inet = InetAddress.getByName("127.0.0.1");
socket=new Socket(inet,8899);
//2.获取一个输出流,用于输出数据
os=socket.getOutputStream();
//3.写出数据的操作
os.write("你好".getBytes());
} catch (UnknownHostException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
//4.资源的关闭
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务端
public static void server(){
ServerSocket ss=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos=null;
try {
//1.创建服务器端的ServerSocket,指明自己的端口号
ss=new ServerSocket(8899);
//2.调用accept()表示接受来自客户端的socket
socket=ss.accept();
//3.获取输入流
is=socket.getInputStream();
//不建议这样写,可能出现乱码
// byte[] buffer=new byte[20];
// int len;
// while ((len=is.read(buffer))!=-1){
// String str=new String(buffer,0,len);
// System.out.println(str);
// }
//4.读取输入流中的数据
baos=new ByteArrayOutputStream();
byte[] buffer=new byte[5];
int len;
while ((len=is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
//5.资源的关闭
try {
if (baos!=null)
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(ss!=null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
实现TCP的网络编程
例2:客户端发送文件给服务端
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Demo {
public static void main(String[] args) {
server();
client();
}
//客户端
public static void client() {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
os = socket.getOutputStream();
fis = new FileInputStream(new File("D:\\大一下学期文档\\桌面壁纸.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void server(){
ServerSocket ss=null;
Socket socket=null;
InputStream is=null;
FileOutputStream fos=null;
try {
ss=new ServerSocket(9090);
socket=ss.accept();
is=socket.getInputStream();
fos=new FileOutputStream(new File("D:\\大一下学期文档\\桌面壁纸1.jpg"));
byte[] buffer=new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fos!=null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(ss!=null)
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
p626-630
这里的代码操作都不成功,先照搬,后面再解决问题。