1、基于http的网络编程
URL、URLConnection、HttpURLConnection、JarURLConnection
URL 统一资源定位器
创建URL连接用法:
首先要声明一个URL对象,定位资源地址
1、通过在 URL 上调用 openConnection 方法创建连接对象。
2、处理设置参数和一般请求属性。
3、使用 connect 方法建立到远程对象的实际连接。
4、远程对象变为可用。远程对象的头字段和内容变为可访问。
<!--EndFragment-->
public static void test01(){
URL url = null;
try {
url = new URL("http://192.168.0.12:8080/mp3/resources.xml");
URLConnection urlc = url.openConnection();
urlc.connect();
StringBuffer sb = new StringBuffer();
BufferedReader br =
new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String str = "";
while((str = br.readLine()) != null){
sb.append(str);
}
System.out.print(sb.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
2、基于TCP/IP的网络编程
客户端Socket的创建和使用方法,Socket选项
服务器端ServerSocket
Socket在网络编程中的作用是实现客户端和服务器端的通信,他们相互发送的信息都保留在socket的对象当中,服务器端可以通过ServerSocket对象的accept()方法监听客户端,并且可以获得监听到的客户端的socket的对象。由此可以获得客户端发送给服务器的信息。此时客户端正在等待服务器的回应,服务器端的可以吧信息写入到Socket对象当中,供客户端的读取。
理解客户端和服务器端中的Socket.getInputStream() 和Socket.getOutputStream()
以下源代码:
1、Client_Socket.java
package socket_01;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client_Socket {
public static void main(String[] args){
try{
//启动客户端
Socket socket = new Socket("localhost", 12345);
socket.connect(null);
//客户端的读入内容
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//把读入的内容发送给服务器,同时接受服务器回传的数据getInputStream()---- getOutputStream())
BufferedReader bufferReader =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
String readLine="";
while((readLine = br.readLine()) != null ){
System.out.println("client input one time .");
os.println(readLine);
os.flush();
String responseline = bufferReader.readLine();
System.out.println("收到回复:" + responseline);
}
bufferReader.close();
os.close();
socket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
2、Server_Socket.java
package socket_01;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server_Socket {
public static void main(String[] args){
ServerSocket server = null;
try {
server = new ServerSocket(12345);
System.out.println("启动服务:" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort());
while(true){
Socket socket = server.accept();
System.out.println("客户端连接:" + socket.getInetAddress().getHostAddress() + ":" + socket.getPort());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
String line;
while((line = is.readLine()) != null){
System.out.println("收到:" + line);
if(line.equals("bye")){
break;
}
else{
os.println(sin.readLine());
os.flush();
}
}
is.close();
os.close();
socket.close();
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try{
server.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
<!--EndFragment-->
2、基于UDP的网络编程
与之有关的类的有:DatagramPacket DatagramSocket MulticastSocket
DatagramPacket需要发送的数据包类,此类对数据进行封装。
DatagramSocket 数据包的套接字,该类对象以数据包为参数,发送和接受数据包。Send方法和receive方法
MulticastSocket 组合 DatagramPacket使用。与DatagramSocket的使用类似。
package udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UDPServer {
public static void main(String[] args){
try {
DatagramSocket socket = new DatagramSocket(12345);
byte buf[] = new byte[1000];
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length);
System.out.println("开始接受包信息");
while(true){
socket.receive(receivePacket);
String name = receivePacket.getAddress().toString();
System.out.println("来自主机: " + name + "\n端口" + receivePacket.getPort());
String s = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("接受数据 " + s);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
package udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClient {
public static void main(String[] args){
try {
DatagramSocket socket = new DatagramSocket();
InetAddress host = InetAddress.getByName("localhost");
String msg = "hello, i am client !";
DatagramPacket sendPacket = new DatagramPacket(msg.getBytes(),msg.length(),host,12345);
socket.send(sendPacket);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
服务器端监听是否有数据包到来。相应的方法是socket.receive(receivePacket);当没有数据包到达时,该方法一直处于阻塞状态。
客户端通过socket.send(sendPacket);发送数据包。