Java Server and C++ Client

Discussion in 'C++' started by cometahalley, Oct 7, 2010.

  1. cometahalley

    cometahalley New Member

    Joined:
    Oct 7, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi everyone.

    I have some experience on Matlab, Java and C++, but for the type of programming I'm trying to do here, I'm basically a beginner.

    Eventually, the main idea of this Server/Client is that the client will ask the server for time, receive the time, read the string and sync its time to the server, assuming there is a time drift.

    As of right now I have the server in Java and a client in Java that work. They are not syncing yet, but the client makes a request to the server, which sends a string with the date and time and the client prints that string. We don't need the time sync part quite yet. More importantly is to create a client on C++ that does the same that the one on Java does (I'll post both codes soon) and works with the Java server.

    On the C++ side, I got a simple client/Server program and I'm trying to change the client enough so that it works with the Java server.

    My 1st problem is that because this C++ client was written a while ago, the Visual C++ 2010 is saying that the project is out of date. I tried reading through forums for help with that and each was very specific to each case. I tried a few things suggested, but still no luck.

    Below I'll post the C++ client and later I'll post the Java server/client.

    Anyone interested in helping me out with this? It'd be much appreciated, I've done stuff on C++ but nothing like network sockets, so I'm a little confused.
     
  2. cometahalley

    cometahalley New Member

    Joined:
    Oct 7, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Here's the C++ client I have:
    -----------------------------
    Code:
    #include <winsock2.h>
    #include <windows.h>
    #include <fcntl.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <stdio.h>
    #include <conio.h>
    
    int main(int argv, char** argc){
    
    int host_port= 1101;
    char* host_name="127.0.0.1";
    
    //Initialize socket support WINDOWS ONLY!
    unsigned short wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 ||
    HIBYTE( wsaData.wVersion ) != 2 )) {
    fprintf(stderr, "Could not find useable sock dll %d\n",WSAGetLastError());
    goto FINISH;
    }
    
    
    int hsock;
    int * p_int ;
    hsock = socket(AF_INET, SOCK_STREAM, 0);
    if(hsock == -1){
    printf("Error initializing socket %d\n",WSAGetLastError());
    goto FINISH;
    }
    
    p_int = (int*)malloc(sizeof(int));
    *p_int = 1;
    if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
    (setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
    printf("Error setting options %d\n", WSAGetLastError());
    free(p_int);
    goto FINISH;
    }
    free(p_int);
    
    struct sockaddr_in my_addr;
    
    my_addr.sin_family = AF_INET ;
    my_addr.sin_port = htons(host_port);
    
    memset(&(my_addr.sin_zero), 0, 8);
    my_addr.sin_addr.s_addr = inet_addr(host_name);
    
    
    if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
    fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError());
    goto FINISH;
    }
    
    
    char buffer[1024];
    int buffer_len = 1024;
    int bytecount;
    
    int c;
    memset(buffer, '\0', buffer_len);
    
    for(char* p=buffer ; (c=getch())!=13 ; p++){
    printf("%c", c);
    *p = c;
    }
    
    if( (bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){
    fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
    goto FINISH;
    }
    printf("Sent bytes %d\n", bytecount);
    
    if((bytecount = recv(hsock, buffer, buffer_len, 0))==SOCKET_ERROR){
    fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
    goto FINISH;
    }
    printf("Recieved bytes %d\nReceived string \"%s\"\n", bytecount, buffer);
    
    closesocket(hsock);
    
    FINISH:
    ;
    }
     
    Last edited by a moderator: Oct 7, 2010
  3. cometahalley

    cometahalley New Member

    Joined:
    Oct 7, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Wow, no replies, hope people start looking at this thread

    Anyway, the first thing I did was to change the host port since the Java application was using the port "1234", so I changed it to that.

    Next, I added a piece of code to find the computer's IP address and use that instead of the internal IP (127.0.0.1), because the Java client (will run parallel to the C++ client) has at the beginning a part where it looks for the IP.

    ----------------------

    The problem is still the same though, because this code is oldish, when I try to run it on VC++ 2010, it says that the code is out of date and I can't figure out how to get around that, so I can't check to see if the alterations work. Any ideas on how I can get rid of this msg and run the code? I tried suggestions from people that had the same problem, but none of them worked, I think it's a problem more particular to this code itself. Any suggestions?

    thank you
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What's the exact error you get from VS2010?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice