实验过程中,服务器和客户端都能连接上了,但是服务器就是收不到客户端发来的字符串。
再贴一下代码:
//Mult.java
package t14_chapter;
import java.io.*;
import java.net.*;
class Mult extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Mult(Socket s)throws IOException{
socket =s;
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(new OutputStreamWriter(s.getOutputStream()),true);
start();
}
public void run() {
try{
while(true){
String str=in.readLine();
//wait(1000);
if(str.equals("END"))
break;
System.out.println("Receiving and echoing:"+str);
out.println(str);
}
System.out.println("closing...");
}catch(IOException e){}
finally{
try{
socket.close();
}catch(IOException e){}
}
}
}
//ServerSocketMult.java
package t14_chapter;
import java.io.*;
import java.net.*;
public class ServerSocketMult {
static final int PORT=8080;
public static void main(String[] args)throws IOException{
ServerSocket s=new ServerSocket(PORT);
System.out.println("Server Startes");
try{
while(true){
Socket socket=s.accept();
System.out.println("Connection success!");
try{
new Mult(socket);
}
catch(IOException e){}
finally{
socket.close();
}
}
}
catch(IOException e){}
finally{
s.close();
}
}
}
//ClientSocketMultThread.java
package t14_chapter;
import java.io.*;
import java.net.*;
class ClientSocketMultThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private static int counter=0;
private int id=counter++;
private static int threadcount=0;
public static int threadCount(){
return threadcount;
}
public ClientSocketMultThread(InetAddress addr){
System.out.println("Making client"+id);
threadcount++;
try{
socket =new Socket(addr,ServerSocketMult.PORT);
}catch(IOException e){
}
try{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
start();
}catch(IOException e){
try{
socket.close();
}catch(IOException e2){}
}
}
public void run(){
try{
for(int i=0;i<25;i++){
out.println("Client"+id+"i");
String str=in.readLine();
System.out.println(str);
}
out.println("END");
}catch(IOException e){}
finally{
try{
socket.close();
}catch(IOException e){}
threadcount--;
}
}
}
//ClientSocketMult.java
package t14_chapter;
import java.io.*;
import java.net.*;
public class ClientSocketMult {
static final int MAX_THREADS=40;
public static void main(String[] args)throws IOException,InterruptedException{
InetAddress addr=InetAddress.getByName(null);
while(true){
if(ClientSocketMultThread.threadCount()<MAX_THREADS)
new ClientSocketMultThread(addr);
Thread.sleep(100);
}
}
}
跪求大神求救!!!!