現在、リモートサーバに接続しプレーンテキストをプッシュするJavaクライアントを作成しています。サーバーの言語がわからないので、特定のポートのIPアドレスに接続してからデータをプッシュするように指示されています。サーバがjava tcp/ipクライアントからデータを受信しない
私はちょうど良い(明らかに)接続している、と私の最終的には、データが正常に送信されているようだ。しかし、サーバー側では、データが受信されることなく、接続/切断のみが表示されます。
私はTCP/IPとJavaのソケットを使い慣れていて、おそらく私が最後に何かをしているのだろうかと思っています。
基本的に、私のJavaクライアントは特定のディレクトリ内のすべての.txtファイルを見つけ出し、それらのファイルからテキストを取り出し、そのテキストを出力ストリームにプッシュします。ファイルのデータがフラッシュされると、ファイルは 'sent'ディレクトリに移動され、次のファイル(存在する場合)に移動します。ここで
は私の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();
}
}
私はこれを取得:
[email protected]:~/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)!
だから、私の知る限り、すべては私の端に罰金です。なぜこれが起こっているのか誰にも分かりますか?私は明らかに私の終わりに間違っている何かがありますか?
フィードバックに感謝します。
乾杯
ジャレット
EDIT:また、ちょうど私がローカルでテストする「サーバ」のjavaファイルを書き、それに接続し、正常に私のクライアントプログラムからそれにデータを送信するために管理し、言及します。
あなたは単にデータをストリームすることを期待しているのですか、何らかのヘッダーを送信する必要がありますか? – dm03514
@ dm03514私は元々、データをプレーンテキストとして送信する必要があると言われました。それは良い質問ですが、私は確かに彼らに再度質問しています(英語のネイティブスピーカーではないので混乱が起こっている可能性があります) – Jarrett
あなたはそのリモートポートにtelnetして、コマンドのヘルプ/バージョン/ か何か?明らかに、ヘッダーは必要ありません。 –