写了段网络编程代码,结果出现了很奇怪的问题。
客户端一次请求得到2次回复,第二次回复为4个空字节,而服务器确实只flush了一次。
而且,客户端打印服务端回复,只打印了一半,后半段要再请求一次才能打印出来。
以下是代码,求大神解答
客户端:
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private static Socket socket;
/**
* @param args
* @throws IOException
* @throws UnknownHostException
* @throws InterruptedException
*/
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
socket=new Socket("localhost",8999);
System.out.println("连接服务端成功");
PrintWriter ow = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Scanner scan=new Scanner(System.in);
String str;
while(true){
char[] buf=new char[1024];
System.out.println("请输入请求:");
str=scan.next().trim();
if(str.equals("quit"))
break;
System.out.println("发送请求");
ow.write(str);
ow.flush();
System.out.println("等待服务端回复");
StringBuffer sb=new StringBuffer();
int rd=0;
if((rd=br.read(buf))>0){
for(int j=0;j<rd;j++)
{sb.append(buf[j]);
}
System.out.println("=============="+sb.toString()+"=============");
}
}
ow.close();
br.close();
socket.close();
}
}
服务端:
package server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private static ServerSocket server;
private static ArrayList<Thread> threadGroup = new ArrayList<Thread>();
/**
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String[] args) throws IOException,
InterruptedException {
server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(8999));
Socket client;
while (true) {
System.out.println("等待客户端的连接~~");
client = server.accept();
Child child = new Server().new Child(client);
Thread thread = new Thread(child);
thread.start();
System.out.println("连接成功~~~");
threadGroup.add(thread);
child.setGroup(threadGroup);
while (threadGroup.size() > 4) {
Thread.sleep(1000);
}
if (threadGroup.size() == 0)
break;
}
server.close();
}
private class Child implements Runnable {
Socket client = null;
ArrayList<Thread> group = new ArrayList<Thread>();
public Child(Socket client) {
this.client = client;
}
public void setGroup(ArrayList<Thread> threadGroup) {
this.group = threadGroup;
}
@Override
public void run() {
BufferedReader br = null;
PrintWriter ow = null;
try {
ow = new PrintWriter(client.getOutputStream());
br = new BufferedReader(new InputStreamReader(
client.getInputStream()));
char[] buf = new char[1024];
int i = 0;
while (true) {
if (i == 4) {
ow.write("您已达到最大访问次数");
break;
}
i++;
System.out.println(i + "等待客户端请求");
int rd=0;
try {
rd = br.read(buf);
} catch (Exception e) {
System.out.println(client.getInetAddress());
br.close();
ow.close();
group.remove(this);
client.close();
return;
}
ow.write("服务回应" + buf[0]);
for (int j = 1; j < buf.length; j++) {
ow.append(buf[j]);
}
ow.flush();
}
br.close();
ow.close();
group.remove(this);
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
------解决方案--------------------
while (true) {
if (i == 4) {