[置顶] 网络通讯,socket

主要运用到了URL类,数据通讯主要分为TDP和UDP两种形式。这两种形式中,主要使用到了DatagramSocket、DatagramPacket这两个类,

DatagramPacket类中,没有ip地址的构造方法是用来创建接收数据包的。有ip地址的构造方法是用来创建发送数据包的

通过URL获取IP地址:

URL url=new URL("http://www.hncu.net");
InetAddress ip=InetAddress.getByName(url.getHost());
System.out.println(ip.getHostAddress());

通过URLConnection获取相关信息:

URL source = new URL("http://www.hncu.net");
URLConnection con=source.openConnection();
System.out.println(con.getContentType());
System.out.println(con.getURL());
System.out.println(con.getContentLength());
System.out.println(new Date(con.getLastModified()));

接收演示:

public class ReceiveDemo {


public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket(10000);

//把数据接收到dp中
byte buf[] = new byte[1024];
//DatagramPacket类中,没有ip地址的构造方法是用来创建接收数据包的
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);//接收之后,所有的数据都在dp里面

//从dp中解析出我们想要的信息
//获取ip
String ip = dp.getAddress().getHostAddress();
int port = dp.getPort();//端口
byte data[] = dp.getData();//对方发送的数据
String info = new String(data);
System.out.println(ip+":"+port+"----"+info);
} catch (Exception e) {
e.printStackTrace();
}
}


}

发送演示:

public class SendDemo {


public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket(9999);
String info="湖南城市学院,Socket通信信息!";
byte buf[] = info.getBytes();//用默认码表
//DatagramPacket类中,有ip地址的构造方法是用来创建发送数据包的
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.31.168"),10000 );//注意,IP和端口都是接收方的
ds.send(dp);
ds.close();
} catch (Exception e) {
e.printStackTrace();
}

}


}


其中,接收和发送的端口号在同一台机器上不能使用同一个,端口号的范围是0-63325,系统默认保留0-1024端口号作为预留,尽量不要使用!

public class UDPChat {
public static void main(String[] args) {
try {
DatagramSocket send = new DatagramSocket(10001);
DatagramSocket receive = new DatagramSocket(10002);

new Thread(new Send(send) ).start();
new Thread(new Receive(receive)).start();
} catch (Exception e) {
e.printStackTrace();
}

}
}


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


@Override
public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while( (line=br.readLine())!=null ){
byte buf[] = line.getBytes();
//要用接收方的ip和接收端口
DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.31.168"), 10004);
ds.send(dp);
if("over".equals(line)){
break;
}
}
ds.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
class Receive implements Runnable{
private DatagramSocket ds;
public Receive(DatagramSocket receive) {
this.ds = receive;
}


@Override
public void run() {
try {
byte buf[] = new byte[1024];//大小够存储一行就可以
while(true){
//接收数据
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
//解析数据
String ip = dp.getAddress().getHostAddress();
String info= new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"说:"+info);
if("over".equals(info)){
System.out.println(ip+"离开聊天室....");
//break;
}
}
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(socket,网络,UDP,通讯,java程序开发)