tcpclient.java

来自「五子棋程序.它的功能很好」· Java 代码 · 共 82 行

JAVA
82
字号
import java.net.*;
import java.io.*;

public class TCPclient
{
        public static void main(String[] args) {
                try
                {
                        if(args.length!=2)
                        {
                          System.err.println("User: java TPclient <host> <port>");
                          System.exit(0);
                        }
                }
                catch(Exception e)
                {
                        System.err.println(e.getMessage());
                }

                String host = args[0];
                int port = Integer.parseInt(args[1]);
                TCPclient clientsa = new TCPclient();
                Socket so = null;
                try 
                {
                  so = new Socket(host, port);
                }
                catch (IOException ex) {
                }
                System.out.println("Connecting to server......");

                String fromname = null; DataInputStream in = null;
                try
                {
                      System.out.println("Please input the source filename:");
                      BufferedReader rrs = new BufferedReader(new
                          InputStreamReader(System.in));
                      fromname = rrs.readLine();  //从键盘读文件名
                      DataOutputStream out = new DataOutputStream(new
                          BufferedOutputStream(so.getOutputStream()));
                      in = new DataInputStream(new BufferedInputStream(so.
                          getInputStream()));
                      out.writeBytes(fromname + "\n");  //将文件名发送到服务器
                      out.flush();
                }
                catch (IOException ex1) {
                }
                    FileOutputStream to = null;

                    try {
                           byte y = in.readByte();
                           if (y > 0) 
                           {
                               File to_file = new File("t_"+fromname);
                               to = new FileOutputStream(to_file);
                               DataOutputStream fout = new DataOutputStream(to);
                                   while (true) 
                                    {
                                           byte[] bq = new byte[500];
                                           int len = in.read(bq);
                                           if (len <= 0)
                                              break;
                                           fout.write(bq,0,len);
                                    }
                           }
                           else {
                                    System.out.println("source file not found");
                           }
                      }
                     catch(IOException e){ ; }
                     finally {
                              if (to != null)
                              try {
                                   to.close();
                                   so.close();
                                   }
                              catch (IOException e) { ; }
                      }
        }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?