第十五章作业

第五题

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值