大家先看一段代码。。很简单的,可能有点长import java.io.*;
import java.net.*;
public class FileServer
{
private final int port=8000;
private ServerSocket serverSocket;
private File file;
FileServer()throws IOException
{
serverSocket=new ServerSocket(port);
System.out.println("-------------------服务器已经启动-------------------");
}
public void send(Socket s)throws IOException
{
InputStream is=s.getInputStream();
byte[] buff=new byte[1024];
is.read(buff);
String info=new String(buff);
file=new File(info);
InputStream fileIs=new FileInputStream(file);
OutputStream os=s.getOutputStream();
while(fileIs.read(buff)!=-1)
{
os.write(buff);
}
s.shutdownOutput();
s.shutdownInput();
}
public void exceute()throws IOException
{
Socket socket;
while(true)
{
socket=serverSocket.accept();
send(socket);
}
}
public static void main(String[] args)throws IOException
{
new FileServer().exceute();
}
}
///////////////////////////////////////////class FileClient
{
private final int port=8000;
private final String host="localhost";
private Socket socket;
private static String fileName;
FileClient()throws IOException
{
socket=new Socket(host,port);
System.out.println("连接服务器成功");
}
public void revice()throws IOException,InterruptedException
{
OutputStream out=socket.getOutputStream();
out.write(fileName.getBytes());
InputStream is=socket.getInputStream();
byte[] buff=new byte[1024];
while(is.read(buff)!=-1)
{
OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
fileIn.write(buff);
}
}
public static void main(String[] args)
{
try
{
if(args.length==1)
{
fileName=args[0];
new FileClient().revice();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}这个代码的问题很简单的。就是当一个FileClient通过命令参数,叫FileServer发一个文件给FileClient。。
当我输入命令数是GET的时候就会卡在那里不运行了,大家可以试一下。。我输其他任何字符串都不会这样,郁闷中还有就是当我输入的命令参数是1.txt时,意思就是要FileClient接受一个来自FileServer的一个1.txt文件并把它放在f盘里面。。
但是两个1.txt的内容完全不同大家看一下下面的图。。这是在FileServer下的1.txt的内容
这是在FileClient得到的1.txt的内容
import java.net.*;
public class FileServer
{
private final int port=8000;
private ServerSocket serverSocket;
private File file;
FileServer()throws IOException
{
serverSocket=new ServerSocket(port);
System.out.println("-------------------服务器已经启动-------------------");
}
public void send(Socket s)throws IOException
{
InputStream is=s.getInputStream();
byte[] buff=new byte[1024];
is.read(buff);
String info=new String(buff);
file=new File(info);
InputStream fileIs=new FileInputStream(file);
OutputStream os=s.getOutputStream();
while(fileIs.read(buff)!=-1)
{
os.write(buff);
}
s.shutdownOutput();
s.shutdownInput();
}
public void exceute()throws IOException
{
Socket socket;
while(true)
{
socket=serverSocket.accept();
send(socket);
}
}
public static void main(String[] args)throws IOException
{
new FileServer().exceute();
}
}
///////////////////////////////////////////class FileClient
{
private final int port=8000;
private final String host="localhost";
private Socket socket;
private static String fileName;
FileClient()throws IOException
{
socket=new Socket(host,port);
System.out.println("连接服务器成功");
}
public void revice()throws IOException,InterruptedException
{
OutputStream out=socket.getOutputStream();
out.write(fileName.getBytes());
InputStream is=socket.getInputStream();
byte[] buff=new byte[1024];
while(is.read(buff)!=-1)
{
OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
fileIn.write(buff);
}
}
public static void main(String[] args)
{
try
{
if(args.length==1)
{
fileName=args[0];
new FileClient().revice();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}这个代码的问题很简单的。就是当一个FileClient通过命令参数,叫FileServer发一个文件给FileClient。。
当我输入命令数是GET的时候就会卡在那里不运行了,大家可以试一下。。我输其他任何字符串都不会这样,郁闷中还有就是当我输入的命令参数是1.txt时,意思就是要FileClient接受一个来自FileServer的一个1.txt文件并把它放在f盘里面。。
但是两个1.txt的内容完全不同大家看一下下面的图。。这是在FileServer下的1.txt的内容
这是在FileClient得到的1.txt的内容
不过我运行了一下,没问题,能复制文本。也没造成不一致的情况!code=Java]
//FileServer.java
import java.io.*;
import java.net.*;
public class FileServer
{
private final int port=8000;
private ServerSocket serverSocket;
private File file;
FileServer()throws IOException
{
serverSocket=new ServerSocket(port);
System.out.println("-------------------服务器已经启动-------------------");
}
public void send(Socket s)throws IOException
{
InputStream is=s.getInputStream();
byte[] buff=new byte[1024];
is.read(buff);
String info=new String(buff);
file=new File(info);
InputStream fileIs=new FileInputStream(file);
OutputStream os=s.getOutputStream();
while(fileIs.read(buff)!=-1)
{
os.write(buff);
}
s.shutdownOutput();
s.shutdownInput();
}
public void exceute()throws IOException
{
Socket socket;
while(true)
{
socket=serverSocket.accept();
send(socket);
}
}
public static void main(String[] args)throws IOException
{
new FileServer().exceute();
}
}
//FileClient.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;public class FileClient
{
private final int port=8000;
private final String host="localhost";
private Socket socket;
private static String fileName;
FileClient()throws IOException
{
socket=new Socket(host,port);
System.out.println("连接服务器成功");
}
public void revice()throws IOException,InterruptedException
{
OutputStream out=socket.getOutputStream();
out.write(fileName.getBytes());
InputStream is=socket.getInputStream();
byte[] buff=new byte[1024];
while(is.read(buff)!=-1)
{
OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
fileIn.write(buff);
}
}
public static void main(String[] args)
{
try
{
if(args.length==1)
{
fileName=args[0];
new FileClient().revice();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}/*运行:服务器
C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileServer
-------------------服务器已经启动-------------------客户机:
C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileClient 1.txt
连接服务器成功
连接服务器成功*/[/code]
//FileServer.java
import java.io.*;
import java.net.*;
public class FileServer
{
private final int port=8000;
private ServerSocket serverSocket;
private File file;
FileServer()throws IOException
{
serverSocket=new ServerSocket(port);
System.out.println("-------------------服务器已经启动-------------------");
}
public void send(Socket s)throws IOException
{
InputStream is=s.getInputStream();
byte[] buff=new byte[1024];
is.read(buff);
String info=new String(buff);
file=new File(info);
InputStream fileIs=new FileInputStream(file);
OutputStream os=s.getOutputStream();
while(fileIs.read(buff)!=-1)
{
os.write(buff);
}
s.shutdownOutput();
s.shutdownInput();
}
public void exceute()throws IOException
{
Socket socket;
while(true)
{
socket=serverSocket.accept();
send(socket);
}
}
public static void main(String[] args)throws IOException
{
new FileServer().exceute();
}
}
//FileClient.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket; public class FileClient
{
private final int port=8000;
private final String host="localhost";
private Socket socket;
private static String fileName;
FileClient()throws IOException
{
socket=new Socket(host,port);
System.out.println("连接服务器成功");
}
public void revice()throws IOException,InterruptedException
{
OutputStream out=socket.getOutputStream();
out.write(fileName.getBytes());
InputStream is=socket.getInputStream();
byte[] buff=new byte[1024];
while(is.read(buff)!=-1)
{
OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
fileIn.write(buff);
}
}
public static void main(String[] args)
{
try
{
if(args.length==1)
{
fileName=args[0];
new FileClient().revice();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
} /* 运行:服务器
C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileServer
-------------------服务器已经启动------------------- 客户机:
C:\Documents and Settings\Administrator\桌面\新建文件夹\新建文件夹>java FileClient 1.txt
连接服务器成功
连接服务器成功 */
不是在运行FileClient时传入一个文件名作为参数吗?
while(is.read(buff)!=-1)
{
OutputStream fileIn =new FileOutputStream("f:\\"+fileName);
fileIn.write(buff);
} 把while循环里的OutputStream fileIn =new FileOutputStream("f:\\"+fileName); 放到while的上面就可以了,因为你不必没读取一次就创建一个输出流。
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at socket.FileServer.<init>(FileServer.java:12)
at socket.FileServer.main(FileServer.java:42)
我这里怎么总报这个错误,端口也换过了啊???