服务器端:
import java.net.*
import java.io.*
public class DateServer {
public static void main(String[] args) {
ServerSocket server=null
try{
server=new ServerSocket(6666)
System.out.println(
"Server start on port 6666...")
while(true){
Socket socket=server.accept()
new SocketHandler(socket).start()
/*
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
)
out.println(new java.util.Date().toLocaleString())
out.close()
*/
}
}catch(Exception e){
e.printStackTrace()
}finally{
if(server!=null) {
try{
server.close()
}catch(Exception ex){}
}
}
}
}
class SocketHandler extends Thread {
private Socket socket
public SocketHandler(Socket socket) {
this.socket=socket
}
public void run() {
try{
PrintWriter out=new PrintWriter(
new OutputStreamWriter(
socket.getOutputStream()
)
)
out.println(
new java.util.Date().
toLocaleString())
out.close()
}catch(Exception e){
e.printStackTrace()
}
}
}
客户端:
package com.briup
import java.io.*
import java.net.*
public class FtpClient {
public static void main(String[] args) {
if(args.length==0) {
System.out.println("Usage:java FtpClient file_path")
System.exit(0)
}
File file=new File(args[0])
if(!file.exists()||!file.canRead()) {
System.out.println(args[0]+" doesn't exist or can not read.")
System.exit(0)
}
Socket socket=null
try{
socket=new Socket(args[1],Integer.parseInt(args[2]))
BufferedInputStream in=new BufferedInputStream(
new FileInputStream(file)
)
BufferedOutputStream out=new BufferedOutputStream(
socket.getOutputStream()
)
byte[] buffer=new byte[1024*8]
int i=-1
while((i=in.read(buffer))!=-1) {
out.write(buffer,0,i)
}
System.out.println(socket.getInetAddress().getHostAddress()+" send file over.")
in.close()
out.close()
}catch(Exception e){
e.printStackTrace()
}finally{
if(socket!=null) {
try{
socket.close()
}catch(Exception ex){}
}
}
}
}
FTP文件上传 (“adress.txt”, FTP取现行目录 () + “/adress.txt”, )注意1,本地路径,如果是相对路径的话,就要将文件和易语言程序放在一个目录下,如果是绝对路径的话,注意要写全文件格式比如"D:\ABC\1.TXT"
注意2,FTP文件路径需要跟上文件名,不能写"/123/wwwroot/",要讲完整的路径+文件名写出来,推荐写法FTP取现行目录 () + “/1.txt"
ftp server ? 可以到sourceforge或者github开源软件仓库上去查找下载。流行的开源软件也可以,比如ftp filezilla server就是比较流行的。不过不一定是c代码了,也许是c++的。欢迎分享,转载请注明来源:夏雨云
评论列表(0条)