2014-04-03 54 views
0

我是新來的網絡通信和Java編程。 我正在使Android上的應用程序(客戶端)與計算機上的服務器連接。兩個程序都連接到同一個Wifi網絡。但我有一個問題。當我從android客戶端發送消息到服務器時,什麼都沒有發生。哪裏不對?tcp連接不起作用java android

Android客戶端

try 
    { 
     Socket s = new Socket("192.168.0.101", 5555); 

     BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 

     String outMsg = ""; 
     outMsg = "jakas wiadomosc"; 
     out.write(outMsg); 
     out.flush(); 
     out.close(); 
     s.close(); 
    } 

    catch(Exception e) 
    { 
     Log.d("Error", "something goes wrong"); 
    } 
} 

和服務器計算機

try 
    { 
     ServerSocket ss = null; 

     ss = new ServerSocket(5555); 

     Socket s = ss.accept(); 

     BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 

     String incomingMsg = in.readLine() + System.getProperty("line.separator"); 
     jTextArea1.setText(incomingMsg); 
     System.out.print(incomingMsg); 

     in.close(); 

     s.close(); 
    } 
    catch(Exception e) 
    { 
     System.out.print("not work"); 
    } 
+0

做任何事情出現在logcat的? – Howli

+0

你在AndroidManifest.xml中編寫了適當的權限嗎? markubik

+0

有什麼問題? –

回答

0

對我做了一件與客戶端程序:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class WifiActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_wifi); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .commit(); 
    } 
} 

public void buttonGo(View v){ 
    new PolaczenieWifi(this).execute(); 
    } 



/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_pil_oot, container, false); 
     return rootView; 
    } 
} 
} 

,並創建另一個類PolaczenieWifi用的AsyncTask

import java.io.BufferedWriter; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.util.Log; 

public class PolaczenieWifi extends AsyncTask<Void, Void, Void> { 

Activity wywolajActivity; 

public PolaczenieWifi(Activity wywolajActivity) { 
    this.wywolajActivity = wywolajActivity; 
} 

@Override 
protected Void doInBackground(Void... arg0) { 
try 
{ 
    Socket s = new Socket("192.168.0.101", 5555); 

    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 

    String outMsg = ""; 
    outMsg = "jakas wiadomosc"; 
    out.write(outMsg); 
    out.flush(); 
    out.close(); 
    s.close(); 
} 

catch(Exception e) 
{ 
    Log.d("Error", "something goes wrong"); 
    Log.d("bład", e.toString()); 
} 
return null; 
} 

} 

但我不知道這個AsyncTask寫得不錯。在按鍵部分activity_wifi我加android:onClick="buttonGo"

服務器代碼不改變(在Netbeans的製造):

public static void main(String args[]) throws IOException { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(WifiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(WifiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(WifiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(WifiFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new WifiFrame().setVisible(true); 
     } 
    }); 

    try 
    { 
     ServerSocket ss = null; 

     ss = new ServerSocket(5555); 

     Socket s = ss.accept(); 

     BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 

     String incomingMsg = in.readLine() + System.getProperty("line.separator"); 
     jTextArea1.setText(incomingMsg); 
     System.out.print(incomingMsg); 

     in.close(); 

     s.close(); 
    } 
    catch(IOException e) 
    { 
     System.out.print("Whoops! It didn't work!\n"); 
     System.out.print(e.toString()); 
    } 
}