Tcp客户端不能向android服务器发送消息

本文关键字:消息 服务器 android 客户端 不能 Tcp | 更新日期: 2023-09-27 17:53:05

我为android写了一个服务器。服务器工作良好,我成功地从模拟客户端(SocketTest)的应用程序发送消息。所以我自己用c#写了一个客户端。我尝试了我的客户端对SocketTest(当然作为服务器),它的工作。与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);
}

}

Tcp客户端不能向android服务器发送消息

问题解决了!

string sendMSG = msgTB.Text + "'r'n";