了解しました。ソケットからデータを受信し、ソケットにデータを送信する必要があるプログラムを設定しようとしています。ソケットのクライアント側に特定のデータを送信させてから、サーバー側に特定のデータを送信させる方法に困惑しています。これが私が現在持っているものです。私は今のところクライアント側で本当に迷っているので、それは私のサーバー側だけです。
さらに評価するために、次のようにしたいと思いますが、ソケットのクライアント側を書き込むために何を調査すればよいかわかりません。サーバー側でコードを書き直す必要がある場合は、 ?
package sockets;
import java.net.*;
import java.io.*;
public class SocketMain {
private int port = 0;
public ServerSocket socket;
public Socket clientSock;
public SocketMain() {
init();
}
public static void main(String[] args) {
new SocketMain();
}
private void init() {
try {
socket = new ServerSocket(port);
System.out.println("Server started, bound to port: "+port);
clientSock = socket.accept();
File directory = new File("./Storage/");
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directory + "/Store.dat");
if (!file.exists()) {
file.createNewFile();
}
DataInputStream in = new DataInputStream(clientSock.getInputStream());
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
bw.write(line+"\n");
bw.flush();
bw.close();
}
socket.close();
clientSock.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}