网络编程的基本模型就是客户机到服务器模型.简单的说就是两个进程之间相互通讯,然后其中一个必须提供一个固定的位置,而另一个则只需要知道这个固定的位置.并去建立两者之间的联系..然后完成数据的通讯就可以了.这里提供固定位置的通常称为服务器,而建立联系的通常叫做客户端.基于这个简单的模型,就可以进入网络编程啦.
Java对这个模型的支持有很多种Api.而这里我只想介绍有关Socket的编程接口.对于Java而言已经简化了Socket的编程接口.首先我们来讨论有关提供固定位置的服务方是如何建立的.Java提供了ServerSocket来对其进行支持.事实上当你创建该类的一个实力对象并提供一个端口资源你就建立了一个固定位置可以让其他计算机来访问你.
ServerSocket server=new ServerSocket(6789);
这里稍微要注意的是端口的分配必须是唯一的.因为端口是为了唯一标识每台计算机唯一服务的.另外端口号是从0~65535之间的,前1024个端口已经被Tcp/Ip 作为保留端口,因此你所分配的端口只能是1024个之后的.好了.我们有了固定位置.现在所需要的就是一根连接线了.该连接线由客户方首先提出要求.因此Java同样提供了一个Socket对象来对其进行支持.只要客户方创建一个Socket的实例对象进行支持就可以了.
Socket client=new Socket(InetAddress.getLocalHost(),5678);
客户机必须知道有关服务器的IP地址.对于这一点Java也提供了一个相关的类InetAddress 该对象的实例必须通过它的静态方法来提供.它的静态方法主要提供了得到本机IP 和通过名字或IP直接得到InetAddress的方法.
上面的方法基本可以建立一条连线让两台计算机相互交流了.
下面是socket传文件的一个简单例子。
发送端代码:
package fileTransmission;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class FileSender {
private ServerSocket ss=null;
public FileSender(){
}
public void sendFile(String filePath,int port){
DataOutputStream dos=null;
DataInputStream dis=null;
Socket socket=null;
try {
File file=new File(filePath);
ss=new ServerSocket(port);
socket=ss.accept();
dos=new DataOutputStream(socket.getOutputStream());
dis=new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
int buffferSize=1024;
byte[]bufArray=new byte[buffferSize];
dos.writeUTF(file.getName());
dos.flush();
dos.writeLong((long) file.length());
dos.flush();
while (true) {
int read = 0;
if (dis!= null) {
read = dis.read(bufArray);
}
if (read == -1) {
break;
}
dos.write(bufArray, 0, read);
}
dos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭所有连接
try {
if (dos != null)
dos.close();
} catch (IOException e) {
}
try {
if (dis != null)
dis.close();
} catch (IOException e) {
}
try {
if (socket != null)
socket.close();
} catch (IOException e) {
}
try {
if (ss != null)
ss.close();
} catch (IOException e) {
}
}
}
public static void main(String []args){
new FileSender().sendFile("E:/VITA文档记录.doc", 8821);
}
}
接收端代码:
package fileTransmission;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileIncepter {
public FileIncepter(){
}
public void receiveFile(String savePath,String ip,int port){
Socket socket=null;
try {
socket = new Socket(ip,port);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
DataInputStream dis=null;
try {
dis = new DataInputStream(new BufferedInputStream(socket
.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
}
int bufferSize = 1024;
// 缓冲区
byte[] buf = new byte[bufferSize];
int passedlen = 0;
long len = 0;
// 获取文件名称
try{
savePath += dis.readUTF();
DataOutputStream fileOut = new DataOutputStream(
new BufferedOutputStream(new BufferedOutputStream(
new FileOutputStream(savePath))));
len = dis.readLong();
System.out.println("文件的长度为:" + len + " KB");
System.out.println("开始接收文件!");
while (true) {
int read = 0;
if (dis!= null) {
read = dis.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
System.out.println("文件接收了" + (passedlen * 100 / len) + "%");
fileOut.write(buf, 0, read);
}
System.out.println("接收完成,文件存为" + savePath);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
public static void main(String[] args) {
new FileIncepter().receiveFile("F:\\", "localhost", 8821);
}
}
参考了网络博客代码,在此感谢原作者。
http://daoyongyu.iteye.com/blog/265677
http://www.blogjava.net/sterning/archive/2007/10/13/152508.html