网络变成中的两个要素:
1、IP和端口号;
2、提供网络通信协议:TP\IP参考模型。
通信要素一:IP和端口号
1、IP地址唯一的去标识Internet上的计算机;
2、再JAVA中使用InetAddress类代表IP;
3、IP分类:IPv4和IPv6;万维网和局域网;
4、域名;
5、本地回路地址:127.0.0.1 对应着:localhost;
6、如何实例化InetAddress
:两个方法:getByName(String host)
、getLocalHost()
;两个常用方法:getHostName()\getHostAddress()
。
端口号标识正在计算机上运行的进程(程序),分为公认端口、注册端口、动态/私有端口。
Socket:端口号与IP地址的组合得出的一个网络套接字。
TCP协议与UDP协议的区别。
TCP:要三次握手四次挥手、可靠、效率低;
UDP:不握手、不可靠、效率高。
实现TCP网络编程的例子:
package com.JavaWebTest;
import org.junit.Test;
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 java.net.UnknownHostException;
/**
* 实现TCP的网络编程
*
* 客户短发送信息给服务端,服务端将数据显示在控制台上
*/
public class TCPTest1 {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
//1、创建Socket对象,指明服务器端的IP和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet,8899);
//2、获取输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据
os.write("你好,我是客户端mm".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、关闭资源
if(os !=null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 服务端
@Test
public 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();
//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、关闭资源
if(baos != null){
try {
baos.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) {
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
用JAVA SOCKET编程,读服务器几个字符,再写入本地显示,代码:
//服务端
public class Server {
private ServerSocket ss;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Server() {
try {
ss = new ServerSocket(10000);
while (true) {
socket = ss.accept();
String RemoteIP = socket.getInetAddress().getHostAddress();
String RemotePort = ":" + socket.getLocalPort();
System.out.println("A client come in!IP:" + RemoteIP + RemotePort);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = in.readLine();
System.out.println("Cleint send is :" + line);
out = new PrintWriter(socket.getOutputStream(), true);
out.println("Your Message Received!");
out.close();
in.close();
socket.close();
}
} catch (IOException e) {
out.println("wrong");
}
}
publicstaticvoid main(String[] args) {
new Server();
}
};
//客户端
public class Client {
Socket socket;
BufferedReader in;
PrintWriter out;
public Client() {
try {
System.out.println("Try to Connect to 127.0.0.1:10000");
socket = new Socket("127.0.0.1", 10000);
System.out.println("The Server Connected!");
System.out.println("Please enter some Character:");
BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(socket.getOutputStream(), true);
out.println(line.readLine());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(in.readLine());
out.close();
in.close();
socket.close();
} catch (IOException e) {
out.println("Wrong");
}
}
public static void main(String[] args) {
new Client();
}
}
UDP网络通信:
package com.JavaWebTest;
import org.junit.Test;
import java.io.IOException;
import java.net.*;
public class UDPTest {
// 发送端
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是你爹";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
//接收端
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
URL(Uniform Resourece Locator)编程:
URL的基本结构由5部分组成:
<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
例如: http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123
URL编程例子:
package com.JavaWebTest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest1 {
public static void main(String[] args) throws IOException {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("beauty3.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 {
// 关闭资源
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
urlConnection.disconnect();
}
}