黑马程序员--第二十三天:网络编程第一天

---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ----------------------

 

//23-4

import java.net.*;

class  IPDemo
{
	public static void main(String[] args) throws Exception
	{
		//InetAddress i = InetAddress.getLocalHost();
		//System.out.println(i.toString());
		//System.out.println(i.getHostAddress());
		//System.out.println(i.getHostName());

		InetAddress in[] = InetAddress.getAllByName("www.baidu.com");
		
	/*	for (InetAddress i : in)
		{
			System.out.println(i.toString());
			System.out.println(i.getHostAddress());
			System.out.println(i.getHostName());
		}
		*/
		
		for (InetAddress i : in)
			System.out.println(i.toString());
	}
}


/*23-5
UDP特点:
1.将数据封装成数据包,不需要建立连接
2.每个数据包大小在64k内
3.面向无连接,是不可靠协议
4.不需要建立连接,速度快。(力求速度)
(每个包的小于64k,无连接,不可靠,但是速度快)

TCP特点:
1.建立连接,形成传输数据通道。
2.在连接过程中进行大数据量传输
3.通过三次握手完成连接,是可靠协议。
4.必须建立连接,效率会稍低
(三次握手建立连接,可靠,效率稍低,但是大数据量传输).
*/


/*23-6
Socket //可以理解为港口,网络通信其实就是Socket通信,数据在Socket间通过io传输。
*/


/*23-7
1.建立udpsocket服务
2.提供数据并将数据封装到数据包
3.通过socket的send功能把数据发出去
4.关闭资源
*/

import java.net.*;
/*
class UDPDemo 
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}
*/

class UDPSend
{
	public static void main(String[] args) throws Exception
	{
		//1.建立udpsocket服务
		DatagramSocket ds = new DatagramSocket(8888);
		
		//2.提供数据并将数据封装到数据包
		byte[] buf = "upd data".getBytes();
		DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("First-Pc"),10000);

		//3.通过socket的send功能把数据发出去
		ds.send(dp);

		//4.关闭资源
		ds.close();
		System.out.println("Hello World!");
	}
}

/*23-8
1.建立udpsocket服务
2.定义一个数据包,用于存储接收到的字节数据
3.通过socket服务的receive接收数据到包中
4.取出数据
5.关闭资源
*/

class UDPReceive
{
	public static void main(String[] args) throws Exception
	{
		//1.建立udpsocket服务,通常会监听一个端口,给接收网络应用程序定义一个数字表示
		DatagramSocket ds = new DatagramSocket(10000);
		
		while(true){
		//2.提供数据并将数据封装到数据包
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf,buf.length);

		//3.通过socket的receive功能接收数据
		ds.receive(dp);

		//4.获取包中的数据
		String ip = dp.getAddress().getHostAddress();
		dp.getData();
		String data = new String(dp.getData(),0,dp.getLength());

		int port = dp.getPort();

		System.out.println(ip+"::"+data+"::"+port);
		}

		//5.关闭资源
		//ds.close();
	}
}


//23-9

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

class UDPSend2
{
	public static void main(String[] args) throws Exception
	{
		//1.建立udpsocket服务
		DatagramSocket ds = new DatagramSocket(8888);
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		String line = null;
		while (!(line = bf.readLine()).equals("88"))
		{
			byte[] buf = line.getBytes();
			DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("255.255.255.255"),10000);
			ds.send(dp);
		}
		
		//2.提供数据并将数据封装到数据包
	//	byte[] buf = "upd data".getBytes();
		//DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("First-Pc"),10000);

		//3.通过socket的send功能把数据发出去
		//ds.send(dp);

		//4.关闭资源
		ds.close();
		System.out.println("Hello World!");
	}
}

class UDPReceive2
{
	public static void main(String[] args) throws Exception
	{
		//1.建立udpsocket服务,通常会监听一个端口,给接收网络应用程序定义一个数字表示
		DatagramSocket ds = new DatagramSocket(10000);
		
		while(true){
		//2.提供数据并将数据封装到数据包
		byte[] buf = new byte[1024*64];
		DatagramPacket dp = new DatagramPacket(buf,buf.length);

		//3.通过socket的receive功能接收数据
		ds.receive(dp);

		//4.获取包中的数据
		String ip = dp.getAddress().getHostAddress();
		dp.getData();
		String data = new String(dp.getData(),0,dp.getLength());

		int port = dp.getPort();

		System.out.println(ip+":");
		System.out.println(data+":--:"+port);
		}

		//5.关闭资源
		//ds.close();
	}
}


/*23-10
编写一个聊天程序。
*/

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


class  ChatDemo
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket sendSocket = new DatagramSocket();
		DatagramSocket receiveSocket = new DatagramSocket(10001);
		new Thread(new Send(sendSocket)).start();
		new Thread(new Receive(receiveSocket)).start();
		//System.out.println("Hello World!");
	}
}



class Send implements Runnable
{
	private DatagramSocket ds;
	public Send(DatagramSocket ds){
		this.ds = ds;
	}
	public void run()
	{
		try{
		//1.建立udpsocket服务
		//DatagramSocket ds = new DatagramSocket(8888);
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		String line = null;
		while (!(line = bf.readLine()).equals("88"))
		{
			byte[] buf = line.getBytes();
			DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("first-pc"),10001);
			ds.send(dp);
		}
		}catch(Exception e){
			throw new RuntimeException("发送失败");
		}
		
		//2.提供数据并将数据封装到数据包
	//	byte[] buf = "upd data".getBytes();
		//DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("First-Pc"),10000);

		//3.通过socket的send功能把数据发出去
		//ds.send(dp);

		//4.关闭资源
		finally{
		ds.close();
		}
		//System.out.println("Hello World!");
	}
}

class Receive implements Runnable
{
	private DatagramSocket ds;
	public Receive(DatagramSocket ds){
		this.ds = ds;
	}

	public void run() 
	{
		//1.建立udpsocket服务,通常会监听一个端口,给接收网络应用程序定义一个数字表示
		//DatagramSocket ds = new DatagramSocket(10000);
		try{
		while(true){
		//2.提供数据并将数据封装到数据包
		byte[] buf = new byte[1024*64];
		DatagramPacket dp = new DatagramPacket(buf,buf.length);

		//3.通过socket的receive功能接收数据
		ds.receive(dp);

		//4.获取包中的数据
		String ip = dp.getAddress().getHostAddress();
		dp.getData();
		String data = new String(dp.getData(),0,dp.getLength());

		int port = dp.getPort();

		System.out.println(ip+":");
		System.out.println(data+":--:"+port);
		}
		}catch(Exception e){
			throw new RuntimeException("接收失败");
		}

		//5.关闭资源
		//ds.close();
	}
}


/*23-11
1.Socket,对象建立时连接主机
2.ServerSocket
*/

/*
1.创建Socket服务,连接主机和端口
2.
*/

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

/*
class TCPDemo 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		Socket s = new Socket("First-Pc",10003);

		OutputStream out = s.getOutputStream();
		out.write("TCP data");

		s.close();
		System.out.println("Hello World!");
	}
}

*/
class ClientDemo 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		Socket s = new Socket("First-Pc",10004);

		OutputStream out = s.getOutputStream();
		out.write("TCP data".getBytes());

		s.close();
		System.out.println("Hello World!");
	}
}

/*
1.建立服务端的ServerSocket,并监听一个端口。
2.获取连接过来的客户端对象。通过accept方法(阻塞式)
3.客户端如果发过来数据,服务端要使用对应的客户端对象,并获得客户端对象的读取流来读取发过来的数据
4.关闭服务端(可选操作);
*/

class ServerDemo 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		ServerSocket ss = new ServerSocket(10004);
		Socket s = ss.accept();//通过accept获取连接过来的客户端 Socket对象

		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+":....");

		InputStream in = s.getInputStream();

		byte[] buf = new byte[1024];
		int len= in.read(buf);

		System.out.println(new String(buf,0,len));

		s.close();
		System.out.println("Hello World!");
	}
}


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

/*23-12
class TCPDemo 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		Socket s = new Socket("First-Pc",10003);

		OutputStream out = s.getOutputStream();
		out.write("TCP data");

		s.close();
		System.out.println("Hello World!");
	}
}

*/
class ClientDemo2 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		Socket s = new Socket("First-Pc",10004);

		OutputStream out = s.getOutputStream();
		out.write("TCP data".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();
	}
}


class ServerDemo2 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		ServerSocket ss = new ServerSocket(10004);
		Socket s = ss.accept();//通过accept获取连接过来的客户端对象

		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+":....");

		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("hello".getBytes());

		s.close();
		System.out.println("Hello World!");
	}
}


/*23-13
需求:建立一个文本转换服务器。
客户端给服务端发送文本,服务端会将文本转换成大写再返回给客户端。
而且客户端可以不断的进行文本转换,当客户端输入over时,转换结束。
*/

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

class ClientTransText
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		Socket s = new Socket("First-Pc",10005);
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
		//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		//PrintStream br = new PrintStream(System.in);
		//PrintStream in = new PrintStream(s.getInputStream());
		PrintStream out = new PrintStream(s.getOutputStream(),true);
		
		String line = null;
		while (!(line = br.readLine()).equals("over"))
		{
			//out.write(line);
			//out.newLine();
			//out.flush();
			out.println(line);

			String up = in.readLine();
			System.out.println(up);
		}

		s.close();
	}
}


class ServerTransText 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		ServerSocket ss = new ServerSocket(10005);
		Socket s = ss.accept();//通过accept获取连接过来的客户端对象
		
		BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
		//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
		//PrintStream in = new PrintStream(s.getInputStream());
		PrintStream out = new PrintStream(s.getOutputStream(),true);

		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+":....");

		String line = null;

		while ((line = in.readLine())!=null)
		{
			System.out.println(line);
		/*
			out.write(line.toUpperCase());
			out.newLine();
			out.flush();	*/

			out.println(line.toUpperCase());
		}

		s.close();
		ss.close();
	}
}

/*
例子的问题:
现象:客户端和服务端都在莫名的等待。
原因:客户端和服务端都有阻塞方法,这些方法都没读到结束标记。因此双方同时一直等待对方
*/
//客户端的close()方法在结束是会给服务端发-1,用于服务端结束程序。



/*23-14

*/

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

class ClientUpdataText
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		Socket s = new Socket("122.244.71.219",10006);
		BufferedReader br = new BufferedReader(new FileReader("ChatDemo.java"));

		PrintStream out = new PrintStream(s.getOutputStream(),true);
		
		String line = null;
		while ((line = br.readLine())!=null)
		{
			out.println(line);
		}

		//out.println("over");

		s.shutdownOutput();

		BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
		
		String str = in.readLine();//????????????这里怎么会出错呢?与视频上没什么不同,疑惑!!!
		//原因是 服务端的PrintStream 不能传字符流,要把它改为 PrintWriter。
		System.out.println(str);
		
		br.close();
		s.close();
	}
}


class ServerUpdataText 
{
	public static void main(String[] args) throws Exception
	{
		//创建Socket服务,连接主机和端口
		ServerSocket ss = new ServerSocket(10006);
		Socket s = ss.accept();//通过accept获取连接过来的客户端对象

		BufferedWriter bw = new BufferedWriter(new FileWriter("ChatDemo.txt"));
		
		BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

		//PrintStream out = new PrintStream(s.getOutputStream(),true);

		String ip = s.getInetAddress().getHostAddress();
		System.out.println(ip+":....");

		String line = null;

		while ((line = in.readLine())!=null)
		{
			//if (line.equals("over"))
			//break;
			
			bw.write(line);
			bw.newLine();
			bw.flush();
			//System.out.println(line);
		/*
			out.write(line.toUpperCase());
			out.newLine();
			out.flush();	*/
		}

		//PrintStream out = new PrintStream(s.getOutputStream(),true);
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		out.println("上传成功");

		bw.close();
		ss.close();
		s.close();
	}
}

/*
例子的问题:
现象:客户端和服务端都在莫名的等待。
原因:客户端和服务端都有阻塞方法,这些方法都没读到结束标记。因此一直等待
*/
//客户端的close()方法在结束是会给服务端发-1,用于服务端结束程序。


---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ---------------------- 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值