黑马程序员技术交流社区
标题:
为什么程序只能发一次的输入
[打印本页]
作者:
z1342802487
时间:
2014-4-30 10:36
标题:
为什么程序只能发一次的输入
本帖最后由 z1342802487 于 2014-5-2 21:31 编辑
我想写一个服务程序,接受客户端发送的语句,收到信息后向客户端发送,服务端输入bye结束服务端运行。客户端输入bye结束客户端运行。为什么我的程序只能发一次?之后就输不进去了。客户端
import java.io.*;
import java.net.*;
public class TCPClient
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost",10002);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter osPrintWriter = new PrintWriter(socket.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client start to writing....");
String readline;
readline = sin.readLine();
while (!readline.equals("bye"))
{
osPrintWriter.println(readline);
osPrintWriter.flush();
System.out.println("Client:"+readline);
System.out.println("From Server:"+is.readLine());
readline=sin.readLine();
}
osPrintWriter.close();
is.close();
socket.close();
} catch (Exception e1)
{
}
}
}
复制代码
服务端代码,
import java.io.*;
import java.net.*;
public class TCPServer
{
public static void main(String[] args)
{
try
{
ServerSocket server=null;
try
{
server=new ServerSocket(10002);
} catch (Exception e)
{
}
Socket socket = null;
try
{
socket=server.accept();
System.out.println("Server has started");
} catch (Exception e1)
{
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("From Client:"+is.readLine());
line=sin.readLine();
while (!line.equals("bye"))
{
os.print(line);
System.out.println("Server:"+line);
System.out.println("From Client:"+is.readLine());
line=sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
复制代码
作者:
xtxiaolu
时间:
2014-4-30 22:06
亲,加一个循环白!while() 里面在加上判断语句,根据盘短语句输入跳出循环.
作者:
Lin0411
时间:
2014-5-1 23:31
修正后的代码如下,没有错误。
public class TCPClient
{
public static void main(String[] args)
{
try
{
Socket socket = new Socket("localhost",10002);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter osPrintWriter = new PrintWriter(socket.getOutputStream(),true);
BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Client start to writing....");
String readline;
readline = sin.readLine();
while (!readline.equals("bye"))
{
osPrintWriter.println(readline);
System.out.println("Client:"+readline);
System.out.println("From Server:"+is.readLine());
readline=sin.readLine();
}
osPrintWriter.close();
is.close();
socket.close();
}
catch (Exception e1)
{
}
}
}
public class TCPServer
{
public static void main(String[] args) throws IOException
{
ServerSocket server=null;
try
{
server=new ServerSocket(10002);
}
catch (Exception e)
{
}
Socket socket = null;
String line = null;
BufferedReader is = null;
PrintWriter os = null;
try
{
socket=server.accept();
System.out.println("Server has started");
}
catch (Exception e1)
{
e1.printStackTrace();
}
is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
os = new PrintWriter(socket.getOutputStream(),true);
BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
while (!"bye".equals(line))
{
System.out.println("From Client:"+is.readLine());
line=sin.readLine();
System.out.println("Server:"+line);
os.println(line);
line = sin.readLine();
}
os.close();
is.close();
socket.close();
server.close();
}
}
源代码的错误可能是一些具有线程阻塞性的函数顺序放错了,导致只能发送一次便阻塞了。
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2