【问题标题】:server not receiving data from java tcp/ip client服务器未从 java tcp/ip 客户端接收数据
【发布时间】:2012-03-05 01:20:36
【问题描述】:

我目前正在编写一个 java 客户端来连接到远程服务器并将纯文本推送到它。我不知道服务器使用什么语言,我被告知只需连接到某个端口上的 IP 地址,然后推送数据。

我连接得很好(显然),而在我看来,数据发送得很好。但是,在服务器端,他们只看到我连接/断开连接,没有收到任何数据。

我是 Java 中 TCP/IP 和套接字的新手,我想知道我是否正在做一些不靠谱的事情。

基本上,我的 Java 客户端会在某个目录中找到所有 .txt 文件,从这些文件中获取文本,然后将该文本推送到输出流中。一旦文件的数据被刷新,它会将文件移动到“已发送”目录,然后继续到下一个文件(如果有)。

这是我的 Java 代码:

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

public class Client
{

public class MyFileFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return name.toLowerCase().endsWith(".txt");
    }
}

public Client()
{
    try {

        String dirName = "/var/lib/adt/";
        String sentDir = "/var/lib/adt/sent/";

        File directory = new File(dirName);
        if (!directory.exists()) {
            System.out.println("directory does not exist!  Location: " + dirName);
            return;
        }

        // get files in directory
        File[] listOfFiles = directory.listFiles(new MyFileFilter());

        if (listOfFiles == null || listOfFiles.length == 0) {
            System.out.println("No files to send.");
            return;
        }

        FileInputStream fin;
        DataInputStream dis;

        String sendAddr = "192.168.1.9";
        int sendPort = 9486;

        Socket client = new Socket(sendAddr, sendPort);
        //getting the o/p stream of that connection
        PrintStream out = new PrintStream(client.getOutputStream());
        //reading the response using input stream
        BufferedReader in= new BufferedReader(new InputStreamReader(client.getInputStream()));

        System.out.println("Sending file(s) to '" + sendAddr + ":" + sendPort + "'");

        for (int i = 0; i < listOfFiles.length; i++)  {
            if (listOfFiles[i].isFile())  {
                String fName = listOfFiles[i].getName();
                if (fName.endsWith(".txt") || fName.endsWith(".TXT")) {
                    fin = new FileInputStream (dirName + fName);
                    dis = new DataInputStream(fin);

                    String message = "";
                    String messagePart = "";
                    while ((messagePart = dis.readLine()) != null)
                        message += messagePart + "\n";

                    if (message.equals(""))
                        continue;

                    System.out.println("Sending file '" + fName + "' with message: " + message);

                    out.print(message);
                    System.out.println("Written!");
                    out.flush();
                    System.out.println("Flushed!");

                    // move file to 'sent' directory
                    File dir = new File(sentDir);

                    boolean success = listOfFiles[i].renameTo(new File(dir, fName));
                    if (!success) {
                        // File was not successfully moved
                        System.out.println("File not successfully moved to 'sent' directory.");
                    }

                    dis.close();
                    fin.close();
                }
            }
        }

        in.close();
        out.close();

        client.shutdownOutput();
        if (!client.isClosed())
            client.close();

        System.out.println("Successfully sent all file(s)!");
    } catch (Exception e) {
        System.out.println("ERROR while sending file: " + e.toString());
        e.printStackTrace();
    }

}

public static void main(String a[])
{
    new Client();
}

}

对于我的控制台输出,我得到了这个:

jarrett@jarrett-Latitude-D520:~/Downloads$ java Client 
Sending file(s) to '192.168.1.9:9486'
Sending file 'blah.txt' with message: asdpoifjawpeoifjawpeoifjwapoeifjapwoie

Written!
Flushed!
Successfully sent all file(s)!

据我所知,一切都很好。有谁知道为什么会发生这种情况?有什么我明显做错了吗??

感谢任何反馈!

干杯

贾勒特

编辑:另外,顺便提一下,我编写了一个“服务器”java 文件来进行本地测试,并设法连接到它并成功地从我的客户端程序向它发送数据。

【问题讨论】:

  • 他们是希望您只传输数据还是必须发送某种标头?
  • @dm03514 最初有人告诉我,我需要做的就是将数据作为纯文本发送给他们。不过这是一个很好的问题,我再次问他们只是为了确定(他们不是以英语为母语的人,所以可能会有一些混乱)
  • 你能telnet到那个远程端口并发出任何命令帮助/版本/或什么?
  • @dm03514 显然不需要标头。
  • @Sergey Benner 我无法访问服务器:S

标签: java sockets tcp client


【解决方案1】:

Jarrett,你的代码正在发送 *something;它看起来不错,并且您正在正确刷新流,以避免它备份缓冲区并在您关闭它时丢失。

如果您想绝对确定您正在发送某些内容,请安装Wireshark,建立连接,然后在发送数据时嗅探它。您应该会看到您发送的出站流量。

如果您看到它,那么您可以让客户知道他们的服务没有正确接收数据。我的猜测是,使用他们的端点的要求比他们已经传达给您的要求更多(例如标题、编码、格式等),并且在他们的最后可能有一个解析错误杀死 处理你的数据,所以他们只是假设你从不发送任何东西,因为他们没有看到存储在数据库或类似的东西中的处理结果。

只是猜测。

【讨论】:

  • hmm..感谢您的建议-我将研究wireshark,看看它是否有帮助。再次感谢!
  • 好的,我尝试了 Wireshark..连接正在被确认,“FIN”消息正在被确认,但我的“PSH”消息没有被确认。我认为这是问题所在。但是,我不知道为什么它没有被承认。有什么想法吗??
  • 看起来对他们的系统有更多要求 - 具体来说,我需要在我的消息中添加页眉/页脚。现在它就像一个魅力。感谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多