run_wind

导航

黑马程序员——JAVA基础之网络编程

 

 

网络编程

 

网络模型:OSI参考模型和TCP/IP参考模型

 

 

 

网络通讯三要素:

 

IP地址:InetAddress
•  网络中设备的标识
•  不易记忆,可用主机名
•  本地回环地址:127.0.0.1 主机名:localhost

端口号
•  用于标识进程的逻辑地址,不同进程的标识
•  有效端口:0~65535,其中0~1024系统使用或保留端口。

传输协议
•  通讯的规则
•  常见协议:TCP,UDP

 

 

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPDemo {
	public static void main(String[] args) throws UnknownHostException {
		//该方法返回一个本机主机对象
		InetAddress i = InetAddress.getLocalHost();
		//获取本地主机名称
		System.out.println("name:"+i.getHostName());
		//获取本地主机地址
		System.out.println("address:"+i.getHostAddress());
		//获取指定名称的主机对象
		InetAddress i1 = InetAddress.getByName("www.baidu.com");
		//获取指定主机的名称
		System.out.println("name:"+i1.getHostName());
		//获取指定主机的地址
		System.out.println("address:"+i1.getHostAddress());
	}
}


 

UDP和TCP区别:

 

UDP
•  将数据及源和目的封装成数据包中,不需要建立连接
•  每个数据报的大小在限制在64k内
•  因无连接,是不可靠协议
•  不需要建立连接,速度快

TCP
•  建立连接,形成传输数据的通道。
•  在连接中进行大数据量传输
•  通过三次握手完成连接,是可靠协议
•  必须建立连接,效率会稍低

 

Socket

Socket就是为网络服务提供的一种机制。通信的两端都有Socket。网络通信其实就是Socket间的通信。数据在两个Socket间通过IO传输。

 


UDP发送端与接收端是两个独立的运行程序。

 

 

建立UDP发送端的一般步骤:
1.建立发送端,接收端。
2.建立数据包。
3.调用Socket的发送接收方法。
4.关闭Socket。

 

 

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPSend {
	public static void main(String[] args) throws IOException {
		//建立UDP服务,通过DatagramSocket对象
		DatagramSocket ds = new DatagramSocket();
		//确定数据,并封装成数据包
		byte[] buf = "udp hello!!".getBytes();
		DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.138.1.101"),10000);
		//通过Socket服务,将已有的数据包发送出去。通过send方法
		ds.send(dp);
		//关闭资源
		ds.close();
	}
}


 

import java.net.*;
import java.io.*;

class UdpDemo 
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}

class UdpSend
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket ds = new DatagramSocket();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String len = null;
		while ((len = br.readLine()) != null)
		{
			if (len.equals("886"))
				break;
			byte[] buf = len.getBytes();
			DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("119.187.244.160"),10001);
	
			ds.send(dp);
		}
		ds.close();
	}
}

class UdpRece
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket ds = new DatagramSocket(10001);

		while (true)
		{
			byte[] buf = new byte[1024];
			DatagramPacket dp = new DatagramPacket(buf,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);
		}	
	}
}


 

定义UDP的接收端的一般步骤

1,定义udpsocket服务。通常会监听一个端口,其实就是给这个接受网络应用程序定义数字标识。方便于明确哪些数据过来该应用程序可以处理。

2.定义一个数据包,因为要存储接收到的字节数据。因为数据包对象中有更多功能可以提取字节数据中的不同数据信息。

3.通过Socket服务的receive方法将接受到的数据存入已定义好的数据包中。

4.通过数据包对象的特有功能。将这些不同的数据取出。

5.关闭资源

 

 

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPReceive {
	public static void main(String[] args) throws IOException {
		//创建udpsocket,建立端点
		DatagramSocket ds = new DatagramSocket(1000);
		//定义数据包,用于存储数据
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf,buf.length);
		//通过服务的receive方法将收到数据存入数据包中
		ds.receive(dp);
		//通过数据包的方法获取其中的数据
		String ip = dp.getAddress().getHostAddress();
		String data = new String(dp.getData(),0,dp.getLength());
		int port = dp.getPort();
		//关闭资源
		ds.close();
	}
}


 

 

TCP传输

Socket和ServerSocket

建立客户端和服务器端

建立连接后,通过Socket中的IO流进行数据的传输

关闭socket

同样,客户端与服务器端是两个独立的应用程序。 

 

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {
	public static void main(String[] args) throws UnknownHostException, IOException {
		//创建客户端的Socket服务,指定目的主机和端口
		Socket s = new Socket("192.168.1.101",10086);
		//为了发送数据,应该获取Socket流中的输出流
		OutputStream out = s.getOutputStream();
		out.write("hello tcp".getBytes());
		s.close();
	}
}


 

 

基本思路(客户端)

客户端需要明确服务器的ip地址以及端口,这样才可以去试着建立连接,如果连接失败,会出现异常。

连接成功,说明客户端与服务端建立了通道,那么通过IO流就可以进行数据的传输,而Socket对象已经提供了输入流和输出流对象,通过getInputStream(),getOutputStream()获取即可。

与服务端通讯结束后,关闭Socket。

 

 

基本思路(服务端)

服务端需要明确它要处理的数据是从哪个端口进入的。

当有客户端访问时,要明确是哪个客户端,可通过accept()获取已连接的客户端对象,并通过该对象与客户端通过IO流进行数据传输。

当该客户端访问结束,关闭该客户端。

 

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
	public static void main(String[] args) throws IOException {
		//建立服务端Socket服务,并监听一个端口
		ServerSocket ss = new ServerSocket(10086);
		//通过accept方法获取连接过来的客户端对象。
		Socket s = ss.accept();
		//获取客户端的ip
		String ip = s.getInetAddress().getHostAddress();
		//获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据
		InputStream in = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		System.out.println(new String(buf,0,len));
		s.close();
		ss.close();//可选操作
	}
}


 

 

演示TCP传输的客户端和服务端的互访
需求:客户端给服务端发送数据,服务端收到后,给客户端反馈信息。

 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class TcpClient {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket s = new Socket("192.168.1.101",10086);
		OutputStream out = s.getOutputStream();
		out.write("fuwuduan nihao".getBytes());
		InputStream in = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		System.out.println(new String(buf,0,len));
		s.close();
	}
}
public class TcpServer {
	public static void main(String[] args) throws IOException {
		ServerSocket ss = new ServerSocket(10086);
		Socket s = ss.accept();
		String ip = s.getInetAddress().getHostName();
		InputStream in = s.getInputStream();
		byte[] buf = new byte[1024];
		int len = in.read(buf);
		System.out.println(new String(buf,0,len));
		OutputStream out = s.getOutputStream();
		out.write(("kehuduan nihao"+ip).getBytes());
		s.close();
		ss.close();
	}
}


 

 

TCP上传图片示例:
客户端思路:1.建立端点。2.读取客户端已有的图片数据。3.通过Socket输出流将数据发给服务端。4读取服务器端反馈的信息。5.关闭资源
 

 

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class PicClient {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket s = new Socket("192.168.1.101",10086);
		FileInputStream fis = new FileInputStream("ytl.bmp");
		OutputStream out = s.getOutputStream();
		byte[] buf = new byte[1024];
		int len = 0;
		while((len=fis.read(buf))!=-1)
		{
			out.write(buf, 0, len);
		}
		//告诉服务器端数据已经写完
		s.shutdownOutput();
		InputStream in = s.getInputStream();
		byte[] bufIn = new byte[1024];
		int num = in.read(bufIn);
		System.out.println(new String(bufIn,0,num));
		fis.close();
		s.close();
	}
}


 

 

 

服务器思路:1.建立端点。2.建立一个存储数据的文件。3.通过Socket读取流读取客户端发来的数据。4.写给客户端反馈信息。5关闭资源

 

 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class PicServer {
	public static void main(String[] args) throws IOException {
		ServerSocket ss = new ServerSocket(10086);
		while(true)
		{
			Socket s = ss.accept();
			InputStream in = s.getInputStream();
			FileOutputStream fos = new FileOutputStream("server.bmp");
			byte[] buf = new byte[1024];
			int len = 0;
			while((len=in.read(buf))!=-1)
			{
				fos.write(buf,0,len);
			}
			OutputStream out = s.getOutputStream();
			out.write("上传成功!!".getBytes());
			fos.close();
			s.close();
		}
	}
}


 

 

TCP客户端并发上传图片
上个示例有个局限性。当A客户端连接上以后。被服务器端获取到。服务器执行具体流程。这时B客户端连接,只有等待。因为服务器端还没有处理完A客户端的请求,还有循环回来执行下次accept方法。所有暂时获取不到B客户端对象。那么为了可以让多个客户端同时并发访问服务器。服务器端最好就是将每个客户端封装到一个单独的线程中,这样,就可以同时处理多个客户端请求。只要明确了每个客户端要在服务端执行的代码即可。将该代码存入run方法中。

 

 

 

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class PicServer {
	public static void main(String[] args) throws IOException {
		ServerSocket ss = new ServerSocket(10086);
		while(true)
		{
			Socket s = ss.accept();
			new Thread(new PicThread(s)).start();
		}
	}
}
class PicThread implements Runnable
{
	private Socket s;
	PicThread(Socket s)
	{
		this.s = s;
	}
	public void run() {
		String ip = s.getInetAddress().getHostName();
		try {
			InputStream in = s.getInputStream();
			FileOutputStream fos = new FileOutputStream("server.bmp");
			byte[] buf = new byte[1024];
			int len = 0;
			while((len=in.read(buf))!=-1)
			{
				fos.write(buf,0,len);
			}
			OutputStream out = s.getOutputStream();
			out.write((ip+"上传成功!!").getBytes());
			fos.close();
			s.close();
		} catch (Exception e) {
			throw new RuntimeException("上传失败!"+ip);
		}
	}
}


 

 

 

 

 

自定义服务端
客户端:浏览器  服务端:自定义

 

 

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
	public static void main(String[] args) throws IOException {
		ServerSocket ss  = new ServerSocket(10086);
		Socket s = ss.accept();
		PrintWriter out  = new PrintWriter(s.getOutputStream(),true);
		out.println("欢迎您!!");
		s.close();
	}
}


 

 

 

URL-URLConnecetion
示例演示URL对象的常用方法

 

import java.net.MalformedURLException;
import java.net.URL;

public class URLDemo {
	public static void main(String[] args) throws MalformedURLException {
		URL url = new URL("http://192..168.1.101:8080/myweb/demo.html");
		System.out.println("protocol:"+url.getProtocol());
		//获取此URL的协议名称
		System.out.println("host:"+url.getHost());
		//获取此URL的端口号
		System.out.println("port:"+url.getPort());
		//获取此URL的路径
		System.out.println("path:"+url.getPath());
		//获取此URL的文件名
		System.out.println("file:"+url.getFile());
		//获取此URL所带的参数信息
		System.out.println("query:"+url.getQuery());
	}
}



 

 

 

 

 

posted on 2014-12-02 15:50  run_wind  阅读(139)  评论(0编辑  收藏  举报