1. 程式人生 > >第十五章作業

第十五章作業

第五題

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Q_5 {

    public static void main(String[] args) throws IOException {
        URL url=  new URL("http://www.jianshu.com/p/17e771cb34aa"
); URLConnection con = url.openConnection(); BufferedReader is= new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); FileOutputStream fos = new FileOutputStream("./test.txt"); String line; while((line = is.readLine()) != null ) { line = line + "\n"
; fos.write(line.getBytes("UTF-8")); fos.flush(); } is.close(); fos.close(); } }

基於TCP的C/S通訊程式

伺服器端MultiTalkServer.java

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

class ServerThread extends Thread{
    Socket socket = null
; int clienNum; public ServerThread(Socket s, int num){ socket = s; clienNum = num + 1; } public void run(){ try { String readLine; BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")); //PrintWriter os = new PrintWriter(socket.getOutputStream()); System.out.println("connected: " + socket.getInetAddress()); System.out.println("Client " + clienNum + ": " + is.readLine()); readLine = sin.readLine(); while(!readLine.equals("bye")){ os.write(readLine + "\n"); os.flush(); System.out.println("Client " + clienNum + ": " + is.readLine()); readLine = sin.readLine(); } os.close(); is.close(); socket.close(); } catch (IOException e) { System.out.println("Error: " + e); } } } public class MultiTalkServer { private static int clientNum = 0; public static void main(String[] args) throws IOException { ServerSocket server = null; boolean listening = true; try { server = new ServerSocket(4700); } catch (IOException e) { System.out.println("Could not listen on port: 4700"); System.exit(-1); } while(listening){ new ServerThread(server.accept(), clientNum).start(); ++clientNum; } server.close(); } }

客戶端TalkClient

import java.io.*;
import java.net.Socket;

public class TalkClient {

    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("192.168.1.109", 4700);
        } catch (IOException e) {
            System.out.println("can't not listen to " + e);
        }
        try {
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
            //PrintWriter os = new PrintWriter(socket.getOutputStream());
            String readLine;
            while(!(readLine = sin.readLine()).equals("bye")) {
                os.write(readLine + "\n");
                os.flush();
                System.out.println("Server " + socket.getInetAddress() + ": " + is.readLine());
            }
            os.close();
            is.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

基於UDP的C/S通訊程式

伺服器端QuoteServer.java
import java.io.IOException;

public class QuoteServer {

    public static void main(String[] args) throws IOException {
        new QuoteServerThread().start(); 
    }
}
QuoteServerThread.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class QuoteServerThread extends Thread{
    protected DatagramSocket socket = null;
    protected BufferedReader sin = null;
    protected boolean moreQuotes = true;
    public QuoteServerThread() throws IOException {
        this("QuoteServerThread");
    }
    public QuoteServerThread(String name) throws IOException {
        super(name);
        socket = new DatagramSocket(4700);
        sin = new BufferedReader(new InputStreamReader(System.in));
    }

    public void run() {
        while (moreQuotes) {
            try {
                byte[] buf = new byte[256];
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                buf = packet.getData();
                int size = 0;
                while(buf[size] != '\0') ++size;
                byte[] newBuf = new byte[size];
                for(int i = 0; i < size; ++i)
                    newBuf[i] = buf[i];
                System.out.println("Client: " + new String(newBuf, "UTF-8"));
                String dString = sin.readLine();
                if (dString.equals("bye"))
                    moreQuotes = false;
                buf = dString.getBytes("UTF-8");
                InetAddress address = packet.getAddress();
                int port = packet.getPort();
                packet = new DatagramPacket(buf, buf.length, address, port);
                socket.send(packet);
            } catch (IOException e) {
                e.printStackTrace();
                moreQuotes = false;
            }
        }
        socket.close();

    }
}
QuoteClient.java

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

public class QuoteClient {

    public static void main(String[] args) throws IOException {
        DatagramSocket socket= new DatagramSocket();
        BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
        InetAddress address = InetAddress.getByName("127.0.0.1");
        String readLine;
        while(!(readLine = sin.readLine()).equals("bye")) {
            byte[] buf = readLine.getBytes("UTF-8");
            DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4700);
            socket.send(packet);
            buf = new byte[256];
            packet = new DatagramPacket(buf, buf.length);
            socket.receive(packet);
            buf = packet.getData();
            int size = 0;
            while(buf[size] != '\0') ++size;
            byte[] newBuf = new byte[size];
            for(int i = 0; i < size; ++i)
                newBuf[i] = buf[i];
            System.out.println("Server: " + new String(newBuf, "UTF-8"));
        }
        socket.close();
    }
}