Question

So I wrote a client/server program where the client is written in C++ and the server is written in Java. When i try to send data to the java side, the send() function fails and returns -1. I have no idea what the problem is.

Heres the Client side:

    int main(){

    string address = "127.0.0.1";
    sockaddr_in socketInfo;//declare struct object
    SOCKET socketClient;
    WSAData network;
    int errorNum;//networking functions return integer values that represent the success of the call to the function
    errorNum = WSAStartup(MAKEWORD(2, 2), &network);
    if (errorNum == 0){
        socketInfo.sin_family = AF_INET;//set family to ipv4
        socketInfo.sin_port = htons(6897);//set port to connect to and convert to network byte order htons
        errorNum = inet_pton(socketInfo.sin_family, "127.0.0.1", &socketInfo.sin_addr);
        socketClient = socket(socketInfo.sin_family, SOCK_STREAM, IPPROTO_TCP);//creates a socket
        if (socketClient != INVALID_SOCKET){
            errorNum = connect(socketClient, (sockaddr*)&socketInfo, sizeof(socketInfo));
            if (errorNum != SOCKET_ERROR){
                cout << "Connected to Java Application!" << endl;
                int num = 152;
                cout << "Trying to send..." << endl;
                const char* buff = (const char*)(num);
                errorNum = send(socketClient, buff, sizeof(buff), 0);

                if (errorNum != SOCKET_ERROR)
                    cout << "sent with success " << errorNum << endl;
                else
                    cout << "Failed to send " << errorNum << endl;
                closesocket(socketClient);
            }
            else{
                cout << "Failed to connect" << endl;
                closesocket(socketClient);
            }
        }
        else
            cout << "Socket creation failed!" << endl;
    }//end if
    else
        cout << "WSA startup failed" << endl;

    WSACleanup();

    //thread t1(getStroke, VK_SPACE);
    //thread t2(getStroke, VK_ESCAPE);

    //t1.join();
    //t2.join(); 

    system("pause");
    return 0;
}

Heres the Server side:

    try {
        server = new ServerSocket(6897);
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    new Thread(new Runnable() {
        public void run() {
            try {
                System.out.println("Waiting on C++ Application...");
                connection = server.accept();
                System.out.println("Connected to C++ Application!");
                setupStreams();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        private void setupStreams() {
            // TODO Auto-generated method stub
            try {
                input = new DataInputStream(connection.getInputStream());
                int data = 0;
                System.out.println("Waiting for data...");
                data = input.read();
                System.out.println("Data recieved " + data);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }).start();
Was it helpful?

Solution

You are using invalid cast in send() function.

int num = 152; cout << "Trying to send..." << endl; const char* buff = (const char*)(num);

In above code, you have to use & for num, like below code,

const char* buff = (const char*)(&num);

And, also for 'endianness', using htonl() is much better. So, I think this code will be better

cout << "Trying to send..." << endl;

int num = 152;
int net_num = htonl(num);
send(sock, (const char*)&net_num, sizeof(int), 0);

OTHER TIPS

first check the IPv4 Adress(using ipconfig in CMD)and you should do while loop in Thread to check every time the received Data.

private void setupStreams() {
            // TODO Auto-generated method stub
           while (!Thread.currentThread().isInterrupted())// you have to add this line
            try {
                input = new DataInputStream(connection.getInputStream());
                String data = ""; //use string is better than int
                System.out.println("Waiting for data...");
                data = input.readUTF();// readUTF for string types
                System.out.println("Data recieved " + data);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

and don't forget to send data as string from client

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top