我为android编写了一个服务器。服务器运行良好,我成功从模拟客户端的应用程序(SocketTest)发送消息。 所以我自己用C#写了一个客户端。 我尝试了我的客户端对SocketTest(当然是服务器)并且它的工作。 android的连接步骤似乎很好,我也不例外。 但我在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 Server
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 :(得分:0)
问题解决了!
string sendMSG = msgTB.Text + "\r\n";