📄 tcpclient.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -