我想用java写一个具有最简单ftp功能的程序!
头几步思路是这样的;
建立连接后!
TCPServer向客户端发送一个消息说 connection has been setted up并在客户端打印这句话
客户端看到连接建立的消息后 从键盘敲入一个命令 并将该命令发送给TCPServer 
TCOServer接受到 客户端的命令以后 
如果 命令是 exit 
再想客户端发送一条消息 说 "连接已关闭" 并打印在客户端
 并在server端 打印 "连接已被客户端关闭"
源代码在附件里 
错误是 当客户端键入命令 exit 时,该消息可以被 server接受到 
但当server向 client 发送 ""连接已关闭"" 时 却没有反应 client 也接受不到任何消息(估计server第二次没有发送成功""连接已关闭"")
io流太难了 尤其涉及网络编程这一块 
这代码 弄得我都不行了 !! 还没有做文件下载那一块呢!import java.io.*;
import java.net.*;
import java.awt.*; 
class ServerOneClient extends Thread
{
private Socket socket;
private BufferedReader in;//输入缓冲流
private PrintWriter out;//输出流
//private DataOutputStream out;
public ServerOneClient(Socket s) throws IOException//构造函数
{
socket=s;
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));//从客户端接受信息 输入流
out =new PrintWriter(socket.getOutputStream(),true);
//out=new DataOutputStream(socket.getOutputStream());
start();
}
public void run()//线程的执行
{
try{
while(true)
{
String constr="the connection has been setted up ";
//byte byteBuffer[]= new byte[1024];
//int c;
//while((c=)!=-1)
//{
out.println(constr);//+'/t'+"choose the file you want to download"+'/t'+"the file are :"+'/t'+"a.txt"+'/t'+"and if you want to exit just input exit and press enter");
//}

String str=in.readLine();
//System.out.println(str);
if(str.equals("exit"))
{
System.out.println("连接已被客户端关闭");
out.println("连接已关闭");

break;
}
else
{
//out.println("now downloading...please wait");
//System.out.println("连接成功向客户传数据");
}
}
System.out.println("closing....");
}catch(IOException e){
}
finally{
try{
socket.close();
}catch(IOException e){
};
}
}
};class TCPServer 
{
static final int PORT=8080;
public static void main(String[] args) throws IOException
{
ServerSocket s=new ServerSocket(PORT);
System.out.println("Server Started");
int count=0;
try{
while(true)
{
Socket socket=s.accept();//阻塞已知道一个连接发生
try{
new ServerOneClient(socket);
//count++;//连接一个就增一
}catch(IOException e){
//如果失败的话就关闭socket
socket.close();
}
}
}finally{
s.close();//线程关闭之 
}
}
}客户端 import java.net.*;
import java.io.*;
import java.awt.*; 
class TCPClient1 
{
public static void main(String[] args) throws IOException
{
InetAddress addr=InetAddress.getByName(null);
String str;
Socket socket=new Socket(addr,TCPServer.PORT);
try{
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//DataInputStream in=new DataInputStream(socket.getInputStream());
DataOutputStream out =new DataOutputStream(socket.getOutputStream());
//FileOutputStream out=new FileOutputStream(socket.getOutputStream());
str=in.readLine();
System.out.println(str);
String cmd;
BufferedReader bin=new BufferedReader(
new InputStreamReader(System.in)); 
cmd=bin.readLine();
out.writeBytes(cmd);
if(cmd.equals("exit"))
{
System.out.println("断开 ");
String cutmsg;
//cutmsg=in.readLine();
//System.out.println(cutmsg);
socket.close();
}
else
{
System.out.println("开始下载");
//String conmsg;
//conmsg=in.readLine();
//System.out.println(conmsg);
}

}finally{

 System.out.println("closing...");
 socket.close();
}
}
}

解决方案 »

  1.   

    流在输出数据的时候最好flush一下,out.flush();
    另外,看你的程序好像是客户端在输入exit的时候socket就关闭了,你们你的流程就成了:
    1、输入exit,客户端关闭。
    2、服务器收到exit,发送连接已被客户端关闭,然后关闭socket
    试想,客户端都关闭了,你的信息怎么发过去?
    所以,客户端关闭的时候最好不要通过客户端发出的命令关闭,可以从服务器端收到exit命令的时候再关闭,或者连接超时再关闭