基于UDP的网络通信
package com.day1_2015_8_3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class MySendTest {
public static void main(String[] args) {
try {
DatagramSocket send = new DatagramSocket();
DatagramSocket receive = new DatagramSocket(8088);
Scanner scanner = new Scanner(System.in);
byte[] ReceiveArray = new byte[1024];
while (true) {
Calendar rightNow =Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time = format.format(rightNow.getTime());
System.out.print(time+"\n小明说:");
String s = scanner.next();
byte[] array = s.getBytes();
DatagramPacket dp = new DatagramPacket(array, array.length, InetAddress.getLocalHost(), 8080);
send.send(dp);
DatagramPacket packetRecive = new DatagramPacket(ReceiveArray, 1024, InetAddress.getLocalHost(), 8080);
receive.receive(packetRecive);
byte[] data = packetRecive.getData();
System.out.println(time +"\n小红说:" + new String(data, 0, packetRecive.getLength()));
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.day1_2015_8_3;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;
public class MyReceiveTest {
public static void main(String[] args) {
try {
DatagramSocket receive = new DatagramSocket(8080);
DatagramSocket send = new DatagramSocket();
Scanner scanner = new Scanner(System.in);
byte[] array = new byte[1024];
while (true) {
Calendar rightNow =Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String time = format.format(rightNow.getTime());
DatagramPacket dp = new DatagramPacket(array, 1024,InetAddress.getLocalHost(), 8088);
receive.receive(dp);
byte[] data = dp.getData();
System.out.println(time+"\n小明说:"+ new String(data, 0, dp.getLength()));
System.out.print(time+"\n小红说:");
String s = scanner.next();
byte[] sendByte = s.getBytes();
DatagramPacket ps = new DatagramPacket
(sendByte, sendByte.length, InetAddress.getLocalHost(), 8088);
send.send(ps);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
URL(openStream()网址)
package com.day1_2015_8_3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {
public static void main(String[] args) {
try {
URL url = new URL("http://www.baidu.com");
InputStream is = url.openStream();
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
while(line!=null){
System.out.println(line);
line = br.readLine();
}
br.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.day1_2015_8_3;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL2 {
public static void main(String[] args) {
try {
URL url = new URL("http://pic17.nipic.com/20111026/2487606_163354654187_2.jpg");
File file = new File("d:\\11.jpg");
if(!file.exists()){
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
InputStream is = url.openStream();
byte[] array = new byte[1024];
int i = is.read(array);
while(i!=-1){
os.write(array,0,i);
i=is.read(array);
}
os.flush();
os.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}