day23套接字socket网络传输TCP和udp

/*
客户端
通过查阅Socket对象,发现在该对象建立时,就可以连接指定主机
因为tcp是面向连接,所以在建立socket服务时
就要有服务端存在,并连接成功,形成通路后,在该通道进行数据的传输


需求:给服务端发送给一个文本数据
步骤
1.创建socket服务,并指定要连主机和端口。

*/
import java.net.*;
import java.io.*;
class TcpClient
{
	public static void main(String[] args) 
	{
		//创建客户端socket服务,并指定要连主机和端口。
		Socket s=new Socket("127.0.0.1",10003);

		//为了发送数据,应该获取socket流中的输出流
		OutputStream out=s.getOutputStream();

		out.write("tcp ge men lai le".getByte());

		s.close();
	}
}

/*
需求:定义端点接收数据并打印到控制台
服务端:
1.建立服务端的socket服务,ServerSocket();
	并监听一个端口
2.获取连接过来的客户端对象
	通过ServerSocket的accept方法,没有连接就等,这个方法阻塞式
3.客户端如果发过来数据,那么服务端要使用对应的客户端对象,并
	获取到该客户端对象的读取流来读取发过来的数据,打印控制台
4.关闭服务端。(可选)
*/
class TcpServer
{
	public static void main(String[] args) throws Exception
	{
		//建立服务端的socket服务	并监听一个端口
		ServerSocket ss=new ServerSocket(10003);

		// 通过accept方法获取连接过来的客户端对象
		Socket s=ss.accept();

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

		//获取客户端发送的数据,那么使用客户端的读取流来读取数据
		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();
	}
}
class  TcpDemo
{
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
	}
}

import java.net.*;
/*
需求:通过udp传输方式,将一段文字数据发送出去
思路
1.建立updsocket服务。
2.提供数据,将数据封装到数据包中
3.通过socket服务的发送功能,将数据包发送出去
4.关闭资源。
*/
class UdpSend
{
	public static void main(String[] args)  throws Exception
	{
		//建立updsocket服务,通过DatagramSocket
		DatagramSocket ds=new DatagramSocket();

		//提供数据,将数据封装到数据包中
		//DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
		
		byte[] buf="udp ge men lai le".getBytes();
		DatagramPacket dp=
			new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10000);
		
		//通过socket服务的发送功能send方法,将数据包发送出去
		ds.send(dp);

		//关闭资源。
		ds.close();
		System.out.println("Hello World!");
	}
}
/*
需求:
定义一个应用程序,用于接受udp协议传输的数据并处理


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

2.定义一个数据包,因为要存储接受到的字节数据,
因为数据包对象中有更多功能提取字节数据中不同数据信息
3.通过socketf服务的receive方法将收到的数据存入已经定义好的数据包中
4.通过数据包对象特有功能,将数据取出,打印到控制台
5.关闭资源
*/
class UdpRece
{
	public static void main(String[] args)  throws Exception
	{
		//创建udp socket 建立端点
		DatagramSocket ds=new DatagramSocket(10000);
		
		while(true)
		{
		//定义数据包,用于存储数据
		byte[] buf=new byte[1024];
		DatagramPacket dp=new DatagramPacket(buf,buf.length);

		//通过socketf服务的receive方法将收到的数据存入已经定义好的数据包中
		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+"::"+data+"::"+port);
		}
		//关闭资源
		//ds.close();
		
	}

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


import java.net.*;
import java.io.*;
class  UdpSend2
{
	public static void main(String[] args) 
	{
		DatagramSocket ds=new DatagramSocket();

		BufferedReader bufr=
			new BufferedReader(new InputStreamReader(System.in));

		String line=null;

		while((line=bufr.readLine())!=null)
		{
			if ("886".equal(line))
			{
				break ;
			}

			byte[] buf=line.getBytes();

			DatagramPacket dp=
			new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10001);

			ds.send(dp);
		}
		ds.close();
	}
}
class  UdpRece2
{
	public static void main(String[] args) 
	{
		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+"::"+data+"::"+port);
		}
	}
}

/*
程序编写一个聊天
有需要收数据的部分,和发数据的部分
这两部分需要同时执行。
那就用到多线程技术
一个线程控制收,一个线程发

因为收和发动作不一致,所以要定义run方法
*/
import java.net.*;
import java.io.*;
class Send implements Runnable
{
	private DatagramSocket ds;
	public Send(DatagramSocket ds)
	{
		this.ds=ds;
	}
	public void run()
	{
		try
		{
			BufferedReader bufr=
			new BufferedReader(new InputStreamReader(System.in));

			String line=null;

			while((line=bufr.readLine())!=null)
			{
				if ("886".equal(line))
				{
					break ;
				}
				byte[] buf=line.getBytes();

				DatagramPacket dp=
						new DatagramPacket(buf,buf.length,InetAddress.getByName("127.0.0.1"),10002);

				ds.send(dp);
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException("发送端失败");
		}
	}
}

class Rece implements Runnable
{
	private DatagramSocket ds;
	public Rece(DatagramSocket ds)
	{
		this.ds=ds;
	}
	public void run()
	{
		try
		{
			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+"::"+data+"::"+port);
		}
		catch (Exception e)
		{
			throw new RuntimeException("接受端失败");
		}
	}
}
class  ChatDemo
{
	public static void main(String[] args) throws Exception
	{
		DatagramSocket sendSocket=new DatagramSocket();
		DatagramSocket receSocket=new DatagramSocket(10002);

		new Thread(new Send(sendSocket)).start();
		new Thread(new Rece(receSocket)).start();

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值