用TCP通信模型创建一个web服务器
?
?
主要用ServerSocket 一直监听,这里将其放到while循环中,在循环体内单开一个线程
?
?
public class MyWebServer {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(80);
Socket socket=null;
while((socket=ss.accept())!=null){
new HTTPThread(socket).start();
}
ss.close();
}
}
?
?
在线程中的run方法中写要做的东西,另外要在构造函数中将Socket对象传进来
?
?
public class HTTPThread extends Thread {
private Socket socket;
HTTPThread(Socket socket){
this.socket=socket;
}
public void run() {
OutputStream outputStream=socket.getOutputStream();
PrintWriter pWriter=new PrintWriter(outputStream);
pWriter.println("<html>");
pWriter.println("<body>");
pWriter.println("hello! this is my page");
pWriter.println("</body>");
pWriter.println("</html>");
pWriter.flush();
pWriter.close();
socket.close();
}
}
?
?
用UDP通信模型创建一个即时聊天软件
?
?
主要是要写一个接收线程和一个发送线程,然后就在main函数中启动这两个线程就ok了
?
?
class SendThread extends Thread {
private DatagramSocket datagramSocket;
private int sendPort;
SendThread(int port, int sendPort) {
super();
this.sendPort = sendPort;
this.datagramSocket = new DatagramSocket(port);
}
public void run() {
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
String string = null;
while ((string = bReader.readLine()) != null) {
DatagramPacket dp = new DatagramPacket(string.getBytes(),0,string.length(),InetAddress.getByName("localhost"),sendPort);
datagramSocket.send(dp);
System.out.println("send message:" + string);
}
}
}
class ReceiveThread extends Thread {
private DatagramSocket datagramSocket;
ReceiveThread(int port) {
super();
this.datagramSocket = new DatagramSocket(port);
}
public void run() {
byte[] buff = new byte[1024];
DatagramPacket dp = new DatagramPacket(buff, 1024);
while (true) {
datagramSocket.receive(dp);
String string = new String(dp.getData(), 0, dp.getLength());
System.out.println("receive message:" + string);
}
}
}
public static void main(String[] args) throws SocketException {
new ReceiveThread(Integer.valueOf(args[0])).start();
new SendThread(Integer.valueOf(args[1]), Integer.valueOf(args[2])).start();
}
?
?
用Java访问web站点
?
?
首先通过一个路径的字符串作为参数创建一个URL对象,调用URL对象的openConnection方法得到一个HttpURLConnection对象的引用
?
?
然后调用HttpURLConnection的connect方法建立连接,待建立好连接之后,就可以调用HttpURLConnection的getHeaderFields方法得到一个Map对象,这个Map对象中存放的就是Header中的键值对
?
?
服务器除了返回Header之外,还会返回响应内容,用一个BufferedReader读这些响应内容,当然会用到InputStreamReader作为参数,而InputStreamReader的输入来源即是调用HttpURLConnection的getInputStream方法
?
?
public static void main(String[] args) throws IOException {
String path = "http://www.baidu.com";
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
Map<String, List<String>> headerMap = httpURLConnection.getHeaderFields();
for (String key : headerMap.keySet()) {
System.out.println(key + ":" + headerMap.get(key));
}
BufferedReader br = new BufferedReader(new InputStreamReader(
httpURLConnection.getInputStream(), "utf-8"));
String string=null;
while((string=br.readLine())!=null){
System.out.println(string);
}
httpURLConnection.disconnect();
}