当前位置: 代码迷 >> J2SE >> java Socket有关问题。贴出源程序。大家帮忙看看
  详细解决方案

java Socket有关问题。贴出源程序。大家帮忙看看

热度:95   发布时间:2016-04-24 12:56:42.0
java Socket问题。贴出源程序。大家帮忙看看。
receiveServer.java
Java code
import java.io.*;import java.util.*;import java.net.*; public class receiveServer {    final int RECEIVE_PORT=9090;        public receiveServer() {        ServerSocket rServer=null;                Socket request=null;                Thread receiveThread=null;        try{            rServer=new ServerSocket(RECEIVE_PORT);            rServer.setSoTimeout(0);                        while(true){                 System.out.println("等待用户请求.");                request=rServer.accept();                 System.out.println("接收客户机连接请求.");                                receiveThread=new serverThread(request);                                receiveThread.start();                            }        }catch(IOException e){            e.printStackTrace();        }    }    public static void main(String args[]){         new receiveServer();    }}


serverThread.java
Java code
import java.net.*;import java.io.*;public class serverThread extends Thread {    Socket clientRequest;        InputStreamReader reader;    BufferedReader input;    InputStream inputStream = null;        PrintWriter output;    OutputStreamWriter writer;    OutputStream outputStream;            public serverThread(Socket s) {        this.clientRequest=s;        try{            outputStream = clientRequest.getOutputStream();            inputStream = clientRequest.getInputStream();            reader =  new InputStreamReader(inputStream);            writer =  new OutputStreamWriter(outputStream);            input  =  new BufferedReader(reader);            output =  new PrintWriter(writer,true);        }catch(IOException e){            e.printStackTrace();        }    }    public void run(){        String command=null;        String str=null;        boolean done=false;        while(!done){            try{                str=input.readLine();            }catch(IOException e){                System.out.println(e.getMessage());            }            command=str.trim().toUpperCase();            if(str==null || command.equals("QUIT"))                done=true;            else if(command.equals("HELP")){                output.println("query");                output.println("quit");                output.println("help");                output.println("sendfile");            } else if(command.startsWith("QUERY")){                output.println("query");            }else if(command.startsWith("SENDFILE")){                output.println("READY\n");                output.flush();                                try{                    String savePath = "/home/hwcd/002.xls";                    DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));                                        DataInputStream inputS = new DataInputStream(new BufferedInputStream(this.inputStream));                    System.out.println("文件的长度为:" + inputS.readLong() + "\n");                    System.out.println("开始接收文件!" + "\n");                                        DataOutputStream outputS = new DataOutputStream(this.outputStream);                                        byte[] buf=new byte[4096];                                        while(true){                        int num=0;                        num = inputS.read(buf); //客户端会一直停在这个地方。                        System.out.println("num="+num+"\n");                        if (num == -1) {                            break;                        }                        fileOut.write(buf, 0, num);                    }                                        System.out.println("接收完成:"+clientRequest.isClosed()+ "\n");                    outputS.writeUTF("OK\n");                    outputS.flush();                                        done=true;                    fileOut.close();                                    }catch(Exception e){                    e.printStackTrace();                    done=true;                }            }else if(!command.startsWith("HELP") && !command.startsWith("SENDFILE") && !command.startsWith("QUIT") && !command.startsWith("QUERY")){                output.println("Command not Found!Please refer to the HELP!");             }        }            try{            clientRequest.close();        }catch(IOException e){            System.out.println(e.getMessage());        }        command=null;    }}
  相关解决方案