socket编程

ip标识一台计算机,端口则是标识一个应用程序。

/*
网络层就是给定要去的ip地址。。。
通过物理层传播出去,网线就是标准的物理层设备。。。。。。光纤和无线也是。。。
Scoket就是插口。。。。。。
每一应用程序都有这样一个插口,通过插口调用底层资源来通信。。。
想要通信就一定要有插口。。。
网络编程就是Scoket编程。。。
reciver是阻塞方法,Readline也是阻塞方法。。。

Ip地址最后一段一般都是1到254   0和255是不能使用的,0一般代表的是一个网络段,255代表的则是广播段。。。。。。

ip标识一台计算机,端口则是标识一个应用程序。。。。。。
*/
import java.net.*;
import java.io.*;
class anli
{
	public static void main (String[] args)throws Exception  //网络编程的使用一定要抛出异常的。。。
	{
		//oftenUse();
		Upddemo();
	}
	public static void Upddemo()throws Exception
	{
		DatagramSocket ds = new DatagramSocket();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = null;
		while((line = br.readLine())!= null)
		{
			if("over".equals(line))
			{
				break;
			}
			byte[] bt = line.getBytes();
			DatagramPacket dp = new DatagramPacket(bt,bt.length,InetAddress.getByName("192.168.109.119"),1000); //1000是发送的端口
			ds.send(dp);
		}
		//byte[] bt = "chenruibing".getBytes();
		
		ds.close();
	}
	/*public static void oftenUse()throws Exception
	{
		InetAddress i = InetAddress.getLocalHost(); //静态方法,返回的是计算机的名字和ip值
		//InetAddress i = InetAddress.getByName("192.168.109.119");  //返回的也只是ip地址,可以查找局域网或者当时正处于网络上的主机
		//InetAddress[] i = InetAddress.getAllByName("WCL2BX2RZXDQBKM");//由主机名字,返回其 IP 地址所组成的数组,返回值是数组
		sop(i.toString());
		sop(i.getHostName());  //获取主机名
		sop(i.getHostAddress());//获取ip
		for(InetAddress j : i)
		{
			sop(j);
		}
	}*/
	public static void sop (Object obj)
	{
		System.out.println(obj);
	}
}


class rec //接收端一般是不关闭的
{
	public static void main (String[] args)throws Exception
	{
		DatagramSocket ds = new DatagramSocket(1000);  //接受方要监听端口。。。
		while(true)
		{
			byte[] bt = new byte[1024];  //用来存放发送过来的数据
			DatagramPacket dp = new DatagramPacket(bt,bt.length);//数组不用加括号,字符串才要用括号
			ds.receive(dp);		//好像并不是一定要接受的。。。
			String ip = dp.getAddress().getHostAddress();
			String data = new String(dp.getData(),0,dp.getLength());//获取缓冲区的数据其实就是获取发过来的数据
			int port = dp.getPort();
			sop(ip+"::"+data+"::"+port); //port指定端口是随机的,因为在发送端哪里没有指定一个确定的发送端口。。。
		}
		
	}
	public static void sop (Object obj)
	{
		System.out.println(obj);
	}
	
}

oneThread to conversation

import java.io.*;
import java.net.*;
public class anli
{
	public static void main (String[] args)throws Exception
	{
		DatagramSocket send = new DatagramSocket();
		DatagramSocket rec = new DatagramSocket(10086);
		new Thread(new sendclass(send)).start();
		new Thread(new recclass(rec)).start();
	}

}
class sendclass implements Runnable
{
	private DatagramSocket ds;
	sendclass(DatagramSocket ds)
	{
		this.ds = ds;
	}
	public void run ()
	{
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while((line = br.readLine())!=null)
			{
				if("over".equals(line))
					break;
				byte[] bt = line.getBytes();   //以字节的方式传递数据
				DatagramPacket dp = new DatagramPacket(bt,bt.length,InetAddress.getByName("192.168.109.119"),10086);
				ds.send(dp);
			}
			br.close();
			ds.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException ("发送端失败");
		}
	}
}
class recclass implements Runnable
{
	private DatagramSocket ds;
	recclass(DatagramSocket ds)
	{
		this.ds = ds;
	}
	public void run ()
	{
		try
		{
			while(true)
			{
				byte[] bt = new byte[1024];
				DatagramPacket dp = new DatagramPacket(bt,bt.length);//接受包
				ds.receive(dp);
				String ip = dp.getAddress().getHostAddress();  //使用接受包来获取数据   因为数据都是放在接受包里面的。。。
				String data = new String(dp.getData(),0,dp.getLength());
				sop(ip+"::"+data);
			}
		}
		catch (Exception e)
		{
			throw new RuntimeException ("接受端失败");
		}
	}
	public static void sop (Object obj)
	{
		System.out.println(obj);
	}
}



你可能感兴趣的:(socket编程)