CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    4

    Java Server and C++ Client

    Hi everyone.

    I have some experience on Matlab, Java and C++, but fot 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.

    Thanks ahead of time.

  2. #2
    Join Date
    Oct 2010
    Posts
    4

    Re: Java Server and C++ Client

    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 &#37;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 cometahalley; November 15th, 2010 at 12:10 PM.

  3. #3
    Join Date
    Oct 2010
    Posts
    4

    Re: Java Server and C++ Client

    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. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Java Server and C++ Client

    Please post the exact error code from VS2010. I don't know of any errors resembling "out of date".

    Also, you should surround your code with [code][/code] tags so that it is easier to read.

  5. #5
    Join Date
    Oct 2010
    Posts
    4

    Re: Java Server and C++ Client

    Quote Originally Posted by hoxsiew View Post
    Please post the exact error code from VS2010. I don't know of any errors resembling "out of date".

    Also, you should surround your code with [code][/code] tags so that it is easier to read.
    sorry about the code block.

    when I try to run (debug) the code, a window pops up saying that the project is out of date and it gives me the option to "build it," but when I try that option, it builds, but then it says that there were errors during the build and asks if I want to run the last successful build, which, if I do, also comes with an error saying that it was unable to start the program.

    I'm new to VC++, some people told me to use dev C++, do you think that's a better option to VC++?

    thank you

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Java Server and C++ Client

    Quote Originally Posted by cometahalley View Post
    sorry about the code block.

    when I try to run (debug) the code, a window pops up saying that the project is out of date and it gives me the option to "build it," but when I try that option, it builds, but then it says that there were errors during the build and asks if I want to run the last successful build, which, if I do, also comes with an error saying that it was unable to start the program.

    I'm new to VC++, some people told me to use dev C++, do you think that's a better option to VC++?

    thank you
    There were build errors, and no EXE was created. If no exe was ever created, then you cannot run the last successful build (as there never was one).
    Fix the errors, and rebuild.

    Viggy

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured