简体   繁体   English

Java套接字编程和流

[英]Java Socket Programming and Streams

I have written the following code for sending a text file from Client to Server using Sockets. 我编写了以下代码,用于使用套接字将文本文件从客户端发送到服务器。 I am new to both JAVA and Socket Programming. 我是JAVA和Socket编程的新手。 I feel I have got the socket concepts but Java Streams are quite confusing. 我觉得我已经有了套接字的概念,但是Java Streams令人困惑。 Any help with the following code will be really helpful. 以下代码的任何帮助将非常有帮助。 PLEASE mention the mistake and Solution with reasoning. 请在推理中提及错误和解决方案。

The data seems to be sent just fine. 数据似乎发送正确。 But it is not received on the other end. 但是另一端没有收到。

Server Code: 服务器代码:

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(3000);
        while(true)
        {
            Socket convSocket = serverSocket.accept();
            System.out.println("connection accepted");

            BufferedReader in = new BufferedReader(new InputStreamReader(convSocket.getInputStream()));
            FileWriter fileOut = new FileWriter("/Users/aakashmalhotra/a.txt");

            int c;
            while( (c = in.read()) != -1){
                fileOut.write(c);
            }
            System.out.println("Transfer Done");
        }


    }
}

Client Code: 客户代码:

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] argv) throws Exception {
        Socket clientSocket = new Socket("localhost", 3000); // create a socket

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        BufferedReader fileIn = new BufferedReader(new FileReader("/Users/aakashmalhotra/s.txt"));

        int c;
        while ((c = fileIn.read()) != -1) {
            out.write(c);
        }

    }
}

您需要在客户端使用.flush()或.close()输出编写器。

IO objects, and especially streams, always need to be closed. IO对象(尤其是流)始终需要关闭。 This is because the OS is optimized for these type of operations, and it decides when the data should be sent on the pipe. 这是因为操作系统针对这些类型的操作进行了优化,并且可以决定何时在管道上发送数据。 Closing the resource make sure that the remainder of the data is sent. 关闭资源,确保其余数据已发送。 Example for the server side: 服务器端示例:

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(3000);
        while (true) {
            Socket convSocket = serverSocket.accept();
            System.out.println("connection accepted");

            try (BufferedReader in = new BufferedReader(new InputStreamReader(convSocket.getInputStream()))) {
                try (FileWriter fileOut = new FileWriter("/Users/aakashmalhotra/a.txt")) {
                    int c;
                    while ((c = in.read()) != -1) {
                        fileOut.write(c);
                    }
                    System.out.println("Transfer Done");
                }
            }
        }
    }
}

It is important to note the usage of a try-with-resource statement, which ensures that the streams will be closed regardless of the outcome of your application. 重要的是要注意try-with-resource语句的用法,该语句可确保关闭流,而不管应用程序的结果如何。 This guarantees that your stream will always be closed and that no resource will leak. 这样可以确保您的流将始终关闭,并且不会泄漏任何资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM