我想要做的是从套接字连接读取数据,然后将所有这些写入文件。我的读者和所有相关的陈述如下。任何想法,为什么它不工作?如果你能看到更有效的方法来做到这一点,那也是有用的。如何从套接字读取数据并将其写入文件?
(我的全代码没有成功连接到插座)
编辑:添加更多的我的代码。
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