C#服务器无法识别Android

用户名

这是我的服务器在C#127.0.0.1中的端口4444

    using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;


public class serv
{
    public static void Main()
    {
        try
        {
            IPAddress ipAd = IPAddress.Parse("127.0.0.1");
            // use local m/c IP address, and 

            // use the same in the client


            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 4444);

            /* Start Listeneting at the specified port */
            myList.Start();

            Console.WriteLine("The server is running at port 4444...");
            Console.WriteLine("The local End point is: " +
                              myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");
        m:
            Socket s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);

            char cc = ' ';
            string test = null;
            Console.WriteLine("Recieved...");
            for (int i = 0; i < k - 1; i++)
            {
                Console.Write(Convert.ToChar(b[i]));
                cc = Convert.ToChar(b[i]);
                test += cc.ToString();
            }

            switch (test)
            {
                case "1":
                    break;


            }

            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            s.Close();
            Console.WriteLine("\nSent Acknowledgement");


            /* clean up */
            goto m;
            myList.Stop();
            Console.ReadLine();

        }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }

}

这是我在JAVA中的android(客户端)代码

package com.example.aclient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;



public class MainActivity extends Activity {
     private Socket client;
     private PrintWriter printwriter;
     private EditText textField;
     private Button button;
     private String messsage;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      textField = (EditText) findViewById(R.id.editText1); //reference to the text field
      button = (Button) findViewById(R.id.button1);   //reference to the send button

      //Button press event listener
      button.setOnClickListener(new View.OnClickListener() {

       public void onClick(View v) {

        messsage = textField.getText().toString(); //get the text message on the text field
        textField.setText("");      //Reset the text field to blank

        try {

         client = new Socket("127.0.0.1", 4444);  //connect to server
         printwriter = new PrintWriter(client.getOutputStream(),true);
         printwriter.write(messsage);  //write the message to output stream

         printwriter.flush();
         printwriter.close();
         client.close();   //closing the connection

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

     }
    }

XML格式

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <EditText
         android:inputType="text"
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="47dp"
        android:layout_toLeftOf="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/editText1"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

C#上的Whay服务器无法识别android客户端??我不知道如何将C#服务器与android客户端连接?这是我的第一台服务器,我对服务器了解不多。所以请帮助我!

敦促

127.0.0.1localhost,您自己的机器。如果您在本地PC上使用C#托管服务器,并且您的Java应用程序看上去很像Android,并且肯定正在另一台计算机(不是您的手机,平板电脑或模拟手机或平板电脑)上运行。您需要使用两台计算机都知道的IP地址进行通信,并且两台计算机都可以追溯到同一物理设备。

在您的PC上127.0.0.1是您的PC。在平板电脑上127.0.0.1是平板电脑,而不是PC。

在网络中查找PC的IP(如果正在运行Windows,则在控制台中输入ipconfig)并使用它。确保在部署时使用公共IP。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章