标签:
网络编程
实现计算机互联的三要素:
1、IP地址
本机回路IP:127.0.0.1
2、端口号(逻辑端口):用于标识应用程序。
端口号0~65535之间的整数;0~1024之间大部分已被用于一些知名的网络服务和应用,所以现在开发的网络应用程序端口号一般是1024以后的整数。
3、通信协议
TCP:三次握手机制,面向连接,稍慢。可靠
UDP:无连接,每个数据报大小限制在64K内。传输快。不可靠。
网络参考模型
TCP/IP参考模型
应用层:javaWeb开发
传输层:TCP/UDP
网际层:IP
主机至网络层:有线还是无线,光纤还是电话线等
描述IP的java包:java.net 下的InetAddress类。
- mport java.net.InetAddress;
- import java.util.Arrays;
-
- public class IPDemo {
-
- public static void main(String[] args) throws Exception {
-
-
-
- InetAddress[] ias = InetAddress.getAllByName("www.baidu.com");
- for(InetAddress ia : ias) {
- System.out.println(ia.getHostAddress() + "..." + ia.getHostName());
- }
- }
-
- }
网络编程:其实就是Socket编程
通信的两端都有Socket, 数据在两个Socket间通过IO传输。
UDP传输:DatagramSocket和DatagramPacket
一个简单的UDP通信示例
- class UDPsend {
- public static void main(String[] args) throws Exception {
-
- DatagramSocket ds = new DatagramSocket();
-
- byte[] buf = "Hello, java".getBytes();
- DatagramPacket dp = new DatagramPacket(buf, 0, buf.length, InetAddress.getByName("127.0.0.1"), 8888);
-
-
- ds.send(dp);
- System.out.println("发送成功");
- }
- }
-
- class UDPreceive {
- public static void main(String[] args) throws Exception {
-
- DatagramSocket ds = new DatagramSocket(8888);
-
- byte[] buf = new byte[1024];
- DatagramPacket dp = new DatagramPacket(buf, 0, buf.length);
-
- ds.receive(dp);
-
- String ip = dp.getAddress().getHostAddress();
- String data = new String(dp.getData(), 0, dp.getLength());
- int port = dp.getPort();
- System.out.println(ip + ", " + port + ", " + data);
-
- ds.close();
- }
- }
需求:用TCP完成一个英文文本大小写转换的客户端和服务器端
客户端发送一个英文字符串到服务端,服务端返回一个将字符串转为全大写后的信息到客户端
- import java.net.*;
- import java.io.*;
- public class TCPTextCast {
-
- public static void main(String[] args) {
-
- }
-
- }
- class TCPClient1 {
-
- public static void main(String[] args) throws Exception {
- Socket s = new Socket("127.0.0.1", 10003);
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
-
- BufferedWriter bufOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
-
-
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
-
- String line = null;
- while((line=br.readLine())!= null) {
- if("over".equals(line))
- break;
- bufOut.write(line);
- bufOut.newLine();
- bufOut.flush();
-
-
- System.out.println("Server: " + bufIn.readLine());
- }
-
- br.close();
- s.close();
- }
-
- }
- class TCPServer1 {
-
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(10003);
- Socket s = ss.accept();
-
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip + "...connceted");
-
- BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
- BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
-
- String data = null;
- while((data=br.readLine()) != null) {
-
- System.out.println(ip + "... : " + data);
-
- bw.write(data.toUpperCase());
- bw.newLine();
- bw.flush();
- }
-
- s.close();
- ss.close();
- }
-
- }
TCP上传文件实例代码:
-
- import java.net.*;
- import java.io.*;
- public class FileLoader {
-
- public static void main(String[] args) {
-
- }
-
- }
-
- class client {
- public static void main(String[] args) throws Exception {
-
- Socket fileLoaderSocket = new Socket("127.0.0.1", 10005);
-
- OutputStream out = fileLoaderSocket.getOutputStream();
- InputStream fileRead = new FileInputStream("G:\\Demo.mp3");
-
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=fileRead.read(buf)) != -1) {
- out.write(buf, 0, len);
- }
-
- fileLoaderSocket.shutdownOutput();
-
-
- BufferedReader in = new BufferedReader(new InputStreamReader(fileLoaderSocket.getInputStream()));
- String serverBack = in.readLine();
- System.out.println(serverBack);
-
-
- fileLoaderSocket.close();
- fileRead.close();
- }
- }
-
- class Server {
- public static void main(String[] args) throws Exception {
-
- ServerSocket ss = new ServerSocket(10005);
-
- Socket fileLoaderSocket = ss.accept();
-
-
- String ip = fileLoaderSocket.getInetAddress().getHostAddress();
- System.out.println(ip + "...conncected");
-
-
- InputStream in = fileLoaderSocket.getInputStream();
- OutputStream fileSave = new FileOutputStream("E:\\server.mp3");
- byte[] buf = new byte[1024];
- int len = 0;
- while((len=in.read(buf)) != -1) {
- fileSave.write(buf, 0, len);
- fileSave.flush();
- }
-
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fileLoaderSocket.getOutputStream()));
- out.write("文件上传成功");
- out.flush();
-
-
- ss.close();
- fileLoaderSocket.close();
- fileSave.close();
- }
- }
public Socket() 到底连接哪台主机?
它是用
public void connect(SocketAddress endpoint,int timeout) throws IOException
SocketAddress是abstract类,它有个子类InetSocketAddress ,此类实现 IP 套接字地址(IP 地址 + 端口号)。
ServerSocket可以指定能连接客户端的最大数:
public ServerSocket(int port,int backlog) throws IOException
域名解析:
1、首先找本机 C:\WINDOWS\system32\drivers\etc 下的hosts文件配对
2、若上述文件中不存在映射(域名-IP),则进行下面的步骤:
黑马程序员-Java基础之网络编程
标签:
原文地址:http://www.cnblogs.com/dan-ran/p/5071761.html