简体   繁体   English

在我的C套接字编程函数中,connect不成功

[英]connect is unsuccessful in my socket programming function in C

I have a function which initiates the socket connection. 我有一个启动套接字连接的功能。 I am trying to a server with a name let's say xp. 我正在尝试使用名称叫xp的服务器。 Socket() is working fine. Socket()工作正常。 But when it comes to connect, I am getting an error. 但是,当进行连接时,我遇到了错误。 I tried to get the IP of that server, I got some random IP. 我试图获取该服务器的IP,但得到了一些随机IP。 I passed the parameters to the connect API. 我将参数传递给connect API。 I print the results of these in a log file. 我将这些结果打印在日志文件中。 I think error lies within the connect(). 我认为错误在于connect()中。 I am working on Linux Ubuntu. 我正在使用Linux Ubuntu。 Here is my program for SocketInit(). 这是我的SocketInit()程序。 I can't get the error with that. 我无法理解错误。

I call the SocketInit function as SocketInit(argv[2]); 我将SocketInit函数称为SocketInit(argv [2]);。 argv[2] has my server name. argv [2]有我的服务器名称。

short SocketInit(char *xp)
{
      if ( (local_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
        printf("socket creation is unsuccessful check in SocketInit() \n");
        sprintf(log_msg, "create socket descriptor error = %d", errno);
        LogMsg('E', log_msg);
        return(-1);
      }
      else
      {
        printf("socket connection is success\n");
      }

      pos_socket.sin_family = AF_INET;
      pos_socket.sin_port   = htons(port_no);
      pos_socket.sin_addr.s_addr = inet_addr(xp);

      if ( connect( local_socket, (struct sockaddr *) &pos_socket, sizeof(pos_socket) ) < 0 ) {
        sprintf(log_msg, "connect on socket error=%d", errno);
        printf("socket connect api is unsuccessful check in SocketInit() \n");
        LogMsg('E', log_msg);
        return(-1);
      }
      else{
        printf("connect is successful\n");
        return 0;
      }

}

How can I connect to the server. 如何连接到服务器。 How can I pass the address to the pos_socket.sin_addr.s_addr ? 如何将地址传递给pos_socket.sin_addr.s_addr? Sometimes I get connect error 110 and 111. But still I can't connect. 有时会出现连接错误110和111。但是仍然无法连接。

Use perror() to print the human-readable error string when connect() or most other unix-like system calls return an error. connect()或大多数其他类似Unix的系统调用返回错误时,请使用perror()打印人类可读的错误字符串。 But since you told us the value of errno, I looked in errno.h for the meaning, and found: 但是由于您告诉我们errno的值,所以我在errno.h查找了含义,然后发现:

#define ETIMEDOUT   110 /* Connection timed out */
#define ECONNREFUSED    111 /* Connection refused */

(BTW, you cannot count on errno's being the same from one unix to another which is why you need to use these defines when checking for specific errors. Never hard-code numeric errno values into your code. It worked out for me this time, but it won't necessarily every time). (顺便说一句,您不能指望从一个Unix到另一个Unix的errno相同,这就是为什么在检查特定错误时需要使用这些定义的原因。永远不要将数字errno值硬编码到您的代码中。这对我来说很有效,但不一定每次都如此)。

ECONNREFUSED means that there was a machine listening at the specified IP address, but that no process was listening for connections on the specified port number. ECONNREFUSED表示有一台机器正在侦听指定的IP地址,但是没有进程在侦听指定端口号上的连接。 Either the remote process is not running, it is not binding or accepting connection properly, or it potentially could be blocked by some sort of firewall. 远程进程未运行,未正确绑定或接受连接,或者可能被某种防火墙阻止。

In any case, this points to a problem with the server. 无论如何,这都指向服务器的问题。

So, check to make sure your remote process is actually ready to accept the connection. 因此,请检查以确保您的远程进程实际上已准备好接受连接。 You can use telnet or netcat as a test client to see if other client programs that are known to work are able to connect to your server. 您可以将telnetnetcat用作测试客户端,以查看其他已知有效的客户端程序是否能够连接到服务器。

Also, I notice that your variable port_no is not declared, so we have no way of knowing what port you are trying to connect to.. but make sure that this variable is of the correct type and has the correct value for the service you are trying to connect to. 另外,我注意到您的变量port_no没有声明,因此我们无法知道您要连接的端口..但是请确保该变量的类型正确,并且对于您所使用的服务具有正确的值尝试连接。 If port_no doesn't specify the correct port you will get the same type of error. 如果port_no没有指定正确的端口,您将得到相同类型的错误。

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

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