import java.io.BufferedReader;//io 包提供对流进行读写的工具也是tcp套接字通信唯一途径
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;//包提供套接字工具。
import java.net.UnknownHostException;
public class RemoteFileClient {
protected String hostIp;
protected int hostPort;
protected BufferedReader socketReader;//负责读数据的对象
protected PrintWriter socketWriter;//负责写数据的对象
public RemoteFileClient(String aHostIp,int aHostPort)
{
hostIp=aHostIp;
hostPort=aHostPort;
}
/**
* 向/远程服务器请求文件信息
* @param fileNameToGet
* @return
*/
public String getFile(String fileNameToGet)
{
StringBuffer fileLines=new StringBuffer();
try
{
socketWriter.println(fileNameToGet);
socketWriter.flush();
String line=null;
while((line=socketReader.readLine())!=null)
{
fileLines.append(line+"");
}
}
catch(IOException e)
{
System.out.println ("Error reading from file &"+fileNameToGet);
}
return fileLines.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
RemoteFileClient remoteFileClient = new RemoteFileClient("130.30.3.68", 3000);
[color=blue]remoteFileClient.setUpConnection();
String fileContents = remoteFileClient.getFile("c:\\RemoteFile.txt");[/color] remoteFileClient.tearDownConnection();
System.out.println(fileContents);
}
/**
*
*连/接到远程服务器
*/
public void setUpConnection()
{
try
{
Socket client = new Socket(hostIp,hostPort);//创建socket对象
OutputStream outToServerStream=client.getOutputStream();
InputStream inFromServerStream=client.getInputStream();
//把/Socket的InputStream包装进BufferedReader 以使我们能够读取流的行
socketReader = new BufferedReader(new InputStreamReader(inFromServerStream));
socketWriter = new PrintWriter(outToServerStream);[color=blue]
//把/Socket的OutputStream包装进PrintWriter 以使我们能够发送文件请求到服务器[/color] }
catch(UnknownHostException e)
{
System.out.println("Error setting up socket connection :unknown host at "+hostIp+":"+hostPort);
}
catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
/**
* 从/远程服务器上断开
*
*/
public void tearDownConnection()
{
try
{
socketWriter.close();
socketReader.close();
}
catch(IOException e)
{
System.out.println("Error tearing down socket connection "+e);
}
}
}
//服务器端的
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class RemoteFileServer {
int listendPort;
public RemoteFileServer(int aListenPort)
{
listendPort=aListenPort;
}
public void acceptConnections() throws IOException
{
ServerSocket server=new ServerSocket(listendPort);
Socket incomingConnection=null;
while(true)
[color=red]{//&://不/断监听直到来了一个连接请求,然后交由handleConnection处理[/color]
try {
[color=darkred][color=red]incomingConnection = server.accept();[/color][/color]
handleConnection(incomingConnection);
}
catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public void handleConnection(Socket incomingConnection) throws IOException
{
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
[color=red]//只是获取client发送的一个文件名称,接收并且将文件的内容显示出来[/color]
FileReader fileReader = new FileReader(new File(streamReader.readLine()));
BufferedReader bufferedFileReader = new BufferedReader(fileReader);
PrintWriter streamWriter = new PrintWriter(outputToSocket);
String line = null;
while ((line = bufferedFileReader.readLine()) != null)
{
streamWriter.println(line);
}
fileReader.close();
streamWriter.close();
streamReader.close();
//切记,永远不要在关闭 Writer 之前关闭 Reader
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
// TODO 自动生成方法存根
RemoteFileServer server=new RemoteFileServer(3000);
server.acceptConnections();
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;//包提供套接字工具。
import java.net.UnknownHostException;
public class RemoteFileClient {
protected String hostIp;
protected int hostPort;
protected BufferedReader socketReader;//负责读数据的对象
protected PrintWriter socketWriter;//负责写数据的对象
public RemoteFileClient(String aHostIp,int aHostPort)
{
hostIp=aHostIp;
hostPort=aHostPort;
}
/**
* 向/远程服务器请求文件信息
* @param fileNameToGet
* @return
*/
public String getFile(String fileNameToGet)
{
StringBuffer fileLines=new StringBuffer();
try
{
socketWriter.println(fileNameToGet);
socketWriter.flush();
String line=null;
while((line=socketReader.readLine())!=null)
{
fileLines.append(line+"");
}
}
catch(IOException e)
{
System.out.println ("Error reading from file &"+fileNameToGet);
}
return fileLines.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
RemoteFileClient remoteFileClient = new RemoteFileClient("130.30.3.68", 3000);
[color=blue]remoteFileClient.setUpConnection();
String fileContents = remoteFileClient.getFile("c:\\RemoteFile.txt");[/color] remoteFileClient.tearDownConnection();
System.out.println(fileContents);
}
/**
*
*连/接到远程服务器
*/
public void setUpConnection()
{
try
{
Socket client = new Socket(hostIp,hostPort);//创建socket对象
OutputStream outToServerStream=client.getOutputStream();
InputStream inFromServerStream=client.getInputStream();
//把/Socket的InputStream包装进BufferedReader 以使我们能够读取流的行
socketReader = new BufferedReader(new InputStreamReader(inFromServerStream));
socketWriter = new PrintWriter(outToServerStream);[color=blue]
//把/Socket的OutputStream包装进PrintWriter 以使我们能够发送文件请求到服务器[/color] }
catch(UnknownHostException e)
{
System.out.println("Error setting up socket connection :unknown host at "+hostIp+":"+hostPort);
}
catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
/**
* 从/远程服务器上断开
*
*/
public void tearDownConnection()
{
try
{
socketWriter.close();
socketReader.close();
}
catch(IOException e)
{
System.out.println("Error tearing down socket connection "+e);
}
}
}
//服务器端的
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class RemoteFileServer {
int listendPort;
public RemoteFileServer(int aListenPort)
{
listendPort=aListenPort;
}
public void acceptConnections() throws IOException
{
ServerSocket server=new ServerSocket(listendPort);
Socket incomingConnection=null;
while(true)
[color=red]{//&://不/断监听直到来了一个连接请求,然后交由handleConnection处理[/color]
try {
[color=darkred][color=red]incomingConnection = server.accept();[/color][/color]
handleConnection(incomingConnection);
}
catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
public void handleConnection(Socket incomingConnection) throws IOException
{
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection.getInputStream();
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
[color=red]//只是获取client发送的一个文件名称,接收并且将文件的内容显示出来[/color]
FileReader fileReader = new FileReader(new File(streamReader.readLine()));
BufferedReader bufferedFileReader = new BufferedReader(fileReader);
PrintWriter streamWriter = new PrintWriter(outputToSocket);
String line = null;
while ((line = bufferedFileReader.readLine()) != null)
{
streamWriter.println(line);
}
fileReader.close();
streamWriter.close();
streamReader.close();
//切记,永远不要在关闭 Writer 之前关闭 Reader
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
// TODO 自动生成方法存根
RemoteFileServer server=new RemoteFileServer(3000);
server.acceptConnections();
}
}