你安卓SDK是什么版本?2.3还是4.0以上

解决方案 »

  1.   

    可能br.readLine();
    out.write();
    这段代码有问题;
    public PrintWriter out;
     public BufferedReader br;
    使用着两个类进行读写我至今也没弄明白要怎么才能写对。
    建议你直接用input 或者outputstream进行读写。
    比如当OutputStream out  = socket.getOutputStream();
    然后将你要发送的字符串 String s;转成byte格式
    比如 btyte buf = s.getByte();
    再通过 out.write(buf)把字节流写出去。同样接收的时候也一样inputStream out  = socket.getInputStream();
    byte buf = new byte[1024]
    out.read(buf,0,1024);
    String msg = new String(buf).trim();
    通过这样的一种方式来获取数据。
    br,readLIne()是阻塞函数,如果在接收的数据中不含有'\n','\r'或者结束符时,往往导致进程挂起,从而程序无法继续。所以在发送数据的另一端,一定要记得在最后加换行符。
    http://blog.csdn.net/jiangsq12345/article/details/8254180
    至于怎么成功运用等老兄你回答我啦,反正我当时没有耐心解决就用了另一种方法,这种方法有个不好的地方就是发送字符串的时候不能按接收到独立的字符串有可能是两个两个字符串杂合在一起,这时候你得在发送进程先阻塞一下不能发送太快。
    我写了这么多怎么也得给分是吧?
      

  2.   

    我照着你说的改了,可是向客户端发信息就停止运行,大神求解。[/quote
    你的代码可以贴上来吗?具体是什么错误你也要跟我说一下,还要你的网络访问权限在Mainifest.xml中有没有添加
      

  3.   

    我照着你说的改了,可是向客户端发信息就停止运行,大神求解。[/quote
    你的代码可以贴上来吗?具体是什么错误你也要跟我说一下,还要你的网络访问权限在Mainifest.xml中有没有添加网络访问权限是加了的。我是导出后用真机安装测试的,所以也不知道是什么错误,就是一向客户端发送信息,应用程序就显示停止。部分代码如下  private Thread _thread = new Thread(){
     public void run(){
     InputStream in=null;
     String msg=null;
     Message message=null;
     message=new Message();
     Bundle bundle=null;
     bundle=new Bundle();
     byte[] buf=new byte[1024];
     try {
     //获取控件里填写的IP地址和端口号,连接服务器
    socket = new Socket(edit1.getText().toString(),Integer.parseInt(edit2.getText().toString()));
    //获得输入输出流
    out =new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
    //br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
    in=socket.getInputStream();
    }  catch (IOException e1) {
    // TODO 自动生成的 catch 块
    e1.printStackTrace();
    }
     
     
     while(true){
    try {
    //读出输入流中的信息
    in.read(buf,0,1024);
    //将读出的信息转化为字符串
    msg = new String(buf).trim();
    } catch (IOException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    //判断字符串是否为空
    if(msg!=null){
    //发送消息更新控件
    bundle.putString("mesg",msg);
                        message.setData(bundle);
    handler.sendMessage(message);
    msg=null;
    buf=null;
    }
    }

     }
     };
      

  4.   

    你首先需要确定是显示问题,还是通讯问题。很好排除的,显示问题先写死值进行测试。如果没有问题,那可能就是通讯问题。socket也很简单,就是套接字。说白了就是两条数据流,一条写,一条读。看你读取数据是否有问题。我博客上有mina的通讯例子,你可以看看的!!
      

  5.   

    感觉不是显示问题,我测试的时候用的是死值:MainActivity.edit3.setText("asd");。但是通讯问题一直没找到。
      

  6.   

    我贴一下最近我用socket做的一个简单的问价传输系统吧
    Server
    import java.io.*;
    import java.net.*;
    import java.util.Scanner;public class MyServer {
     static int portNum=1089;
     static String path;
    public static void main(String args[]) throws IOException{

    final ServerSocket ReceiverSocket = new ServerSocket(portNum);
    System.out.println("Setting the local file location...");//设置本地文件夹路径
    Scanner scan = new Scanner(System.in);
    path = scan.next(); while(true)
    {
    System.out.println("Ready to accept....");
     Socket socket =ReceiverSocket.accept();
     Thread th =new Thread(new ClientThread(socket,path));
      th.run();
    }
    }
     
    }
    class ClientThread implements Runnable
    {  
    private Socket socket ;
    private String path; 
    ClientThread(Socket socket,String path)
    {
    this.socket =socket;
    this.path = path;
    }

    public void run() {
    try {

    System.out.println("A client connected...");
         sendFileName(socket);//发送本地文件夹名中的所有文件夹名

    InputStream is =socket.getInputStream();
    byte buf []= new byte [1024];
    is.read(buf,0,1024);
    String tpath = new String(buf).trim();
    String path =this.path +"\\"+tpath;
    File file =new File(path);
    System.out.println(path);
    if(!file.exists())
    {
    OutputStream outStream = socket.getOutputStream();
        PrintWriter output = new PrintWriter(new OutputStreamWriter(outStream));
        output.print("File doesn't exist!");
        }
    else {
    OutputStream outStream = socket.getOutputStream();
        PrintWriter output = new PrintWriter(new OutputStreamWriter(outStream));
        output.print("Transport is ready!"); }
    sendFile(file,socket);

    }catch (IOException e) {

    e.printStackTrace();
    }


    }
    private static void sendFile(File file ,Socket socket) throws IOException
     {
     byte []buf = new byte[1024];
     FileInputStream in = new FileInputStream(file);
     DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
     System.out.println("Sending...");
     int i =1;
     while(in.read(buf,0,1024)>0)
     {
     dos.write(buf);
     System.out.println("已发送 "+ (i++)+"kb");
     dos.flush();
    }
     socket.close();
     dos.close();
     }
    private void    sendFileName(Socket socket) throws IOException
    {

    File f  = new File(path);
        String  nameFile[] = f.list();
        OutputStream outStream = socket.getOutputStream();     for(int i = 0;i<nameFile.length;i++)
        {
           outStream.write(nameFile[i].getBytes());
           
           outStream.flush();
           try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       }
        outStream.flush();
        outStream.write("EOF".getBytes());
     
    }

    }Clientimport java.io.*;
    import java.net.*;
    public class MyClient {
    static int  PortNum=1089;
    static String hostName;
    public static void main(String args[]) throws IOException
    {
      System.out.println("Input the host name...");
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      hostName = br.readLine();
      InetAddress receiverHost = InetAddress.getByName(hostName);
      Socket socket = new Socket();
      socket.connect(new InetSocketAddress(receiverHost, 1089));
      receieveFileName(socket) ;//接收文件名
      
     System.out.println("Input the file name...");
      String dir = br.readLine();
      System.out.println("Input the local file directory...");
      String local_dir = br.readLine().trim();
      
      OutputStream output= socket.getOutputStream();
      
      output.write(dir.getBytes());
      InputStream ip = socket.getInputStream();
     byte [] buf = new byte[1024];
     ip.read(buf);
      String reply=new String(buf);
      if(reply.equals("File doesn't exist!"))
      {
      
      System.out.println(reply);
      }
      else {
      File f = new File(local_dir);
       if(!f.exists())
       {
       f.mkdir();
       }
       DataInputStream dis = new DataInputStream(socket.getInputStream());
       //  DataOutputStream  fos = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(local_dir+"\\"+dir))));
     FileOutputStream fos = new FileOutputStream(new File(local_dir+"\\"+dir));    

       while(dis.read(buf, 0, 1024)>0)
       {
      buf = new byte[1024];
       fos.write(buf, 0, 1024);
    fos.flush();    
       }
       fos.close();
       
           }
      socket.close();}
    private static void receieveFileName(Socket socket) throws IOException
    {
    InputStream is =socket.getInputStream();

    String msg ;
    do{
    byte buf [] = new byte [1024];
    is.read(buf,0,1024);

         msg = new String(buf).trim();
         System.out.println(msg);
    }
     while(!msg.equals("EOF"));

    }
    }你参考一下具体的操作是这样的
    1.运行服务程序,设置服务程序的本地文件夹路径
    2.运行客户端,设置服务器IP一般在同一PC上运行的话都是localhost
    3.设置客户端文件下载地址
    4.获取到服务器文件名列表
    5.选择一个文件下载