gpt4 book ai didi

java - C# Tcp 客户端无法向 android 服务器发送消息

转载 作者:太空宇宙 更新时间:2023-11-04 12:28:42 24 4
gpt4 key购买 nike

我为 Android 编写了一个服务器。服务器运行良好,我成功从模拟客户端的应用程序发送消息(SocketTest)。于是我自己用C#写了一个客户端。我在 SocketTest 上尝试了我的客户端(当然作为服务器)并且它工作了。与 android 的连接步骤似乎很好,我也不异常(exception)。但我在 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);
}

}

最佳答案

问题解决了!

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

关于java - C# Tcp 客户端无法向 android 服务器发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com