博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
socket编程
阅读量:2396 次
发布时间:2019-05-10

本文共 3581 字,大约阅读时间需要 11 分钟。

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);	}}

转载于:https://my.oschina.net/u/2356176/blog/466603

你可能感兴趣的文章
使用HttpClient爬取国内疫情数据
查看>>
引用传递和值传递有什么区别
查看>>
C++从入门到放肆!
查看>>
C++是什么?怎么学?学完了能得到什么?
查看>>
初学C语言没有项目练手怎么行,这17个小项目收下不谢
查看>>
学好C语言,你只需要这几句口诀!
查看>>
选择大于努力!0基础学好C语言编程,首先要掌握的是什么?
查看>>
C语言和其他语言的不得不说的差别!
查看>>
夫妻俩在互联网公司工作,年收入曝光,网友:这么高!
查看>>
程序员5年工作经验,因频繁跳槽被面试官压工资!
查看>>
职场中神奇的程序员,却常常被人说“太直”,这是什么样的思维?
查看>>
@初学编程的朋友们,如果你能学得这些方法,学习将会更快一步!
查看>>
C/C++编程笔记:C/C++ 的编译和链接
查看>>
C/C++编程知识分享:C++四种强制转换,教你多种类型转换方式!
查看>>
全球最厉害的 14 位程序员,你知道几位呢?最厉害的研究出了它!
查看>>
C/C++基础语法复习(二):C++ 面向对象编程,你需要知道的点
查看>>
简述 C语言 有和 C++ 的基本区别,你真的懂吗?(新手面试必学)
查看>>
刚进职场的程序员,请万分珍重你的第一份工作,不要轻易辞职!
查看>>
C/C++之QT攻略——在QT中容易遇到的那些坑,千万别踩了!
查看>>
@90后程序员,“颜值即正义”的现在,程序员应该如何更新穿搭?
查看>>