Tcp / IP客户端和服务器应用程序 [英] Tcp/IP client and server application

查看:70
本文介绍了Tcp / IP客户端和服务器应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Can someone help me with this question please ?

Write TCP/IP client and server programs as follows:

The client should:

send the name of a file to the server, read the contents of the file back from the server, save the contents to a file.

The server should:

accept a connection from the client, read the name of a file, read the contents of the file from disk, send the contents of the file to the client, close the connection.


I have done most of the requirements in the question except the part where it asks that the client should read the contents of the file back from the server and save the contents to a file. test.txt is the specified file. I only put random contents in it, because that is not the main point, and that is why i did not include the file here. Any idea guys ?!

Thanks in Advance.

What I have tried:

ReadFileClientApp.java:







import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class ReadFileClientApp {

    public static void main(String[] args) throws Exception {

         InetAddress inet = InetAddress.getByName("localhost");
         Socket s = new Socket(inet, 2001);

         OutputStream o = s.getOutputStream();
         PrintWriter p = new PrintWriter(o);
         InputStream in = s.getInputStream();
         Scanner r = new Scanner(in);

         p.println("test.txt");
         p.flush();

        while (r.hasNextLine()){
             System.out.println(r.nextLine());
        }
         }










ReadFileServerApp.java:







import java.io.FileInputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.Scanner;

  public class ReadFileServerApp {

    public static void main(String[] args) throws Exception {
         Socket s;
         ServerSocket ss = new ServerSocket(2001);
         while (true) {
         System.out.println("Server: waiting for connection ..");
         s = ss.accept();

         InputStream in = s.getInputStream();
         Scanner r = new Scanner(in);
         OutputStream o = s.getOutputStream();
         PrintWriter p = new PrintWriter(o);

         String fileName;
         fileName = r.nextLine();
         System.out.println("File requested"+ fileName);

         FileInputStream fin = new FileInputStream(fileName);
         Scanner f = new Scanner(fin);

         while (f.hasNextLine()) {
             p.println(f.nextLine());
         }


         p.close();
         }
         }

  }

推荐答案

我讨厌你评论的方式你的代码,你已经混淆了你的输入和输出扫描仪&您的客户端和服务器代码的流等 - 让它真的难以理解!



您是否正在询问此代码



I hate the way you havnt commented your code, and you've jumbled up your input and output scanners & streams etc of your code for your client and server - makes it real hard to follow !

Are you in effect asking about this code

 p.println("test.txt");
 p.flush();

while (r.hasNextLine()){
     System.out.println(r.nextLine());
}









我认为你已经将'test.txt作为文件名发送到服务器,以便从中读取并返回数据?



如果是这样,那么,肯定不是





?

In which I take it that you've sent 'test.txt to the server as the filename for it to read and return the data from ?

if thats the case, then, surely instead of

while (r.hasNextLine()){
     System.out.println(r.nextLine());
}





你会这样做(记住我在java上非常不实用),例如: -





you would do (remembering Im very unpracticed at java), something like :-

// We've done 
//
// p.println("test.txt");
// p.flush();
//
// To send the Filename to the server - now we (hopefully) get the file data back 
//

// Declare Filename (should be done earlier since its also sent to the server)
String filenameOutput = "test.txt";

// File Object
File fileOut = new File(filenameOutput);

// If output file doesnt exist, create it - maybe if it does exist this should be extended to 'delete it' 
if (!fileOut.Exists())
{
    fileOut.createNewFile();
}

// Stream Object
FileOutputStream fileOutStream = new FileOutputStream(fileOut);

// While the reader (data back from the server) has (lines?)
while (r.hasNextLine())
{
   // Output the data from the server to the stream -> file
   fileOutStream.write(r.nextLine());
}
// Tidy Up
fileOut.close();





为了您的安全起见,我会建议您使用适当的例外对我所显示的代码进行试用/捕获,并处理任何事情我讨厌(在c#中我会提倡'使用......'块)



你的技术中还有很多假设,你假设你的文本文件是面向行的 - 更多的是一个简单的测试用例,我可能会使用字节,并且有一个专用的文件头发送回客户端,其中可能包含文件大小& crc,所以客户端可以确保它有信息来验证(字节)已经安全返回



I would suggest you put a try/catch around the code Ive shown using appropriate exceptions, for your safety, and dispose of anything I havnt (in c# I would be advocating 'using...' blocks)

There's also a lot of 'assumption' in your technique whereby you're assuming your text file is line orientated - for more that a trivial test case, I'd likely use bytes, and, have a dedicated file header sent back to the client containing perhaps the file size & crc, so the client can ensure it has the information to verify the (bytes) have arrived back safely


这篇关于Tcp / IP客户端和服务器应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆