当前位置: 技术问答>java相关
java.net编程时遇到的问题
来源: 互联网 发布时间:2015-11-03
本文导语: 源程序: import java.io.*; import java.net.*; public class Test { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8189); Socket socket = ss.accept(); BufferedReader in = new BufferedR...
源程序:
import java.io.*;
import java.net.*;
public class Test
{
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(8189);
Socket socket = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
out.print("Hello");
boolean done = false;
while(!done)
{
String line = in.readLine();
if(line==null)
done = true;
else
{
out.print("E :"+line);
if(line.trim().equalsIgnoreCase("bye"))
{ done = true; }
}
}
socket.close();
}
catch(IOException ioe) {System.out.println(ioe);}
}
}
然后转到dos窗口中,telnet 127.0.0.1 8189
屏幕并没有现实hello
这个程序是《java核心技术2〉的例子,运行例子没有问题,但是我自己的这个程序却没有反应,为什么?
import java.io.*;
import java.net.*;
public class Test
{
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(8189);
Socket socket = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
out.print("Hello");
boolean done = false;
while(!done)
{
String line = in.readLine();
if(line==null)
done = true;
else
{
out.print("E :"+line);
if(line.trim().equalsIgnoreCase("bye"))
{ done = true; }
}
}
socket.close();
}
catch(IOException ioe) {System.out.println(ioe);}
}
}
然后转到dos窗口中,telnet 127.0.0.1 8189
屏幕并没有现实hello
这个程序是《java核心技术2〉的例子,运行例子没有问题,但是我自己的这个程序却没有反应,为什么?
|
out.flush();
|
或者如果不想用Flush的话
那你就用println那样就会自动的Flush
那你就用println那样就会自动的Flush