tcpclient.java

来自「这是一个用java编写的模拟FTP的程序」· Java 代码 · 共 42 行

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

public class TcpClient 
{
	public static void main(String args[])
	{
		String host="127.0.0.1";
		int port=2000;
		
		try
		{
			Socket client = new Socket (host, port);
			System.out.println ("Connection established" + 
						"\nInput file Name : ");
			
			BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
			String fileName = bin.readLine();

			PrintStream pout = new PrintStream (client.getOutputStream());
			pout.println(fileName);
			pout.flush();
			
			InputStream in = client.getInputStream();
			FileOutputStream fout = new FileOutputStream("a.dat");
			
			int i = 0;
			while ((i=in.read())!=-1)
			{
				fout.write(i);
			}

			System.out.println ("OK!");
			client.close();
			
		}
		catch (IOException ioe)
		{
			System.err.println ("Error " + ioe);
		}
	}
}

⌨️ 快捷键说明

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