Problem with the socket client: bind returns -1



  • I can't use bind. When the program starts, it's doing great, but when it comes to bind, then bind returns "-1." Thank you.

    Here's the source:

    KLEENTE

    #include <cstdlib>
     #include <iostream>
     #include <winsock.h>
    using namespace std;
    

    int main(){
    setlocale(LC_ALL,"rus");
    char url[100];
    int port;
    char my_name[30] = "ASA";
    std::cout << "Url: ";
    cin >> url;
    cout << endl;
    std::cout << "Port: ";
    cin >> port;
    cout << endl;

    WSADATA WsaData;
    if (int err = WSAStartup (MAKEWORD(2, 0), &WsaData) != 0)
    {
    std::cout << "Socket not Loaded!\n";
    }else{
    std::cout << "Socket Loaded \n";
    }

    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1){
    std::cout << "Error! Socket no created.\n" ;
    }else{
    std::cout << "Socket Create.\n";
    }

    sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    addr.sin_addr.s_addr=inet_addr(url);
    int locate;
    locate = connect(sock, (sockaddr *)&addr, sizeof(addr));

    if (locate < 0){

        std::cout &lt;&lt; "Fatal Error!\n";   
           system("pause");
           }else{
    char cut[10000];
    char get[10000];
    send(sock, my_name, 30, 0);
    recv(sock, get, 10000, 0);
    cout &lt;&lt; get &lt;&lt; endl;
    std::cout&lt;&lt;"Enter get: ";
    std::cin&gt;&gt;cut;
                 
             send(sock, cut, 10000, 0);
             
                               system("pause");
                 
                 }
    }
    

    SERVER

    #include <cstdlib>
    #include <iostream>
    #include <winsock.h>
    using namespace std;

    int main()
    {
    char client_name[30];
    char sv_name[30];
    WSADATA WsaData;
    if (int err = WSAStartup(MAKEWORD(2, 0), &WsaData) != 0) {
    std::cout << "Socket not Loaded!\n";
    } else {
    std::cout << "Socket Loaded \n";
    }

    gethostname(sv_name, 30);
    
     int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); /* исправлено sock*/
    if (sock == -1) { /* исправлено sock */
        std::cout &lt;&lt; "Error! Socket no created.\n";
    } else {
        std::cout &lt;&lt; "Socket Create.\n";
    }
    
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080); /* исправлено */
    addr.sin_addr.s_addr = htonl(INADDR_ANY); /* исправлено */
    

    int bindet = bind(sock, (sockaddr *)&addr, sizeof(addr));
    if (bindet == -1){
    std::cout << "Binding Error!\n";

            system("pause"); 
              }else {
                   int listening = listen (sock, 100);
    
                  
                                
         std::cout &lt;&lt; "Server Name: " &lt;&lt; sv_name &lt;&lt; endl &lt;&lt; "Wait for connecting ...\n"; 
      int acc = accept(sock, (sockaddr*)&amp;addr, 0);  
        recv(acc, client_name, 30, 0); 
        
                              cout &lt;&lt;"Connected: " &lt;&lt; client_name &lt;&lt; "\n";
                              if (client_name == "ASA"){
                                              
                               char urls[100];               
                              send(acc, sv_name, strlen(sv_name) + 1, 0);                 
                              int rec = recv(acc, urls, 100, 0); 
                               if (rec &gt; 0){
                               int i;              
                                              
        while(i &lt; 100){
                send(acc, urls, 30, 0);
                
                ++i;
                }
                system("pause"); 
                }
              }}
    

    }



    1. bind() You're here for the client skate. No. !

    The local address of this sketch will be automatically assigned to connect()♪ If you still want to give the client a specific address, in the field. addr.sin_addr.s_addr One of the local IPs should be placed. INADDR_ANY)

    1. memcpy() Copy the IP server in the field. addr.sin_addr.s_addrNot in sin_family

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2