【发布时间】: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