JAVA服务器没回应_如果服务器没有使用java的客户端TCP请求的文件,如何显示错误消息...

我必须编写TCP客户端/服务器的Java项目。代码的目标是客户端向服务器询问文件。如果该文件可用,它将被保存到客户端目录并显示消息文件被找到并保存,否则,该文件未找到。

该代码在文件位于服务器目录时起作用,并且将保存在客户端目录中,但如果该文件不可用,它将在服务器端显示消息不可用,但在客户端,它将保存一个新的空文件不可用的文件的名称并收到文件收到的消息

你能帮忙吗?

我有服务器代码

import java.io.*;

import java.net.*;

import java.util.*;

//********************

public class Server

{

static ServerSocket ServSock;

static int QLen = 6;

//***

public static void main(String args[])

{

try

{

//*** establish server socket

ServSock = new ServerSocket(Integer.parseInt(args[0]), QLen);

while (true)

{

//*** accept a client

Socket Sock = ServSock.accept();

//*** pass the client to a new thread

new Worker(Sock).start();

}

}

catch(IOException e)

{System.out.println(e);}

}

}

//*************************

class Worker extends Thread

{

Socket Sock;

PrintWriter PW;

BufferedReader DIS;

String Question = "Enter the file name to transfer from server:";

//**************

Worker(Socket S)

{Sock=S;}

//**************

public void run()

{

try

{

//*** thread identifies itself

System.out.println("Thread: " + getName());

//*** auto-flush

//*** set up socket I/O streams

PW = new PrintWriter (new BufferedWriter (new OutputStreamWriter(Sock.getOutputStream())),true);

DIS = new BufferedReader (new InputStreamReader (Sock.getInputStream()));

//*** send server question

PW.println(Question);

//*** wait for client response

String R = DIS.readLine();

Scanner in = null;

try{

BufferedOutputStream put=new BufferedOutputStream(Sock.getOutputStream());

BufferedReader st=new BufferedReader(new InputStreamReader(Sock.getInputStream()));

String str = "/Users/Batool/Desktop/HW/Server/";

String path = str + R;

System.out.println("The requested file is path: "+path);

System.out.println("The requested file is : "+R);

File f=new File(path);

System.out.println(f.isFile());

if(f.isFile())

{

//System.out.println("inside if");

FileInputStream fis=new FileInputStream(f);

System.out.println("File transfered");

System.out.println("———————————————————————————————");

byte []buf=new byte[1024];

int read;

while((read=fis.read(buf,0,1024))!=-1)

{

put.write(buf,0,read);

put.flush();

}

}//end if

else{ System.out.println("File not found");

System.out.println("———————————————————————————————");}//end else

}//end try

catch(FileNotFoundException e)

{

System.out.println(e.getMessage());

//System.exit(0);

}//end catch

//*** close this socket connection

Sock.close();

}

catch(IOException e)

{System.out.println(e);}

}

}我有客户端代码

import java.io.*;

import java.net.*;

import java.util.*;

public class Client

{

static Socket sock;

static PrintWriter pw;

static BufferedReader br;

static String response = "";

//************************************

public static void main(String args[]) throws IOException

{

//*** establish connection to remote server

sock = new Socket(args[0],

Integer.parseInt(args[1])); //*** provide server name & port

//*** set up socket I/O streams

pw = new PrintWriter (new BufferedWriter (new OutputStreamWriter(sock.getOutputStream())),true);

br = new BufferedReader (new InputStreamReader (sock.getInputStream()));

//*** wait for server question

String r = br.readLine(); //*** THIS IS A BLOCKING CALL

System.out.println("Server asks: " + r);

//*** respond to server

/*Scanner in = new Scanner(System.in);

String s = in.nextLine();

pw.println(s);

pw.flush();

*/

Socket s=null;

BufferedInputStream get=null;

PrintWriter put=null;

try

{

get=new BufferedInputStream(sock.getInputStream());

put=new PrintWriter(sock.getOutputStream(),true);

String f;

int u;

//System.out.println("Enter the file name to transfer from server:");

DataInputStream dis=new DataInputStream(System.in);

f=dis.readLine();

put.println(f);

File f1=new File(f);

String str = "/Users/Batool/Desktop/HW/Client";

FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));

System.out.println(new File(str,f1.toString()));

System.out.println(f1.isFile());

byte jj[]=new byte[1024];

while((u=get.read(jj,0,1024))!=-1)

{

fs.write(jj,0,u);

}

fs.close();

System.out.println("File received");

System.out.println("———————————————————————————————");

sock.close();

}catch(Exception e)

{

System.out.println("File not found");

System.out.println("———————————————————————————————");

e.printStackTrace();

System.exit(0);

}

//*** close this socket connection

//sock.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值