java如何能让某语句一直循环 5
publicstaticvoidmain(String[]args)throwsIOException{SocketechoSocket=null;PrintWriter...
public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket ( "localhost", 1111); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); } System.out.println(in.readLine()); System.out.println(in.readLine()); String userInput = "ahfpd"; out.println(userInput); while(true) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //隔一段时间} System.out.println(in.readLine()); } }}
一个简单的客户端程序,想让输出语句一直输出直到程序关闭时跳出,现在只能输出一次就不输出了,求教怎么改。 展开
一个简单的客户端程序,想让输出语句一直输出直到程序关闭时跳出,现在只能输出一次就不输出了,求教怎么改。 展开
9个回答
展开全部
测试方法:
1.创建一个新类,复制如下代码:
public static void main(String[] args) {
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try { //创建服务器,并开放3081端口
ServerSocket server = new ServerSocket(3081);
while (true) { //监听服务器端口,一旦有数据发送过来,那么就将数据封装成socket对象 //如果没有数据发送过来,那么这时处于线程阻塞状态,不会向下继续执行
socket = server.accept();
System.out.println("客户端信息:" + socket.getRemoteSocketAddress()); //从socket中得到读取流,该流中有客户端发送过来的数据
InputStream in = socket.getInputStream(); //InputStreamReader将字节流转化为字符流
br = new BufferedReader(new InputStreamReader(in)); //行读取客户端数据
String info = br.readLine();
System.out.println(info);
OutputStream out = socket.getOutputStream();
pw = new PrintWriter(out);
pw.println("服务器说:我扁死你");
pw.flush();
}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pw.close();
br.close();
socket.close();
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.创建另一个新类,复制如下代码:
public static void main(String[] args) {
Socket socket = null;
PrintWriter pw = null;
BufferedReader br = null;
try { //创建socket对象,并指明服务器的IP地址和端口号
while(true){
socket = new Socket("localhost", 3081); //得到socket发送数据的输出流
OutputStream out = socket.getOutputStream(); //将字节流包装成字符流
pw = new PrintWriter(out); //向服务器发送数据
pw.println("客户端说:建军闷烧"); //刷新流,确保数据能写到服务器
pw.flush();
InputStream in = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String info = br.readLine();
System.out.println(info);}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pw.close();
socket.close();
Thread.sleep(1000);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试方法:
先运行第一个main函数,建立起服务器
然后运行第二个函数,运行起客户端,客户端中延迟1秒
1.创建一个新类,复制如下代码:
public static void main(String[] args) {
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
try { //创建服务器,并开放3081端口
ServerSocket server = new ServerSocket(3081);
while (true) { //监听服务器端口,一旦有数据发送过来,那么就将数据封装成socket对象 //如果没有数据发送过来,那么这时处于线程阻塞状态,不会向下继续执行
socket = server.accept();
System.out.println("客户端信息:" + socket.getRemoteSocketAddress()); //从socket中得到读取流,该流中有客户端发送过来的数据
InputStream in = socket.getInputStream(); //InputStreamReader将字节流转化为字符流
br = new BufferedReader(new InputStreamReader(in)); //行读取客户端数据
String info = br.readLine();
System.out.println(info);
OutputStream out = socket.getOutputStream();
pw = new PrintWriter(out);
pw.println("服务器说:我扁死你");
pw.flush();
}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pw.close();
br.close();
socket.close();
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.创建另一个新类,复制如下代码:
public static void main(String[] args) {
Socket socket = null;
PrintWriter pw = null;
BufferedReader br = null;
try { //创建socket对象,并指明服务器的IP地址和端口号
while(true){
socket = new Socket("localhost", 3081); //得到socket发送数据的输出流
OutputStream out = socket.getOutputStream(); //将字节流包装成字符流
pw = new PrintWriter(out); //向服务器发送数据
pw.println("客户端说:建军闷烧"); //刷新流,确保数据能写到服务器
pw.flush();
InputStream in = socket.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
String info = br.readLine();
System.out.println(info);}
} catch (Exception e) { // TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
pw.close();
socket.close();
Thread.sleep(1000);
} catch (IOException e) { // TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试方法:
先运行第一个main函数,建立起服务器
然后运行第二个函数,运行起客户端,客户端中延迟1秒
展开全部
package com.bdqnsjz.homework;
class yourhomework
{
public static void main(String[] args) throws InterruptedException
{
while (1)
{
Thread.sleep(1000); //每过一秒输出一次
System.out.println("循环了一次。。。"); //这语句一直在循环
}
}
}
//已经调试过了,没问题
class yourhomework
{
public static void main(String[] args) throws InterruptedException
{
while (1)
{
Thread.sleep(1000); //每过一秒输出一次
System.out.println("循环了一次。。。"); //这语句一直在循环
}
}
}
//已经调试过了,没问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-12-27
展开全部
使用while循环,如果需要控制,添加一个boolean控制值,让其一直为true就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) throws IOException { Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { echoSocket = new Socket ( "localhost", 1111); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: localhost."); System.exit(1); } while(true) { try { Thread.sleep(1000); String userInput = "ahfpd"; out.println(userInput); System.out.println(in.readLine()); System.out.println(in.readLine()); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //隔一段时间 } System.out.println(in.readLine()); } }} } }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将输入输出分开,不要写成一句话。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询