如何从套接字读取数据并将其写入文件?

发布于 2024-12-02 07:05:11 字数 1346 浏览 2 评论 0原文

我想做的是从套接字连接读取数据,然后将所有数据写入文件。我的读者和所有相关陈述如下。有什么想法为什么它不起作用吗?如果您能找到一种更有效的方法来做到这一点,那也会很有用。

(我的完整代码确实成功连接到套接字)

编辑:添加了更多我的代码。

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

    Date d = new Date();
    int port = 5195;
    String filename = "";
    //set up the port the server will listen on
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(port));

    while(true)
    {

        System.out.println("Waiting for connection");
        SocketChannel sc = ssc.accept();
        try
        {

            Socket skt = new Socket("localhost", port);
            BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            FileWriter logfile = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(logfile);
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            while ((inputLine = stdIn.readLine()) != null) 
            {
                System.out.println("reading in data");
                System.out.println(inputLine);
                out.write(inputLine);
                System.out.println("echo: " + in.readLine());

            }

            sc.close();

            System.out.println("Connection closed");

        }

What I am trying to do is to read in data from a socket connection then write all of that to a file. My reader and all the related statements are below. Any ideas why it is not working? If you can see a more efficient way to do this that would also be useful.

(My full code does successfully connect to the socket)

EDIT: Added more of my code.

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

    Date d = new Date();
    int port = 5195;
    String filename = "";
    //set up the port the server will listen on
    ServerSocketChannel ssc = ServerSocketChannel.open();
    ssc.socket().bind(new InetSocketAddress(port));

    while(true)
    {

        System.out.println("Waiting for connection");
        SocketChannel sc = ssc.accept();
        try
        {

            Socket skt = new Socket("localhost", port);
            BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
            FileWriter logfile = new FileWriter(filename);
            BufferedWriter out = new BufferedWriter(logfile);
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            while ((inputLine = stdIn.readLine()) != null) 
            {
                System.out.println("reading in data");
                System.out.println(inputLine);
                out.write(inputLine);
                System.out.println("echo: " + in.readLine());

            }

            sc.close();

            System.out.println("Connection closed");

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雨成花 2024-12-09 07:05:11

您的程序要求您为从套接字读取的每一行键入一行。您输入的行数足够多吗?

您从控制台读取的行被写入文件,您是否期望来自套接字的行被写入文件?

您在哪里关闭文件(和套接字)

另一种方法是使用 Apache IOUtils 等实用程序

Socket skt = new Socket("localhost", port);
IOUtils.copy(skt.getInputStream(), new FileOutputStream(filename));
skt.close();

You program requires you to type in a line for every line you read from the socket. Are you typing enough lines?

The lines you read from the console are written to the file, did you expect the lines from the socket to be written to the file?

Where are you closing the file (and the socket)

Another approach is to use a utility like Apache IOUtils

Socket skt = new Socket("localhost", port);
IOUtils.copy(skt.getInputStream(), new FileOutputStream(filename));
skt.close();
罪歌 2024-12-09 07:05:11

我认为这行有一个拼写错误:

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

将“System.in”更改为“in”:

BufferedReader stdIn = new BufferedReader(new InputStreamReader(in));

仅供参考,这就是我喜欢读取套接字的方式。我更喜欢避免读者提供的字符串编码,而直接获取原始字节:

byte[] buf = new byte[4096];
InputStream in = skt.getInputStream()
FileOutputStream out = new FileOutputStream(filename);

int c;
while ((c = in.read(buf)) >= 0) {
  if (c > 0) { out.write(buf, 0, c); }
}
out.flush();
out.close();
in.close();

哦,可爱,事实证明代码本质上就是 IOUtils.copy() 所做的(+1 给 Peter Lawrey!):

http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/CopyUtils.java?view=markup#l193< /a>

I think there's a typo in this line:

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

Change "System.in" to just "in":

BufferedReader stdIn = new BufferedReader(new InputStreamReader(in));

FYI, here is how I like to read sockets. I prefer to avoid the string encoding offered by the readers, and just go straight for raw bytes:

byte[] buf = new byte[4096];
InputStream in = skt.getInputStream()
FileOutputStream out = new FileOutputStream(filename);

int c;
while ((c = in.read(buf)) >= 0) {
  if (c > 0) { out.write(buf, 0, c); }
}
out.flush();
out.close();
in.close();

Oh, cute, turns out that code is essentially what IOUtils.copy() does (+1 to Peter Lawrey!):

http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/CopyUtils.java?view=markup#l193

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文