Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. socket programming using vc++

socket programming using vc++

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpquestionc++tutorial
19 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rahuljin
    wrote on last edited by
    #1

    hi, i am doing some socket programming. the problem is that when i use the port number 80, then the error is 10051 which means Network is unreachable, but my network is working find. also when i use any random number for port, the error is 10061 which means Connection refused. please guide me. here is my code ----

    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>

    #define NETWORK_ERROR -1
    #define NETWORK_OK 0

    void ReportError(int, const char *);

    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
    {
    WORD sockVersion;
    WSADATA wsaData;
    int nret;
    struct in_addr addr;
    addr.s_addr = inet_addr("127.0.0.1");

    sockVersion = MAKEWORD(1, 1);
    
    
    // Initialize Winsock as before
    WSAStartup(sockVersion, &wsaData);
    
    
    // Store information about the server
    LPHOSTENT hostEntry;
    
    hostEntry = gethostbyaddr((char \*) &addr, 4, AF\_INET);	// Specifying the server by its name;
    						// another option: gethostbyaddr()
    
    if (!hostEntry)
    {
    	nret = WSAGetLastError();
    	ReportError(nret, "gethostbyaddr()");	// Report the error as before
    
    	WSACleanup();
    	return NETWORK\_ERROR;
    }
    
    
    // Create the socket
    SOCKET theSocket;
    
    theSocket = socket(AF\_INET,			// Go over TCP/IP
    		   SOCK\_STREAM,			// This is a stream-oriented socket
    		   IPPROTO\_TCP);		// Use TCP rather than UDP
    if (theSocket == INVALID\_SOCKET)
    {
    	nret = WSAGetLastError();
    	ReportError(nret, "socket()");
    
    	WSACleanup();
    	return NETWORK\_ERROR;
    }
    
    
    // Fill a SOCKADDR\_IN struct with address information
    SOCKADDR\_IN serverInfo;
    
    serverInfo.sin\_family = AF\_INET;
    
    // At this point, we've successfully retrieved vital information about the server,
    // including its hostname, aliases, and IP addresses.  Wait; how could a single
    // computer have multiple addresses, and exactly what is the following line doing?
    // See the explanation below.
    
    serverInfo.sin\_addr = \*((LPIN\_ADDR)\*hostEntry->h\_addr\_list);
    
    serverInfo.sin\_port = htons(80);		// Change to network-byte order and
    						// insert into port field
    
    
    // Connect to the server
    nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
    if (nret == SOCKET\_ERROR)
    {
    	nret = WSAGetLastError();
    	ReportError(nret, "connect()");
    
    	WSACleanup();
    	return NETWORK\_ERROR;
    }
    
    MessageBoxA(NULL, "Great, it is good", "socket Successful", MB\_OK);
    // Successfully connected!
    
    
    // Send/receive, then cleanup:
    
    G N 2 Replies Last reply
    0
    • R rahuljin

      hi, i am doing some socket programming. the problem is that when i use the port number 80, then the error is 10051 which means Network is unreachable, but my network is working find. also when i use any random number for port, the error is 10061 which means Connection refused. please guide me. here is my code ----

      #include <windows.h>
      #include <winsock.h>
      #include <stdio.h>

      #define NETWORK_ERROR -1
      #define NETWORK_OK 0

      void ReportError(int, const char *);

      int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
      {
      WORD sockVersion;
      WSADATA wsaData;
      int nret;
      struct in_addr addr;
      addr.s_addr = inet_addr("127.0.0.1");

      sockVersion = MAKEWORD(1, 1);
      
      
      // Initialize Winsock as before
      WSAStartup(sockVersion, &wsaData);
      
      
      // Store information about the server
      LPHOSTENT hostEntry;
      
      hostEntry = gethostbyaddr((char \*) &addr, 4, AF\_INET);	// Specifying the server by its name;
      						// another option: gethostbyaddr()
      
      if (!hostEntry)
      {
      	nret = WSAGetLastError();
      	ReportError(nret, "gethostbyaddr()");	// Report the error as before
      
      	WSACleanup();
      	return NETWORK\_ERROR;
      }
      
      
      // Create the socket
      SOCKET theSocket;
      
      theSocket = socket(AF\_INET,			// Go over TCP/IP
      		   SOCK\_STREAM,			// This is a stream-oriented socket
      		   IPPROTO\_TCP);		// Use TCP rather than UDP
      if (theSocket == INVALID\_SOCKET)
      {
      	nret = WSAGetLastError();
      	ReportError(nret, "socket()");
      
      	WSACleanup();
      	return NETWORK\_ERROR;
      }
      
      
      // Fill a SOCKADDR\_IN struct with address information
      SOCKADDR\_IN serverInfo;
      
      serverInfo.sin\_family = AF\_INET;
      
      // At this point, we've successfully retrieved vital information about the server,
      // including its hostname, aliases, and IP addresses.  Wait; how could a single
      // computer have multiple addresses, and exactly what is the following line doing?
      // See the explanation below.
      
      serverInfo.sin\_addr = \*((LPIN\_ADDR)\*hostEntry->h\_addr\_list);
      
      serverInfo.sin\_port = htons(80);		// Change to network-byte order and
      						// insert into port field
      
      
      // Connect to the server
      nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
      
      if (nret == SOCKET\_ERROR)
      {
      	nret = WSAGetLastError();
      	ReportError(nret, "connect()");
      
      	WSACleanup();
      	return NETWORK\_ERROR;
      }
      
      MessageBoxA(NULL, "Great, it is good", "socket Successful", MB\_OK);
      // Successfully connected!
      
      
      // Send/receive, then cleanup:
      
      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      not sure I fully understand your issue you've created a tcp socket client app 1) you try to connect to (local loopback address 127.0.0.1) on port 80 and get an error - do you have an ethernet adaptor, and tcp/ip set up on your machine ? - you should be able to ping yourself on 127.0.0.1 and get a valid response before you try this - then you'll need something listening on port 80 - depending on what sort of a machine, it could be IIS for example, or a server harness 2) you try a random port - unless you fix the issues at (1), its not going to work AND you would need something listening on all the ports on machine to catch the connect on a 'random' port - I dont see what a random port does for your case 'g'

      R 1 Reply Last reply
      0
      • R rahuljin

        hi, i am doing some socket programming. the problem is that when i use the port number 80, then the error is 10051 which means Network is unreachable, but my network is working find. also when i use any random number for port, the error is 10061 which means Connection refused. please guide me. here is my code ----

        #include <windows.h>
        #include <winsock.h>
        #include <stdio.h>

        #define NETWORK_ERROR -1
        #define NETWORK_OK 0

        void ReportError(int, const char *);

        int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
        {
        WORD sockVersion;
        WSADATA wsaData;
        int nret;
        struct in_addr addr;
        addr.s_addr = inet_addr("127.0.0.1");

        sockVersion = MAKEWORD(1, 1);
        
        
        // Initialize Winsock as before
        WSAStartup(sockVersion, &wsaData);
        
        
        // Store information about the server
        LPHOSTENT hostEntry;
        
        hostEntry = gethostbyaddr((char \*) &addr, 4, AF\_INET);	// Specifying the server by its name;
        						// another option: gethostbyaddr()
        
        if (!hostEntry)
        {
        	nret = WSAGetLastError();
        	ReportError(nret, "gethostbyaddr()");	// Report the error as before
        
        	WSACleanup();
        	return NETWORK\_ERROR;
        }
        
        
        // Create the socket
        SOCKET theSocket;
        
        theSocket = socket(AF\_INET,			// Go over TCP/IP
        		   SOCK\_STREAM,			// This is a stream-oriented socket
        		   IPPROTO\_TCP);		// Use TCP rather than UDP
        if (theSocket == INVALID\_SOCKET)
        {
        	nret = WSAGetLastError();
        	ReportError(nret, "socket()");
        
        	WSACleanup();
        	return NETWORK\_ERROR;
        }
        
        
        // Fill a SOCKADDR\_IN struct with address information
        SOCKADDR\_IN serverInfo;
        
        serverInfo.sin\_family = AF\_INET;
        
        // At this point, we've successfully retrieved vital information about the server,
        // including its hostname, aliases, and IP addresses.  Wait; how could a single
        // computer have multiple addresses, and exactly what is the following line doing?
        // See the explanation below.
        
        serverInfo.sin\_addr = \*((LPIN\_ADDR)\*hostEntry->h\_addr\_list);
        
        serverInfo.sin\_port = htons(80);		// Change to network-byte order and
        						// insert into port field
        
        
        // Connect to the server
        nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
        
        if (nret == SOCKET\_ERROR)
        {
        	nret = WSAGetLastError();
        	ReportError(nret, "connect()");
        
        	WSACleanup();
        	return NETWORK\_ERROR;
        }
        
        MessageBoxA(NULL, "Great, it is good", "socket Successful", MB\_OK);
        // Successfully connected!
        
        
        // Send/receive, then cleanup:
        
        N Offline
        N Offline
        norish
        wrote on last edited by
        #3

        'Connection refused' means that port is not active (no TCP server using the port number). 'Network is unreachable' might be meant some firewall had blocked the packet. Can you check the firewall settings?

        R 2 Replies Last reply
        0
        • G Garth J Lancaster

          not sure I fully understand your issue you've created a tcp socket client app 1) you try to connect to (local loopback address 127.0.0.1) on port 80 and get an error - do you have an ethernet adaptor, and tcp/ip set up on your machine ? - you should be able to ping yourself on 127.0.0.1 and get a valid response before you try this - then you'll need something listening on port 80 - depending on what sort of a machine, it could be IIS for example, or a server harness 2) you try a random port - unless you fix the issues at (1), its not going to work AND you would need something listening on all the ports on machine to catch the connect on a 'random' port - I dont see what a random port does for your case 'g'

          R Offline
          R Offline
          rahuljin
          wrote on last edited by
          #4

          thanks for the reply. i can ping 127.0.0.1 easily and getting a valid output.but i dont know how to ping using a port. i have 2 ethernet adaptors, one is used for internet and other is spare. my internet is working find, so port 80 is also working well ??

          G 1 Reply Last reply
          0
          • R rahuljin

            thanks for the reply. i can ping 127.0.0.1 easily and getting a valid output.but i dont know how to ping using a port. i have 2 ethernet adaptors, one is used for internet and other is spare. my internet is working find, so port 80 is also working well ??

            G Offline
            G Offline
            Garth J Lancaster
            wrote on last edited by
            #5

            rahuljin wrote:

            so port 80 is also working well ??

            not necessarily - you'll likely be connecting out to a web-server on port 80 on that server - unless you have something listening on your own machine on port 80 you'll get a connect fail 'g'

            R 1 Reply Last reply
            0
            • N norish

              'Connection refused' means that port is not active (no TCP server using the port number). 'Network is unreachable' might be meant some firewall had blocked the packet. Can you check the firewall settings?

              R Offline
              R Offline
              rahuljin
              wrote on last edited by
              #6

              i have set it as trusted application and granted all rights. i also have tried with firewall off, but the same result.

              1 Reply Last reply
              0
              • G Garth J Lancaster

                rahuljin wrote:

                so port 80 is also working well ??

                not necessarily - you'll likely be connecting out to a web-server on port 80 on that server - unless you have something listening on your own machine on port 80 you'll get a connect fail 'g'

                R Offline
                R Offline
                rahuljin
                wrote on last edited by
                #7

                can u tell me the changes i should make so that it can work ?

                L G 2 Replies Last reply
                0
                • R rahuljin

                  can u tell me the changes i should make so that it can work ?

                  L Offline
                  L Offline
                  led mike
                  wrote on last edited by
                  #8

                  rahuljin wrote:

                  can u tell me the changes i should make so that it can work ?

                  There are no changes to make in your code. I just ran it and it works as expected.

                  R 1 Reply Last reply
                  0
                  • L led mike

                    rahuljin wrote:

                    can u tell me the changes i should make so that it can work ?

                    There are no changes to make in your code. I just ran it and it works as expected.

                    R Offline
                    R Offline
                    rahuljin
                    wrote on last edited by
                    #9

                    which port are you using ? if i use a port which is used by other program like utorrent, it works only when i run utorrent, else dont. if i put some other port number, it shows the errors. i am using kaspersky internet security 2009 with windows 7rc and visual studio 2008. windows firewall is stopped. i also checked with KIS stopped, but no success. now what to do ?

                    1 Reply Last reply
                    0
                    • R rahuljin

                      can u tell me the changes i should make so that it can work ?

                      G Offline
                      G Offline
                      Garth J Lancaster
                      wrote on last edited by
                      #10

                      sorry, o/night here in Aus... as per Led Mike, there's nothing wrong with your code. Now you need to develop a server or grab some test code from someone's project to build one. TCP programming involves two parts - a client, which you've got, and server, listening on a port - you still havnt indicated that you have a server listening to/on a connection, so Im not sure what sorta results you expect. 'g'

                      R 1 Reply Last reply
                      0
                      • G Garth J Lancaster

                        sorry, o/night here in Aus... as per Led Mike, there's nothing wrong with your code. Now you need to develop a server or grab some test code from someone's project to build one. TCP programming involves two parts - a client, which you've got, and server, listening on a port - you still havnt indicated that you have a server listening to/on a connection, so Im not sure what sorta results you expect. 'g'

                        R Offline
                        R Offline
                        rahuljin
                        wrote on last edited by
                        #11

                        do u mean that i should write another program which listen to this port ?? also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?

                        G 1 Reply Last reply
                        0
                        • R rahuljin

                          do u mean that i should write another program which listen to this port ?? also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?

                          G Offline
                          G Offline
                          Garth J Lancaster
                          wrote on last edited by
                          #12

                          rahuljin wrote:

                          do u mean that i should write another program which listen to this port ??

                          yes - the whole point of connecting to something is something must be waiting/listening on the other end ! .. maybe you could use something like http://www.aprelium.com/abyssws/[^]

                          rahuljin wrote:

                          also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?

                          if you are in the connected state, you have a channel open and you can send to and receive from it - depending on whats on the other end - the simplest test of your code is whats known as an echo server - it reads what you send it and sends you back the same info - see here for an example - I dont get why you're asking about another port http://www.paulgriffiths.net/program/c/echoserv.php[^] and here http://www.csc.villanova.edu/~mdamian/Sockets/TcpSockets.htm[^] I hate to say this, but it sounds like you're a little out of your depth - this sort of material is covered in lots of network programming books, and there's plenty out there on the net Maybe you should spent some time doing a bit more research

                          R 1 Reply Last reply
                          0
                          • G Garth J Lancaster

                            rahuljin wrote:

                            do u mean that i should write another program which listen to this port ??

                            yes - the whole point of connecting to something is something must be waiting/listening on the other end ! .. maybe you could use something like http://www.aprelium.com/abyssws/[^]

                            rahuljin wrote:

                            also, if i want to send some information using the send() function, can i use the same port or i have to use another port for that ?

                            if you are in the connected state, you have a channel open and you can send to and receive from it - depending on whats on the other end - the simplest test of your code is whats known as an echo server - it reads what you send it and sends you back the same info - see here for an example - I dont get why you're asking about another port http://www.paulgriffiths.net/program/c/echoserv.php[^] and here http://www.csc.villanova.edu/~mdamian/Sockets/TcpSockets.htm[^] I hate to say this, but it sounds like you're a little out of your depth - this sort of material is covered in lots of network programming books, and there's plenty out there on the net Maybe you should spent some time doing a bit more research

                            R Offline
                            R Offline
                            rahuljin
                            wrote on last edited by
                            #13

                            thanks for the help. this is the first time i am writing something about tcp ip. i tried to get a book for tcp ip at my place but didn't find any. i will try to read online. thanks again.

                            1 Reply Last reply
                            0
                            • N norish

                              'Connection refused' means that port is not active (no TCP server using the port number). 'Network is unreachable' might be meant some firewall had blocked the packet. Can you check the firewall settings?

                              R Offline
                              R Offline
                              rahuljin
                              wrote on last edited by
                              #14

                              is there any way to give an address to bind() ?? i want that the program only except or recevice information from a specific address only. but when i try it gives an error - 10049. how should i resolve it ?

                              N 1 Reply Last reply
                              0
                              • R rahuljin

                                is there any way to give an address to bind() ?? i want that the program only except or recevice information from a specific address only. but when i try it gives an error - 10049. how should i resolve it ?

                                N Offline
                                N Offline
                                norish
                                wrote on last edited by
                                #15

                                Post your code to bind() here, pls. And also, the IP address of the pc that you executed the program.

                                R 1 Reply Last reply
                                0
                                • N norish

                                  Post your code to bind() here, pls. And also, the IP address of the pc that you executed the program.

                                  R Offline
                                  R Offline
                                  rahuljin
                                  wrote on last edited by
                                  #16

                                  here is the member funtion ----

                                  int getServer::getInfo()
                                  {
                                  ifstream ifile(path);
                                  if(ifile)
                                  {
                                  ifile.getline(sss, 80);
                                  sockVersion = MAKEWORD(1, 1);
                                  WSAStartup(sockVersion, &wsaData);
                                  listeningSocket = socket
                                  (
                                  AF_INET,
                                  SOCK_STREAM,
                                  IPPROTO_TCP
                                  );

                                      if (listeningSocket == INVALID\_SOCKET)
                                      {
                                          nret = WSAGetLastError();
                                          reportError(nret, "socket()");
                                          WSACleanup();				
                                          return NETWORK\_ERROR;
                                      }
                                  
                                      serverInfo.sin\_family = AF\_INET;
                                      serverInfo.sin\_addr.s\_addr = inet\_addr(sss);
                                      serverInfo.sin\_port = htons(23571);	
                                  
                                      nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
                                      
                                      if (nret == SOCKET\_ERROR)
                                      {
                                          nret = WSAGetLastError();
                                          reportError(nret, "bind()");
                                          WSACleanup();
                                          return NETWORK\_ERROR;
                                      }
                                  
                                      nret = listen(listeningSocket, 10);	
                                  
                                      if (nret == SOCKET\_ERROR)
                                      {
                                          nret = WSAGetLastError();
                                          reportError(nret, "listen()");
                                          WSACleanup();
                                          return NETWORK\_ERROR;
                                      }
                                  
                                      theClient = accept
                                          (
                                          listeningSocket,
                                          NULL,
                                          NULL
                                          );
                                      
                                      if (theClient == INVALID\_SOCKET)
                                      {
                                          nret = WSAGetLastError();
                                          reportError(nret, "accept()");
                                          WSACleanup();
                                          return NETWORK\_ERROR;
                                      }
                                      
                                      byteRece = recv(theClient, st, 100, 0);
                                      
                                      if (byteRece == SOCKET\_ERROR)
                                      {
                                          nret = WSAGetLastError();
                                          reportError(nret, "send()");
                                          WSACleanup();
                                          return NETWORK\_ERROR;
                                      }
                                      
                                      MessageBoxA(NULL, st, "Server Status", MB\_OK | MB\_ICONEXCLAMATION);
                                      closesocket(theClient);
                                      closesocket(listeningSocket);
                                      
                                      WSACleanup();
                                      return NETWORK\_OK;
                                  }
                                  

                                  in ip.txt, if i put the ip --- 127.0.0.1 for same pc, it works or if i set the instruction ---

                                  serverInfo.sin_addr.s_addr = INADDR_ANY;

                                  if i put an ip address of other pc in the network, then bind() shows an error -- 10049. the ip is like 192.168.250.201.

                                  N 1 Reply Last reply
                                  0
                                  • R rahuljin

                                    here is the member funtion ----

                                    int getServer::getInfo()
                                    {
                                    ifstream ifile(path);
                                    if(ifile)
                                    {
                                    ifile.getline(sss, 80);
                                    sockVersion = MAKEWORD(1, 1);
                                    WSAStartup(sockVersion, &wsaData);
                                    listeningSocket = socket
                                    (
                                    AF_INET,
                                    SOCK_STREAM,
                                    IPPROTO_TCP
                                    );

                                        if (listeningSocket == INVALID\_SOCKET)
                                        {
                                            nret = WSAGetLastError();
                                            reportError(nret, "socket()");
                                            WSACleanup();				
                                            return NETWORK\_ERROR;
                                        }
                                    
                                        serverInfo.sin\_family = AF\_INET;
                                        serverInfo.sin\_addr.s\_addr = inet\_addr(sss);
                                        serverInfo.sin\_port = htons(23571);	
                                    
                                        nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
                                        
                                        if (nret == SOCKET\_ERROR)
                                        {
                                            nret = WSAGetLastError();
                                            reportError(nret, "bind()");
                                            WSACleanup();
                                            return NETWORK\_ERROR;
                                        }
                                    
                                        nret = listen(listeningSocket, 10);	
                                    
                                        if (nret == SOCKET\_ERROR)
                                        {
                                            nret = WSAGetLastError();
                                            reportError(nret, "listen()");
                                            WSACleanup();
                                            return NETWORK\_ERROR;
                                        }
                                    
                                        theClient = accept
                                            (
                                            listeningSocket,
                                            NULL,
                                            NULL
                                            );
                                        
                                        if (theClient == INVALID\_SOCKET)
                                        {
                                            nret = WSAGetLastError();
                                            reportError(nret, "accept()");
                                            WSACleanup();
                                            return NETWORK\_ERROR;
                                        }
                                        
                                        byteRece = recv(theClient, st, 100, 0);
                                        
                                        if (byteRece == SOCKET\_ERROR)
                                        {
                                            nret = WSAGetLastError();
                                            reportError(nret, "send()");
                                            WSACleanup();
                                            return NETWORK\_ERROR;
                                        }
                                        
                                        MessageBoxA(NULL, st, "Server Status", MB\_OK | MB\_ICONEXCLAMATION);
                                        closesocket(theClient);
                                        closesocket(listeningSocket);
                                        
                                        WSACleanup();
                                        return NETWORK\_OK;
                                    }
                                    

                                    in ip.txt, if i put the ip --- 127.0.0.1 for same pc, it works or if i set the instruction ---

                                    serverInfo.sin_addr.s_addr = INADDR_ANY;

                                    if i put an ip address of other pc in the network, then bind() shows an error -- 10049. the ip is like 192.168.250.201.

                                    N Offline
                                    N Offline
                                    norish
                                    wrote on last edited by
                                    #17
                                    serverInfo.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
                                    

                                    ...
                                    nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

                                    It is common sennse always succeeds above. But,

                                    serverInfo.sin\_addr.s\_addr = inet\_addr("192.168.250.201");
                                    

                                    ...
                                    nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

                                    will succeeds only when this server program is executed on the pc which has IP address '192.168.250.201' exactly. Note that bind() address is not client address but server address. So you cannot choose client by bind() and you should check accept()'ed client address by means. Another situation, you have some local IP address and global IP address 10.0.0.1 by PPPOE, you may not wait for connection on 10.0.0.1 usually, because 10.0.0.1 address is owned by a router which connecting another network. Then you should wait on your local IP address and make change the router settings, for example DMZ or static routing, which 10.0.0.1 in-bound packets can route to your local IP address. In this case, in fact, it is not a programming issue but the network setting issue.

                                    R 1 Reply Last reply
                                    0
                                    • N norish
                                      serverInfo.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
                                      

                                      ...
                                      nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

                                      It is common sennse always succeeds above. But,

                                      serverInfo.sin\_addr.s\_addr = inet\_addr("192.168.250.201");
                                      

                                      ...
                                      nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

                                      will succeeds only when this server program is executed on the pc which has IP address '192.168.250.201' exactly. Note that bind() address is not client address but server address. So you cannot choose client by bind() and you should check accept()'ed client address by means. Another situation, you have some local IP address and global IP address 10.0.0.1 by PPPOE, you may not wait for connection on 10.0.0.1 usually, because 10.0.0.1 address is owned by a router which connecting another network. Then you should wait on your local IP address and make change the router settings, for example DMZ or static routing, which 10.0.0.1 in-bound packets can route to your local IP address. In this case, in fact, it is not a programming issue but the network setting issue.

                                      R Offline
                                      R Offline
                                      rahuljin
                                      wrote on last edited by
                                      #18

                                      thanks. can u tell me how to set the accept() function for getting the ip address of client ? i tried but no result.

                                      N 1 Reply Last reply
                                      0
                                      • R rahuljin

                                        thanks. can u tell me how to set the accept() function for getting the ip address of client ? i tried but no result.

                                        N Offline
                                        N Offline
                                        norish
                                        wrote on last edited by
                                        #19

                                        sockaddr_in clientAddress;
                                        int clientAddressLen = sizeof(sockaddr_in);
                                        SOCKET clientSocket= accept(linteningSocket, (sockaddr*)&clientAddress, &clientAddressLen);
                                        if (INVALID_SOCKET != clientSocket) {
                                        // client succeeded to connect me
                                        printf("client from %s\n", inet_ntoa(clientAddress.sin_addr);
                                        // start conversation to clientSocket
                                        ....
                                        }

                                        What do your program codes return above accept? No result yet?

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups