程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-JAVA多線程Socket通信時遇到的問題???

java-JAVA多線程Socket通信時遇到的問題???

編輯:編程綜合問答
JAVA多線程Socket通信時遇到的問題???

實驗過程中,服務器和客戶端都能連接上了,但是服務器就是收不到客戶端發來的字符串。
圖片說明
再貼一下代碼:
//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);
}
}
}

跪求大神求救!!!!

最佳回答:


測試了你的代碼,存在的問題如下:
1 你的ServerSocketMult類的finally{socket.close();}導致你的Mul線程剛開始處理Socket時,這個socket就已經關閉了。所以這個操作多余而且還是問題的根源,因為你已經咋Mul類處理完成後寫了socket.close代碼了。
2 ClientSocketMult這個類啟動指定N個線程,所以對線程數的操作threadcount--;是在多個線程中的,需要對threadcount變量進行同步操作。
3 所有的異常分支,你都是空語句,所以淹沒了異常導致你無法排錯。其實收不到響應數據就是因為1中你關閉了Socket導致的,只要打印了異常就很容易定位的。
修正ServerSocketMult代碼:

 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!");
                new Mult(socket);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            s.close();
        }
    }
}

再修正Mult

 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() {
        System.out.println("print received ...");
        try {
            String str = in.readLine();
            StringBuffer buffer = new StringBuffer();
            //讀取Socket的請求數據,並回應
            while (str!=null&&!str.equals("END")) {
                buffer.append(str);
                str = in.readLine();
            }
            String value = buffer.toString();
            out.println(value);
            System.out.println("Receiving and echoing:" + value);
            System.out.println("closing...");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

修正ClientSocketMultThread

 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 synchronized static int threadCount() {
        return threadcount;
    }

    public synchronized static void addThreadCount(){
        threadcount++;
    }

    public synchronized static void descThreadCount(){
        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);
            }
            out.println("END");

            String str = in.readLine();
            System.out.println("getResponse:"+str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
            }

            descThreadCount();
        }
    }

}

測試Ok,就能收到正確的請求及回復信息。

qq_33563354
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved