网络变成中的两个要素:
    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网络编程的例子:

    1. package com.JavaWebTest;
    2. import org.junit.Test;
    3. import java.io.ByteArrayOutputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.io.OutputStream;
    7. import java.net.InetAddress;
    8. import java.net.ServerSocket;
    9. import java.net.Socket;
    10. import java.net.UnknownHostException;
    11. /**
    12. * 实现TCP的网络编程
    13. *
    14. * 客户短发送信息给服务端,服务端将数据显示在控制台上
    15. */
    16. public class TCPTest1 {
    17. // 客户端
    18. @Test
    19. public void client() {
    20. Socket socket = null;
    21. OutputStream os = null;
    22. try {
    23. //1、创建Socket对象,指明服务器端的IP和端口号
    24. InetAddress inet = InetAddress.getByName("127.0.0.1");
    25. socket = new Socket(inet,8899);
    26. //2、获取输出流,用于输出数据
    27. os = socket.getOutputStream();
    28. //3、写出数据
    29. os.write("你好,我是客户端mm".getBytes());
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. } finally {
    33. //4、关闭资源
    34. if(os !=null) {
    35. try {
    36. os.close();
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. if(socket != null){
    42. try {
    43. socket.close();
    44. } catch (IOException e) {
    45. e.printStackTrace();
    46. }
    47. }
    48. }
    49. }
    50. // 服务端
    51. @Test
    52. public void server() {
    53. ServerSocket ss = null;
    54. Socket socket = null;
    55. InputStream is = null;
    56. ByteArrayOutputStream baos = null;
    57. try {
    58. //1、创建服务器端的ServerSocket,指明自己的端口号
    59. ss = new ServerSocket(8899);
    60. //2、调用accept()表示接受来自于客户端的socket
    61. socket = ss.accept();
    62. //3、获取输入流
    63. is = socket.getInputStream();
    64. //4、获取输入流的数据
    65. baos = new ByteArrayOutputStream();
    66. byte[] buffer = new byte[5];
    67. int len;
    68. while((len = is.read(buffer)) != -1){
    69. baos.write(buffer,0,len);
    70. }
    71. System.out.println(baos.toString());
    72. } catch (IOException e) {
    73. e.printStackTrace();
    74. } finally {
    75. //5、关闭资源
    76. if(baos != null){
    77. try {
    78. baos.close();
    79. } catch (IOException e) {
    80. e.printStackTrace();
    81. }
    82. }
    83. if(is != null){
    84. try {
    85. is.close();
    86. } catch (IOException e) {
    87. e.printStackTrace();
    88. }
    89. }
    90. if(socket != null){
    91. try {
    92. socket.close();
    93. } catch (IOException e) {
    94. e.printStackTrace();
    95. }
    96. }
    97. if(ss != null){
    98. try {
    99. ss.close();
    100. } catch (IOException e) {
    101. e.printStackTrace();
    102. }
    103. }
    104. }
    105. }
    106. }

    用JAVA SOCKET编程,读服务器几个字符,再写入本地显示,代码:

    1. //服务端
    2. public class Server {
    3. private ServerSocket ss;
    4. private Socket socket;
    5. private BufferedReader in;
    6. private PrintWriter out;
    7. public Server() {
    8. try {
    9. ss = new ServerSocket(10000);
    10. while (true) {
    11. socket = ss.accept();
    12. String RemoteIP = socket.getInetAddress().getHostAddress();
    13. String RemotePort = ":" + socket.getLocalPort();
    14. System.out.println("A client come in!IP:" + RemoteIP + RemotePort);
    15. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    16. String line = in.readLine();
    17. System.out.println("Cleint send is :" + line);
    18. out = new PrintWriter(socket.getOutputStream(), true);
    19. out.println("Your Message Received!");
    20. out.close();
    21. in.close();
    22. socket.close();
    23. }
    24. } catch (IOException e) {
    25. out.println("wrong");
    26. }
    27. }
    28. publicstaticvoid main(String[] args) {
    29. new Server();
    30. }
    31. };
    32. //客户端
    33. public class Client {
    34. Socket socket;
    35. BufferedReader in;
    36. PrintWriter out;
    37. public Client() {
    38. try {
    39. System.out.println("Try to Connect to 127.0.0.1:10000");
    40. socket = new Socket("127.0.0.1", 10000);
    41. System.out.println("The Server Connected!");
    42. System.out.println("Please enter some Character:");
    43. BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
    44. out = new PrintWriter(socket.getOutputStream(), true);
    45. out.println(line.readLine());
    46. in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    47. System.out.println(in.readLine());
    48. out.close();
    49. in.close();
    50. socket.close();
    51. } catch (IOException e) {
    52. out.println("Wrong");
    53. }
    54. }
    55. public static void main(String[] args) {
    56. new Client();
    57. }
    58. }

    UDP网络通信:

    1. package com.JavaWebTest;
    2. import org.junit.Test;
    3. import java.io.IOException;
    4. import java.net.*;
    5. public class UDPTest {
    6. // 发送端
    7. @Test
    8. public void sender() throws IOException {
    9. DatagramSocket socket = new DatagramSocket();
    10. String str = "我是你爹";
    11. byte[] data = str.getBytes();
    12. InetAddress inet = InetAddress.getLocalHost();
    13. DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
    14. socket.send(packet);
    15. socket.close();
    16. }
    17. //接收端
    18. @Test
    19. public void receiver() throws IOException {
    20. DatagramSocket socket = new DatagramSocket(9090);
    21. byte[] buffer = new byte[100];
    22. DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
    23. socket.receive(packet);
    24. System.out.println(new String(packet.getData(),0,packet.getLength()));
    25. socket.close();
    26. }
    27. }

    URL(Uniform Resourece Locator)编程:
    URL的基本结构由5部分组成:
    <传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
    例如: http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123

    URL编程例子:

    1. package com.JavaWebTest;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.HttpURLConnection;
    6. import java.net.MalformedURLException;
    7. import java.net.URL;
    8. public class URLTest1 {
    9. public static void main(String[] args) throws IOException {
    10. HttpURLConnection urlConnection = null;
    11. InputStream is = null;
    12. FileOutputStream fos = null;
    13. try {
    14. URL url = new URL("http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123");
    15. urlConnection = (HttpURLConnection) url.openConnection();
    16. urlConnection.connect();
    17. is = urlConnection.getInputStream();
    18. fos = new FileOutputStream("beauty3.jpg");
    19. byte[] buffer = new byte[1024];
    20. int len;
    21. while((len = is.read(buffer)) != -1){
    22. fos.write(buffer,0,len);
    23. }
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. } finally {
    27. // 关闭资源
    28. if(is != null){
    29. try {
    30. is.close();
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. if(is != null){
    36. try {
    37. fos.close();
    38. } catch (IOException e) {
    39. e.printStackTrace();
    40. }
    41. }
    42. }
    43. urlConnection.disconnect();
    44. }
    45. }