0
点赞
收藏
分享

微信扫一扫

《Java编程思想第四版》学习笔记58--改编书上例程JabberServer和JabberClient实现基于TCP协议的文件传输

盖码范 03-14 17:30 阅读 2

1、服务器端代码:

//: JabberServer.java
// Very simple server that just
// echoes whatever the client sends.
import java.io.*;
import java.net.*;
public class JabberServer {
    // Choose a port outside of the range 1-1024:
    public static final int PORT = 8080;
    public static void main(String[] args)
            throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Started: " + s);
        try {
// Blocks until a connection occurs:
            Socket socket = s.accept();
            InputStream input = null;
            try {
                System.out.println(
                        "Connection accepted: "+ socket);
                input = new FileInputStream(new File("G:/iHasher-v0.2.exe"));
                byte[] buf = new byte[1024];
                int bytesRead;
                InputStream in = socket.getInputStream();
                byte[] buf1 = new byte[6];
                int bytesRead1;
                OutputStream out = socket.getOutputStream();

                String str = "";
              while( ( bytesRead1 = in.read(buf1)) != -1) {
                  str = new String(buf1);
                  System.out.println("服务器端收到的数据:"  + str);
                  break;
              }
              if (!str.equals("123456")) {
                  System.out.println("密码不对!");
                  return;
              }
                while ((bytesRead = input.read(buf)) > 0) {
                    out.write(buf,0,bytesRead);
                }
// Always close the two sockets...
            } finally {
                System.out.println("closing...");
                socket.close();
                input.close();
            }
        } finally {
            s.close();
        }
    }
} ///:~

客户端代码:

//: JabberClient.java
// Very simple client that just sends
// lines to the server and reads lines
// that the server sends.
import java.net.*;
import java.io.*;
public class JabberClient {
    public static void main(String[] args)
            throws IOException {
        InetAddress addr =
                InetAddress.getByName("192.168.0.199");
        System.out.println("addr = " + addr);
        Socket socket =
                new Socket(addr, 8080);
        OutputStream output = null;
        try {
            System.out.println("socket = " + socket);
            OutputStream out = socket.getOutputStream();
            InputStream in =  socket.getInputStream();
            output = new FileOutputStream(new File("c:/iHash.exe"));
            byte[] buf = new byte[1024];
            int byteRead;

            out.write("123456".getBytes());
            while ((byteRead = in.read(buf)) > 0) {
                output.write(buf,0,byteRead);
            }
        } finally {
            System.out.println("closing...");
            socket.close();
            output.close();
        }
    }
} ///:~

当客户端密码正确时,会把服务器端的相应文件下载到本地。

举报

相关推荐

0 条评论