Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Java Server and C++ Client (http://www.go4expert.com/forums/java-server-cpp-client-t23504/)

cometahalley 7Oct2010 08:14

Java Server and C++ Client
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.

cometahalley 7Oct2010 08:14

Re: Java Server and C++ Client
Here's the C++ client I have:

#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="";

//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());
goto FINISH;

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



cometahalley 30Oct2010 05:12

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

xpi0t0s 31Oct2010 04:45

Re: Java Server and C++ Client
What's the exact error you get from VS2010?

All times are GMT +5.5. The time now is 18:18.