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. Problem with Socket

Problem with Socket

Scheduled Pinned Locked Moved C / C++ / MFC
c++javasysadminhelpquestion
9 Posts 5 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.
  • A Offline
    A Offline
    Abhijit D Babar
    wrote on last edited by
    #1

    Hello, I am doing socket programming in VC++. I have two applications for Server and Client(in VC++). Message can be send/receive with these two applications from server to client or vice-versa. I download one HL7 Toolkit. I want to communicate this Toolkit with my applications. They did socket programming, but in Java. I want to know, can we send data if sender side socket is created in VC++ and on receiver side socket created in Java. Is socket programming is platform independent? Thanks in advance

    D K P 3 Replies Last reply
    0
    • A Abhijit D Babar

      Hello, I am doing socket programming in VC++. I have two applications for Server and Client(in VC++). Message can be send/receive with these two applications from server to client or vice-versa. I download one HL7 Toolkit. I want to communicate this Toolkit with my applications. They did socket programming, but in Java. I want to know, can we send data if sender side socket is created in VC++ and on receiver side socket created in Java. Is socket programming is platform independent? Thanks in advance

      D Offline
      D Offline
      Divyang Mithaiwala
      wrote on last edited by
      #2

      Yes socket programming is platform Independent.


      Do not trust a computer... Always check what computer is doing regards, Divyang Mithaiwala Software Engineer

      1 Reply Last reply
      0
      • A Abhijit D Babar

        Hello, I am doing socket programming in VC++. I have two applications for Server and Client(in VC++). Message can be send/receive with these two applications from server to client or vice-versa. I download one HL7 Toolkit. I want to communicate this Toolkit with my applications. They did socket programming, but in Java. I want to know, can we send data if sender side socket is created in VC++ and on receiver side socket created in Java. Is socket programming is platform independent? Thanks in advance

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        Socket programming is really platform independent, but watch out for bit order. (LSB and MSB) Consider it as posting bits through the cable. :-O

        Press F1 for help or google it. Greetings from Germany

        1 Reply Last reply
        0
        • A Abhijit D Babar

          Hello, I am doing socket programming in VC++. I have two applications for Server and Client(in VC++). Message can be send/receive with these two applications from server to client or vice-versa. I download one HL7 Toolkit. I want to communicate this Toolkit with my applications. They did socket programming, but in Java. I want to know, can we send data if sender side socket is created in VC++ and on receiver side socket created in Java. Is socket programming is platform independent? Thanks in advance

          P Offline
          P Offline
          ParagPatel
          wrote on last edited by
          #4

          Yes, it is platform independent, but you need to take care below points when going across different platform (in terms of processer) 1) Before send data, convert your data in to network type. (ex. using "htonl" function.) 2) if you passing whole structure as a binary data then need to take care about padding. (use __attribute__((__packed__)))

          Parag Patel Sr. Software Eng, Varaha Systems

          A 1 Reply Last reply
          0
          • P ParagPatel

            Yes, it is platform independent, but you need to take care below points when going across different platform (in terms of processer) 1) Before send data, convert your data in to network type. (ex. using "htonl" function.) 2) if you passing whole structure as a binary data then need to take care about padding. (use __attribute__((__packed__)))

            Parag Patel Sr. Software Eng, Varaha Systems

            A Offline
            A Offline
            Abhijit D Babar
            wrote on last edited by
            #5

            thanks. But now i can't send or receive the data from Toolkit which i download. My application is in VC++ and Toolkit is in Java. Is there any function i have to add in my application.

            T P 2 Replies Last reply
            0
            • A Abhijit D Babar

              thanks. But now i can't send or receive the data from Toolkit which i download. My application is in VC++ and Toolkit is in Java. Is there any function i have to add in my application.

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              Abhijit D. Babar wrote:

              But now i can't send or receive the data from Toolkit which i download. My application is in VC++ and Toolkit is in Java. Is there any function i have to add in my application.

              is your toolkit is Socket server or socketclient. Are you connecting on right socket port?

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • A Abhijit D Babar

                thanks. But now i can't send or receive the data from Toolkit which i download. My application is in VC++ and Toolkit is in Java. Is there any function i have to add in my application.

                P Offline
                P Offline
                ParagPatel
                wrote on last edited by
                #7

                After running your application at both side (server/client) check that your application able to bind on that port or not? You can use "netstat" command in window.

                Parag Patel Sr. Software Eng, Varaha Systems

                A 1 Reply Last reply
                0
                • P ParagPatel

                  After running your application at both side (server/client) check that your application able to bind on that port or not? You can use "netstat" command in window.

                  Parag Patel Sr. Software Eng, Varaha Systems

                  A Offline
                  A Offline
                  Abhijit D Babar
                  wrote on last edited by
                  #8

                  I use netstat and Telnet also and it bind the socket. But when i try to run my client program, it will wait or stop on "connect" function. I place the client code below, please see, where is the problem. /// Client code //// CString str; int sock,iResult,iOptVal,iSenderAddrSize; int BUFF_SIZE = 30,iError; char recv_data[30]; char* localIP = ""; struct sockaddr_in server_addr,SenderAddr; struct hostent * lpHostEntry; WSADATA wsaData; iSenderAddrSize = sizeof(SenderAddr); iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) AfxMessageBox(_T("Error at WSAStartup()\n")); if ((sock = socket(AF_INET, SOCK_STREAM,0)) == -1) AfxMessageBox(_T("Error at Socket")); gethostname(localIP,56); lpHostEntry = gethostbyname(localIP); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(2050); server_addr.sin_addr.s_addr = inet_addr("192.168.1.9"); iError = connect( sock, (SOCKADDR*) &server_addr, sizeof(server_addr) ); if(iError == SOCKET_ERROR){ iError = WSAGetLastError(); CString s1; s1.Format(_T("Connect Error = %d"),iError); AfxMessageBox(s1); exit(1); } while(1){ memset(recv_data,0,sizeof(recv_data)); int iResult = recv(sock,recv_data,BUFF_SIZE,0) ; if(iResult > 0) { recv_data[BUFF_SIZE] = '\0'; str = inet_ntoa(SenderAddr.sin_addr); AfxMessageBox(str); } else{ iError = WSAGetLastError(); } } //////// When i run my Server program, it will stop or wait on "accept" function. //// Server code ///// int sock, connected, bytes_recieved ; char send_data [1024] , recv_data[1024]; CString str,s; struct sockaddr_in server_addr,client_addr; int sin_size; int iResult; WSADATA wsaData; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) AfxMessageBox(_T("Error at WSAStartup()\n")); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { AfxMessageBox(_T("Socket")); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(2050);

                  P 1 Reply Last reply
                  0
                  • A Abhijit D Babar

                    I use netstat and Telnet also and it bind the socket. But when i try to run my client program, it will wait or stop on "connect" function. I place the client code below, please see, where is the problem. /// Client code //// CString str; int sock,iResult,iOptVal,iSenderAddrSize; int BUFF_SIZE = 30,iError; char recv_data[30]; char* localIP = ""; struct sockaddr_in server_addr,SenderAddr; struct hostent * lpHostEntry; WSADATA wsaData; iSenderAddrSize = sizeof(SenderAddr); iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) AfxMessageBox(_T("Error at WSAStartup()\n")); if ((sock = socket(AF_INET, SOCK_STREAM,0)) == -1) AfxMessageBox(_T("Error at Socket")); gethostname(localIP,56); lpHostEntry = gethostbyname(localIP); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(2050); server_addr.sin_addr.s_addr = inet_addr("192.168.1.9"); iError = connect( sock, (SOCKADDR*) &server_addr, sizeof(server_addr) ); if(iError == SOCKET_ERROR){ iError = WSAGetLastError(); CString s1; s1.Format(_T("Connect Error = %d"),iError); AfxMessageBox(s1); exit(1); } while(1){ memset(recv_data,0,sizeof(recv_data)); int iResult = recv(sock,recv_data,BUFF_SIZE,0) ; if(iResult > 0) { recv_data[BUFF_SIZE] = '\0'; str = inet_ntoa(SenderAddr.sin_addr); AfxMessageBox(str); } else{ iError = WSAGetLastError(); } } //////// When i run my Server program, it will stop or wait on "accept" function. //// Server code ///// int sock, connected, bytes_recieved ; char send_data [1024] , recv_data[1024]; CString str,s; struct sockaddr_in server_addr,client_addr; int sin_size; int iResult; WSADATA wsaData; iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != NO_ERROR) AfxMessageBox(_T("Error at WSAStartup()\n")); if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { AfxMessageBox(_T("Socket")); exit(1); } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(2050);

                    P Offline
                    P Offline
                    ParagPatel
                    wrote on last edited by
                    #9

                    Hey man, code is working, i have tested it. I think something else in your application which is causing issue. Just cross check below basic things 1. Message box not going background, and thats why you are feeling that program is stuck. 2. You have given right IP at client side.

                    Parag Patel Sr. Software Eng, Varaha Systems

                    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