网络编程:
1.要想实现网络通信,需要解决的问题:
如何准确定位互联网上的一台或多台主机。
如何实现可靠而高效的数据通信。
2.网络通信的两个要素:
使用IP地址。
使用通信协议:TCP/IP 网络通信模型:应用层,传输层,网络层,物理+数据链路层。
3.java.net.InetAddress 类的使用
InetAdress类的一个对象,就代表一个IP地址。
一个IP地址,唯一地标识Internet上的计算机。
本地回路地址:127.0.0.1 域名:localhost
package inetAddressTest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressTest {
public static void main(String[] args) {
//实例化
try {
// 1.实例化:getByName(String host)
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
System.out.println(inetAddress.getHostAddress());
// 2.获取本地的ip
InetAddress inetAddress2 = InetAddress.getLocalHost();
System.out.println(inetAddress2);
System.out.println(inetAddress2.getHostName());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.端口号:
端口号标识正在计算机上运行的进程(程序)
5.URL编程
Uniform Resource Locator :统一资源定位符,它表示Internet上某一资源的地址。
协议名://主机名:端口号/文件名
public String getProtocol() 获取该URL的协议名
public String getHost() 获取该URL的主机名
public String getPort() 获取该URL的端口号
public String getPath() 获取该URL的路径
public String getFile() 获取该URL的文件名
public String getQuery() 获取该URL的查询名
6.TCP 编程
package inetAddressTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Test;
public class URLTest {
@Test
public void test(){
//实例化
try {
URL url = new URL("");
System.out.println(url.getHost());
//可以使用url的一系列方法
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//下载指定位置的资源
@Test
public void test1(){
HttpURLConnection connection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg");
connection = (HttpURLConnection) url.openConnection();
is = connection.getInputStream();
fos = new FileOutputStream("test.jpg");
byte [] buffer = new byte[1024];
int len ;
while((len = is.read(buffer))!=1)
fos.write(buffer,0,len); // 这一行会报数组越界错误,但不影响。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(fos != null)
try {
fos.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(connection != null)
connection.disconnect();
}
}
}
package inetAddressTest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
/**
* TCP 的 网络通信举例1
* 客户端发送数据给服务器端,服务器端获取数据,将数据显示在控制台
*
*/
/*****先运行服务端,再运行客户端***************/
public class TCPTest {
//服务端
@Test
public void server(){
// 1.创建ServerSocket
ServerSocket serverSocket = null ;
Socket socket = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(8989);
//2.得到Socket
socket = serverSocket.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//有乱码风险
// byte[] buffer = new byte[10];
// int len;
// while((len = is.read(buffer))!=-1){
// String s = new String(buffer,0,len);
// System.out.println(s);
// }
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10];
int len;
while((len = is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(baos != null)
try {
baos.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();
}
if(serverSocket != null)
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//客户端
@Test
public void client(){
OutputStream os = null;
Socket socket = null;
try {
//1.创建Socket,指明对方的IP地址和对方的端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"),8989);
//2.获取一个输出流
os = socket.getOutputStream();
//3.输出数据
os.write("你好,服务端,我是客户端".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭资源
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package inetAddressTest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
/**
* TCP 的 网络通信举例2
* 客户端发送数据给服务器端,服务器端获取数据。并告诉客户端收到了,同时将客户端发来的数据显示在控制台。客户端将服务端发来
* 的数据也打印出来。
*
*/
/*****先运行服务端,再运行客户端***************/
public class TCPTest2 {
//服务端
@Test
public void server(){
// 1.创建ServerSocket
ServerSocket serverSocket = null ;
Socket socket = null;
ByteArrayOutputStream baos = null;
OutputStream os = null;
try {
serverSocket = new ServerSocket(8989);
//2.得到Socket
socket = serverSocket.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10];
int len;
while((len = is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
System.out.println("数据来自于:"+socket.getInetAddress().getHostAddress());
socket.shutdownInput();
//4.给客户端发送反馈信息。
os = socket.getOutputStream();
os.write("已收到".getBytes());
//关闭输出过程
socket.shutdownOutput();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(baos != null)
try {
baos.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();
}
if(serverSocket != null)
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//客户端
@Test
public void client(){
OutputStream os = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.创建Socket,指明对方的IP地址和对方的端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"),8989);
//2.获取一个输出流
os = socket.getOutputStream();
//3.输出数据
os.write("你好,服务端,我是客户端".getBytes());
//关闭输出过程
socket.shutdownOutput();
//4.接收服务器端发送来的数据
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[10];
int len;
while((len = is.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
socket.shutdownInput();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭资源
if(baos != null)
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}