I/O及网络--一个简单的文件传送代码
import java.io.*;
import java.net.*;
public class Server
static final int size = 10240;
static String DiskInfoXML = usrDir + "disk_info.xml";
public static void main(String argv)
DatagramSocket sd = null;
String diskRequest;
try
sd = new DatagramSocket(7777); //建立socket连接
while (true) //一直监听
byte buffer = new byte[size];
sd.receive(packet);
int port = packet.getPort();
InetAddress address = packet.getAddress();
System.out.println( "diskRequest = "n" + diskRequest);
fout.write(buffer,0,len); //写进文件
System.out.println(len+ " bytes received");
}
{
}
{
}
}
/************************* Client *******************************
import java.io.*;
public class Client
public static void main(String argv)
try
int bufferSize = 1024;
int remotePort=7777;
String diskInfoXML = usrDir + "disk_info.xml";
FileInputStream fr = new FileInputStream(f);
Long l=Long.valueOf(f.length());
byte buffer = new byte[len];
for(int i=0;i System.out.print((char)buffer);
Socket sd = new Socket(remoteHost, remotePort,false);
OutputStream out = sd.getOutputStream();
out.flush();
catch (IOException e)
System.out.println(e.toString());
System.out.println( "Send OK ...");
编辑推荐:
温馨提示:因考试政策、内容不断变化与调整,长理培训网站提供的以上信息仅供参考,如有异议,请考生以权威部门公布的内容为准! (责任编辑:长理培训)
点击加载更多评论>>