您的位置:首页 > 理论基础 > 计算机网络

数据数据包JAVASE----17----网络编程

2013-05-14 20:35 405 查看
新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正









1,IP

import java.net.InetAddress; import java.net.UnknownHostException; public class IPdemo { /** * @param args */ public static void main(String[] args) { try { // InetAddress i= InetAddress.getLocalHost(); // System.out.println(i.toString()); // System.out.println("address:"+i.getHostAddress()); // System.out.println("name:"+i.getHostName()); //InetAddress ia= InetAddress.getByName("192.168.1.101"); InetAddress[] ia= InetAddress.getAllByName("www.baidu.com"); for(InetAddress i :ia) { System.out.println(i); } } catch (UnknownHostException e) { e.printStackTrace(); } } }



获得了百度的IP地址

2,TCP和UDP



TCP 就相当于打电话

UDP 就相当于步话机

3,Socket



Socket 就相当于码头

4,UDP发送和接受数据



发送端:

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 {

/**
* 定义一个udp 发送端
*需求:通过udp传输方法将一段文字数据发送出去。
*1,建立udpsocket服务。
*2,提供数据,并将数据封装到数据包中。
*3,通过socket的发送功能,将数据包发出去
*4,关闭资源。
*/
public static void main(String[] args) throws  IOException {

//1,创立UDP服务,通过 DatagramSocket 对象
//DatagramSocket ds=new DatagramSocket();
//或自定义发送端口
DatagramSocket ds=new DatagramSocket(8888);
//2,确定数据,并封装成数据包、
byte[] buf="udp come on".getBytes();
DatagramPacket dp=new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.1.101"),10000);
//3,通过socket服务,将已有的数据包发送出去,通过send方法
ds.send(dp);
//关闭资源

ds.close();
}

}


接收端:

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

/*
* 定义udp的接收端
需求:定义一个应用程序,用于接收udp传输的数据并处理。

1,定义udpsocket服务。通常会监听一个端口,其实就是给这个接受网络应用程序定义数字标识,
便利与明确那些数据过来应用程序可以处理。
2,定义一个数据包,因为要存储接收到的字节数据。因为数据包对象中有更多功能可以提取
字节数据中的不同数据信息。
3,通过socket服务的receive方法将收到的数据存入已定义好的数据包中
4,通过数据包对象的特有功能,将这些不同的数据掏出,打印在控制台上
5,关闭资源
*/
public class UdpRece {

public static void main(String[] args) throws IOException {
//1,创立udp socket,建立端点
DatagramSocket ds=new DatagramSocket(10000);
while(true)
{
//2,定义一个数据包,用于存储数组
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, buf.length);
//3,通过服务的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);
}
//5,关闭资源
//ds.close();

}

}


因为是udp,所以要先启动接收端,再启动发送端,否则会丢包

我运行三次发送端,结果:



每日一道理

一个安静的夜晚,我独自一人,有些空虚,有些凄凉。坐在星空下,抬头仰望美丽天空,感觉真实却由虚幻,闪闪烁烁,似乎看来还有些跳动。美的一切总在瞬间,如同“海市蜃楼”般,也只是刹那间的一闪而过,当天空变得明亮,而这星星也早已一同退去……

改造成键盘录入::

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UdpSend2 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

DatagramSocket ds = new DatagramSocket();
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf=line.getBytes();
DatagramPacket dp=new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.101"), 9999);
ds.send(dp);
}
ds.close();
}

}


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

public class UdpRece2 {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
DatagramSocket ds=new DatagramSocket(9999);
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);
}

}

}


发送端:



接收端:



如果上面的地址 改为:192.168.1.255 这是广播地址,192.168.1.- 这个段里的都能收到。

练习:编写一个聊天程序。

有收数据的部分,和发数据的部分。

这两部分需要同时执行那就需要用到多线程技术,一个线程控制收,一个线程控制发。

因为收和发动作是不一致的,所以要定义2个run方法,而且这2个方法要封装到不同类中

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Send  implements Runnable{

private DatagramSocket  ds;
public  Send(DatagramSocket ds)
{
this.ds=ds;
}
@Override
public void run() {

BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
try {
while((line=bufr.readLine())!=null)
{
if("886".equals(line))
break;
byte[] buf=line.getBytes();
//局域网中的每台计算机都能收到--255
DatagramPacket dp=new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.1.255"), 10008);
ds.send(dp);
}
}
catch (IOException e) {
e.printStackTrace();
}
//ds.close();
}

}


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

public class Rece implements Runnable {

private DatagramSocket ds;

public Rece(DatagramSocket ds) {
this.ds = ds;
}

@Override
public void run() {
try {
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);
}
}

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}
}


import java.net.DatagramSocket;
import java.net.SocketException;

public class DosQQ {

public static void main(String[] args) throws SocketException {

DatagramSocket sendSocket =new DatagramSocket();
DatagramSocket receSocket =new DatagramSocket(10008);

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

}




5,TCP传输



文章结束给大家分享下程序员的一些笑话语录:

小沈阳版程序员~~~ \n程序员其实可痛苦的了......需求一做一改,一个月就过去了;嚎~ \n需求再一改一调,一季度就过去了;嚎~ \n程序员最痛苦的事儿是啥,知道不?就是,程序没做完,需求又改了; \n程序员最最痛苦的事儿是啥,知道不? 就是,系统好不容易做完了,方案全改了; \n程序员最最最痛苦的事儿是啥,知道不? 就是,系统做完了,狗日的客户跑了; \n程序员最最最最最痛苦的事儿是啥,知道不? 就是,狗日的客户又回来了,程序给删没了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: