我想要做的是從套接字連接讀取數據,然後將所有這些寫入文件。我的讀者和所有相關的陳述如下。任何想法,爲什麼它不工作?如果你能看到更有效的方法來做到這一點,那也是有用的。如何從套接字讀取數據並將其寫入文件?
(我的全代碼沒有成功連接到插座)
編輯:添加更多的我的代碼。
public static void main(String args[]) throws IOException
{
Date d = new Date();
int port = 5195;
String filename = "";
//set up the port the server will listen on
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(port));
while(true)
{
System.out.println("Waiting for connection");
SocketChannel sc = ssc.accept();
try
{
Socket skt = new Socket("localhost", port);
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
FileWriter logfile = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(logfile);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
while ((inputLine = stdIn.readLine()) != null)
{
System.out.println("reading in data");
System.out.println(inputLine);
out.write(inputLine);
System.out.println("echo: " + in.readLine());
}
sc.close();
System.out.println("Connection closed");
}
什麼是'skt'?連接到自己?爲什麼?爲什麼你不在'sc'上接受SocketChannel的任何I/O? – EJP