close
好站連結:程式設計工藝大師
//Ftp Client
import java.io.*;
import java.net.*;
import java.net.*;
public class Client13_4 {
int i;
static String iaddr;
static int port;
static String infilename;
public Client13_4() {
try{
Socket socket=new Socket(InetAddress.getByName(iaddr),port);
DataOutputStream outstream = new DataOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream(infilename);
while((i=fis.read()) !=-1)
outstream.writeInt(i);
outstream.writeInt(i);
int i;
static String iaddr;
static int port;
static String infilename;
public Client13_4() {
try{
Socket socket=new Socket(InetAddress.getByName(iaddr),port);
DataOutputStream outstream = new DataOutputStream(socket.getOutputStream());
FileInputStream fis = new FileInputStream(infilename);
while((i=fis.read()) !=-1)
outstream.writeInt(i);
outstream.writeInt(i);
System.out.println("Data sent to internet successfully!");
socket.close();
}
socket.close();
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
System.out.println(e.getMessage());
}
}
public static void main(String args[]) {
if (args.length < 3){
System.out.println("USAGE: java Client13_4 [iaddr] [port] [infilename]");
System.exit(1);
}
if (args.length < 3){
System.out.println("USAGE: java Client13_4 [iaddr] [port] [infilename]");
System.exit(1);
}
iaddr = args[0];
port=Integer.parseInt(args[1]);
infilename = args[2];
Client13_4 ClientStart=new Client13_4();
}
}
port=Integer.parseInt(args[1]);
infilename = args[2];
Client13_4 ClientStart=new Client13_4();
}
}
==============================================================
//FTP server
import java.net.*;
import java.io.*;
import java.io.*;
public class Server13_4 {
int messagein;
static int port;
static String outfilename;
int messagein;
static int port;
static String outfilename;
public Server13_4() {
try{
ServerSocket SS = new ServerSocket(port);
System.out.println("Server is created and waiting Client to connect...");
try{
ServerSocket SS = new ServerSocket(port);
System.out.println("Server is created and waiting Client to connect...");
Socket socket = SS.accept();
System.out.println("Client IP = " +
socket.getInetAddress().getHostAddress());
System.out.println("Client IP = " +
socket.getInetAddress().getHostAddress());
DataInputStream instream = new DataInputStream(socket.getInputStream());
FileOutputStream fos = new FileOutputStream(outfilename);
FileOutputStream fos = new FileOutputStream(outfilename);
while(messagein != -1){
messagein = instream.readInt();
fos.write(messagein);
}
System.out.println("Data written to File successfully!");
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
messagein = instream.readInt();
fos.write(messagein);
}
System.out.println("Data written to File successfully!");
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String args[]){
if(args.length < 2){
System.out.println("Usage: java Server13_4 [port] [outfilename]");
System.exit(1);
}
port=Integer.parseInt(args[0]);
outfilename = args[1];
Server13_4 ServerStart=new Server13_4();
}
}
if(args.length < 2){
System.out.println("Usage: java Server13_4 [port] [outfilename]");
System.exit(1);
}
port=Integer.parseInt(args[0]);
outfilename = args[1];
Server13_4 ServerStart=new Server13_4();
}
}
連線前的txt檔,裡面沒有資料。
連線後的txt檔,裡面出現傳輸檔案的資料了!!
全站熱搜
留言列表