Client和Server两端都有while循环,为什么只收发一次就停了,问题在哪?public class MyServer {
public static void main(String[] args) {
try {
//给服务器开放一个端口
ServerSocket ss = new ServerSocket(6868);
System.out.println("等待连接...");
while (true) {
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接"); //得到输出流,服务器向客户端发送数据
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("true");
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}
}}public class MyClient {
Reader read = null;
public static Socket socket =null;
public static void main(String[] args) throws UnknownHostException, IOException {
try{
socket = new Socket("127.0.0.1",6868);
System.out.println("6868连接成功");
}catch(Exception ex) {
System.out.println("链接服务器失败");
//new MyServer();
}
ClientThread clientthread = new ClientThread(socket);
clientthread.start();
}
}
class ClientThread extends Thread {
Socket socket = null;
public ClientThread(Socket socket) {
this.socket = socket;
}
public void run(){
try{
while(true) {
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
str = null;
//向服务器发送数据
//得到网络输出流
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("Procedure whether normal");
}
}catch(Exception ex) {
}
}
}
public static void main(String[] args) {
try {
//给服务器开放一个端口
ServerSocket ss = new ServerSocket(6868);
System.out.println("等待连接...");
while (true) {
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接"); //得到输出流,服务器向客户端发送数据
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("true");
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
}
}catch (IOException e) {
e.printStackTrace();
}
}}public class MyClient {
Reader read = null;
public static Socket socket =null;
public static void main(String[] args) throws UnknownHostException, IOException {
try{
socket = new Socket("127.0.0.1",6868);
System.out.println("6868连接成功");
}catch(Exception ex) {
System.out.println("链接服务器失败");
//new MyServer();
}
ClientThread clientthread = new ClientThread(socket);
clientthread.start();
}
}
class ClientThread extends Thread {
Socket socket = null;
public ClientThread(Socket socket) {
this.socket = socket;
}
public void run(){
try{
while(true) {
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
str = null;
//向服务器发送数据
//得到网络输出流
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("Procedure whether normal");
}
}catch(Exception ex) {
}
}
}
Socket socket = ss.accept();//等待客户端连接
System.out.println("已连接"); //得到输出流,服务器向客户端发送数据
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
pw.println("true");
//得到输入流
Scanner sc = new Scanner(socket.getInputStream());
String str = sc.nextLine();
System.out.println(str);
} 客户端连接上了,交互一次之后,又回到Socket socket = ss.accept();//等待客户端连接
这里是阻塞的等待,
传统的网络服务端编程,结构都不对
System.out.println(str);
两句就行了