public class JabberServer {
public static final int PORT = 8088;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started" + s);
try {
Socket socket = s.accept();
try {
System.out.println("Connection accepted" + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
while (true) {
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing:" + str);
out.println(str);
}
} finally {
System.out.println("Closing...........");
socket.close();
}
} finally {
s.close();
}
}
}public class JabberClient {
public static void main(String[] args) throws IOException {
InetAddress add = InetAddress.getByName(null);
System.out.println("add=" + add);
Socket socket = new Socket(add, JabberServer.PORT);
try {
System.out.println("Socket = " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
out.println("howdy" + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} finally {
System.out.println("Closing.............");
socket.close();
}
}
}
JabberServer程序链接能成功,但是这个循环里的东西打印不出来。
while (true) {
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing:" + str);
out.println(str);
}
JabberClient程序链接能成功,但是这个循环里的东西打印不出来。
for (int i = 0; i < 10; i++) {
out.println("howdy" + i);
String str = in.readLine();
System.out.println(str);
}
为什么,请大家指点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public static final int PORT = 8088;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT);
System.out.println("Started" + s);
try {
Socket socket = s.accept();
try {
System.out.println("Connection accepted" + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
while (true) {
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing:" + str);
out.println(str);
}
} finally {
System.out.println("Closing...........");
socket.close();
}
} finally {
s.close();
}
}
}public class JabberClient {
public static void main(String[] args) throws IOException {
InetAddress add = InetAddress.getByName(null);
System.out.println("add=" + add);
Socket socket = new Socket(add, JabberServer.PORT);
try {
System.out.println("Socket = " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
out.println("howdy" + i);
String str = in.readLine();
System.out.println(str);
}
out.println("END");
} finally {
System.out.println("Closing.............");
socket.close();
}
}
}
JabberServer程序链接能成功,但是这个循环里的东西打印不出来。
while (true) {
String str = in.readLine();
if (str.equals("END"))
break;
System.out.println("Echoing:" + str);
out.println(str);
}
JabberClient程序链接能成功,但是这个循环里的东西打印不出来。
for (int i = 0; i < 10; i++) {
out.println("howdy" + i);
String str = in.readLine();
System.out.println(str);
}
为什么,请大家指点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
解决方案 »
- 工作查LOG想做个工具自动找但是遇到难题求思路
- javal连接sql数据库
- 谢谢html:multibox的使用,还有,可以的话,我还想了解下,如何让其全部选中,
- java socket客户端和c++服务端通信。。。
- java applet数字签名
- 高分求高手指点!!急!急!急!路由器上LS选路算法和DV选路算法的实现!高手进啊!
- 如何获得本地系统的所有驱动器名 作为树形结构的根结点下的第一层数据
- 我要在画布画一个普通的按钮,看这样是否正确?
- jdk1.4.2下的各个文件的作用,越详细分越多??
- 请教,在jbuilder中,如何将菜单上的一个子菜单项和与它对应的窗口链接起来?
- 关于线程同步的问题,为什么其他线程没有启动?
- 请教一个Java问题!
public static void main(String[] args) throws IOException {
InetAddress add = InetAddress.getByName(null);
System.out.println("add=" + add);
Socket socket = new Socket(add, JabberServer.PORT);
try {
System.out.println("Socket = " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())));
for (int i = 0; i < 10; i++) {
out.println("howdy" + i);
out.flush();//;加上
String str = in.readLine();
System.out.println("from server"+str);
}
out.println("END");
out.flush(); //加上
} finally {
System.out.println("Closing.............");
socket.close();
}
}
}因为PrintWriter是有缓冲的
你的数据没有输出 所以读不到加上就可以 Server没有问题,因为你的PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
//true表示每行都会自动清空缓冲