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 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