user2108742
user2108742

Reputation: 117

Sending a message via TCP Socket between C++ and Java

Sorry for my poor english.

I succeeded in developing a C++ Server Socket that wait for a Java Socket to connect. I first launch my C++ Server wich write "Waiting for a connection...", then a launch my Java Socket and I can see on my C++ serveur "Client connected!" so it works. But then I want to send a message from the C++ server to the Java client. I receive nothing from the server. Here is my C++ server and Java client codes:

C++ Server:

#include "stdafx.h"

#ifdef _WIN32
#include <Winsock2.h>
#define SOCKET_ERRNO    WSAGetLastError()
#define ERRNO       GetLastError()
#else
#define SOCKET_ERRNO    errno
#define ERRNO       errno
#define closesocket close
#endif
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <conio.h>
#include <errno.h>

int _tmain(int argc, _TCHAR* argv[])
{

    SOCKET hSocket, hAccept;
    struct sockaddr_in addr;
    int len = sizeof(addr);    
    int nPort = 2009;

    // Initialize winsock
    WSADATA stack_info ;
    WSAStartup(MAKEWORD(2,0), &stack_info) ;

    //Create socket
    hSocket = socket( PF_INET, SOCK_STREAM, 0 );
    if( hSocket == INVALID_SOCKET )
    {
        printf( "socket() error %d\n", SOCKET_ERRNO );
        getchar();
        exit(1);
    }

    //Listen to the socket
    addr.sin_family = AF_INET ;
    addr.sin_addr.s_addr = htonl (INADDR_ANY);
    addr.sin_port = htons ((unsigned short)nPort );
    if ( bind( hSocket, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR )
    {
        printf("bind() error\n");
        getchar();
        exit(1);
    }

    if ( listen( hSocket, 100) == SOCKET_ERROR )
    {
        printf("listen() error\n");
        getchar();
        exit(1);
    }

    printf("Waiting for a connection...");

    //Waiting for a client to connect
    hAccept = accept(hSocket, NULL, NULL);

    printf("\nClient connected!");

    //Sending a message
    char Buffer[1024];
    sprintf_s( Buffer, "hello" );
    send( hAccept, Buffer, 8, 0 );
    printf("Sent!");

    getchar();

    return 0;
}

Java Client:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {

    public static void main(String[] args) {

        Socket socket;
        BufferedReader in;
        PrintWriter out;

        try {
            socket = new Socket(InetAddress.getLocalHost(),2009);

            in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
            String message_distant = in.readLine();
            System.out.println(message_distant);

            socket.close();

        }catch (UnknownHostException e) {

            e.printStackTrace();
        }catch (IOException e) {

            e.printStackTrace();
        }
    }

}

Does anybody knows why I don't see the message I send from C++ Server to Java client?

Thanks

Upvotes: 1

Views: 1990

Answers (1)

suhe_arie
suhe_arie

Reputation: 352

Your Java client reads the data from the socket using in.readLine(); which basically reads the data until it finds a line separator or carriage return character. I am not so familiar with C++, but it seems that your C++ server only sends "hello" without the line separator. This makes the Java client waits forever. To fix this problem, you can send "hello\n" or "hello\r\n" from your C++ server.

Upvotes: 3

Related Questions