socket服务器未从java tcp/ip客户端接收数据
我目前正在编写一个java客户机来连接到远程服务器并将纯文本推送到它。我不知道服务器使用什么语言,我被告知只需连接到某个端口上的IP地址,然后推送数据
我连接得很好(显然),在我这方面,数据似乎发送得很好。然而,在服务器端,他们只看到我连接/断开连接,没有收到任何数据
我不熟悉Java中的TCP/IP和socket,我想知道我是否在做一些不可靠的事情
基本上,我的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文件来进行本地测试,并成功地连接到它并从我的客户端程序发送数据
# 1 楼答案
Jarrett,你的代码发送了一些信息;它看起来很好,并且您正在正确地刷新流,以避免它备份缓冲区,并在关闭缓冲区时丢失
如果您想绝对确保您正在发送某些内容,请安装Wireshark,建立连接,然后在发送数据时嗅探它。你应该看到你正在发送的出站流量
如果您看到它,那么您可以让客户机知道他们的服务没有正确地接收数据。我的猜测是,使用端点的要求比他们向您传达的要求更多(例如,标题、编码、格式等),并且在端点上可能出现解析错误,导致数据的处理,所以他们只是假设你从来没有发送过任何东西,因为他们没有看到存储在DB或其他等效文件中的处理结果
只是猜测而已