简体   繁体   English

C#TCP客户端无法连接到IP地址

[英]C# TCP client does not connect to IP Adress

Hello I am using Android phone Wi-Fi hotspot to create network, then using C# to connect to this hotspot. 您好我正在使用Android手机Wi-Fi热点创建网络,然后使用C#连接到此热点。

The ipadress of hotspot is: 192.168.43.1. 热点的ipadress是:192.168.43.1。

First I connect to Wi-Fi hotspot by laptop Wi-Fi. 首先,我通过笔记本电脑Wi-Fi连接到Wi-Fi热点。

Now I am using C# code: 现在我正在使用C#代码:

private void connectToServer()
        {
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");

                tcpclnt.Connect("192.168.43.1", 8001);
                // use the ipaddress as in the server program

                Console.WriteLine("Connected");
                Console.Write("Enter the string to be transmitted : ");

                String str = Console.ReadLine();
                Stream stm = tcpclnt.GetStream();

                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                Console.WriteLine("Transmitting.....");

                stm.Write(ba, 0, ba.Length);

                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);

                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(bb[i]));

                tcpclnt.Close();
            }

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

But I always get this exception: 但我总是得到这个例外:

Error..... No connection could be made because the target machine actively refused it 192.168.43.1:8001 错误.....无法建立连接,因为目标计算机主动拒绝它192.168.43.1:8001

Hi, after some searching, i found the port not found in my machine, by using netstat: 嗨,经过一些搜索,我发现使用netstat在我的机器中找不到端口:

TCP    127.0.0.1:5037         admin-PC:65298         TIME_WAIT
  TCP    127.0.0.1:5037         admin-PC:65299         TIME_WAIT
  TCP    127.0.0.1:5037         admin-PC:65300         TIME_WAIT
  TCP    127.0.0.1:5037         admin-PC:65301         TIME_WAIT
  TCP    127.0.0.1:5037         admin-PC:65302         TIME_WAIT
  TCP    127.0.0.1:5037         admin-PC:65304         TIME_WAIT
  TCP    127.0.0.1:5037         admin-PC:65305         TIME_WAIT
  TCP    127.0.0.1:49165        admin-PC:49436         ESTABLISHED
  TCP    127.0.0.1:49263        admin-PC:49264         ESTABLISHED
  TCP    127.0.0.1:49264        admin-PC:49263         ESTABLISHED
  TCP    127.0.0.1:49265        admin-PC:49266         ESTABLISHED
  TCP    127.0.0.1:49266        admin-PC:49265         ESTABLISHED
  TCP    127.0.0.1:49436        admin-PC:49165         ESTABLISHED
  TCP    127.0.0.1:49559        admin-PC:49560         ESTABLISHED
  TCP    127.0.0.1:49560        admin-PC:49559         ESTABLISHED
  TCP    127.0.0.1:51477        admin-PC:51478         ESTABLISHED
  TCP    127.0.0.1:51478        admin-PC:51477         ESTABLISHED
  TCP    127.0.0.1:55300        admin-PC:55301         ESTABLISHED
  TCP    127.0.0.1:55301        admin-PC:55300         ESTABLISHED
  TCP    127.0.0.1:61797        admin-PC:61798         ESTABLISHED
  TCP    127.0.0.1:61798        admin-PC:61797         ESTABLISHED
  TCP    127.0.0.1:61800        admin-PC:61801         ESTABLISHED
  TCP    127.0.0.1:61801        admin-PC:61800         ESTABLISHED
  TCP    127.0.0.1:61807        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:61809        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:61810        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:61811        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:61813        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63271        admin-PC:63272         ESTABLISHED
  TCP    127.0.0.1:63272        admin-PC:63271         ESTABLISHED
  TCP    127.0.0.1:63274        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63275        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63279        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63284        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63304        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63351        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63353        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63354        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63355        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63356        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63357        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63358        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63359        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63367        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63368        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63370        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63373        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63377        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63378        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63385        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63386        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63387        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63388        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63389        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63396        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:63462        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:64544        admin-PC:49333         TIME_WAIT
  TCP    127.0.0.1:64545        admin-PC:64546         TIME_WAIT
  TCP    127.0.0.1:64555        admin-PC:5037          TIME_WAIT
  TCP    127.0.0.1:64557        admin-PC:5037          TIME_WAIT
  TCP    127.0.0.1:64558        admin-PC:5037          TIME_WAIT
  TCP    127.0.0.1:64919        admin-PC:5037          ESTABLISHED
  TCP    127.0.0.1:65303        admin-PC:5563          SYN_SENT
  TCP    192.168.1.34:64035     43.239.149.131:http    TIME_WAIT
  TCP    192.168.12.2:63262     192.168.12.101:22469   ESTABLISHED

I still read the answer but still get this error. 我仍然读了答案,但仍然得到这个错误。 Here is my android code: 这是我的android代码:

public class MainActivity extends Activity {

private ServerSocket serverSocket;

Handler updateConversationHandler;

Thread serverThread = null;

private TextView text;

public static final int SERVERPORT = 8001;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text2);

    updateConversationHandler = new Handler();

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

}

@Override
protected void onStop() {
    super.onStop();
    try {
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class ServerThread implements Runnable {

    public void run() {
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
        } 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();
                updateConversationHandler.post(new updateUIThread(read));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class updateUIThread implements Runnable {

    private String msg;

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

    @Override
    public  void run() {
        text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
    }
}
}

Based on the exception you are getting, the problem is not in your code, it is in the connection itself. 根据您获得的异常,问题不在您的代码中,而是在连接本身。 This can be a firewall issue or the process listening on a different port. 这可能是防火墙问题,也可能是在不同端口上侦听的进程。

EDIT: The OP has found that the problem he had was in the IIS and that resetting the IIS solved his problem . 编辑:OP发现他遇到的问题是在IIS中, 重置IIS解决了他的问题 To reset IIS, you can do this either manually or through command prompt: Run (Win+R) -> open cmd (with admin privileges) -> type "iisreset" (without "") 要重置IIS,您可以手动或通过命令提示符执行此操作:运行(Win + R) - >打开cmd(具有管理员权限) - >键入“iisreset”(不带“”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM