2016-06-29 86 views
1

我寫了一個android的服務器。服務器工作良好,我成功地發送來自應用程序的模擬客戶端(SocketTest)的消息。 所以我用C#寫了一個客戶端。 我試過我的客戶端對SocketTest(當然作爲服務器),它的工作。 到android的連接步驟似乎很好,我也不例外。 但我沒有看到我的Android設備中的消息。C#Tcp客戶端不能發送消息到Android服務器

C#代碼

{ 
public partial class sendSms : Form 
{ 
    TcpClient client; 
    NetworkStream ns; 
    public sendSms() 
    { 
     InitializeComponent(); 
    } 

    private void Connect() 
    { 
     client = new TcpClient("192.168.14.76", 8080); 
     //client = new TcpClient("127.0.0.1", 8080); 
     ns = client.GetStream(); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Connect(); 
    } 

    private void sendBtn_Click(object sender, EventArgs e) 
    { 
     SendMessage(); 

    } 

    private void SendMessage() 
    { 
     string sendMSG = msgTB.Text; 
     ns.Write(Encoding.UTF8.GetBytes(sendMSG), 0, sendMSG.Length); 
     ns.Flush(); 


    } 
} 

}

的Andorid服務器

public class MainActivity extends AppCompatActivity { 

private ServerSocket serverSocket; 
Handler UiHandler; 
Thread thread = null; 
private TextView textView; 
private static final int PORT = 8080; 
private String status = null; 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    textView = (TextView) findViewById(R.id.ipTextView); 
    UiHandler = new Handler(); 
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 
    String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress()); 
    textView.setText("Ip - " + ipAddress + ", Port - " + PORT); 
    // statusTxt.setText("Hello"); 

    this.thread = new Thread(new ServerThread()); 
    this.thread.start(); 

} 


class ServerThread implements Runnable { 

    public void run() { 
     Socket socket = null; 
     try { 
      serverSocket = new ServerSocket(PORT); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (!Thread.currentThread().isInterrupted()) { 

      try { 

       socket = serverSocket.accept(); 

       CommunicationThread commThread = new CommunicationThread(socket); 
       new Thread(commThread).start(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

class CommunicationThread implements Runnable { 

    private Socket clientSocket; 

    private BufferedReader input; 

    public CommunicationThread(Socket clientSocket) { 

     this.clientSocket = clientSocket; 

     try { 

      this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void run() { 

     while (!Thread.currentThread().isInterrupted()) { 

      try { 

       String read = input.readLine(); 

       UiHandler.post(new updateUIThread(read)); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

public String[] getPhoneAndMSG(String msg) 
{ 
    return msg.split("&"); 
} 

class updateUIThread implements Runnable 
{ 
    private String msg; 

    public updateUIThread(String str) { 
     this.msg = str; 
    } 

    @Override 
    public void run() { 
     try { 
      textView.setText(msg); 
      String[] numAndMsG = getPhoneAndMSG(msg); 
      // SmsManager smsManager = SmsManager.getDefault(); 
      // Toast.makeText(getApplicationContext(), numAndMsG[0] +" " +numAndMsG[1] , Toast.LENGTH_LONG).show(); 
      // smsManager.sendTextMessage(numAndMsG[0], null, numAndMsG[1], null, null); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "SMS Faild", Toast.LENGTH_LONG).show(); 

     } 

     // textView.setText(textView.getText().toString()+"Client Says: "+ msg + "\n"); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

回答

0

問題解決了!

string sendMSG = msgTB.Text + "\r\n"; 
相關問題