0

I wrote a little client-server application that connects c++ server with java client. Now when I'm sending a string from my server to client everything works normally, but when I send a string from client to server I get this output:

Hello╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠└Ű=Ď

This is my C++ server code:

int main()
{
    long SUCCESSFUL;
    WSAData WinSockData;
    WORD DLLVERSION;

    DLLVERSION = MAKEWORD(2, 1);
    SUCCESSFUL = WSAStartup(DLLVERSION, &WinSockData);

    SOCKADDR_IN ADDRESS;
    int AddressSize = sizeof(ADDRESS);

    SOCKET sock_LISTEN;
    SOCKET sock_CONNECTION;

    sock_CONNECTION = socket(AF_INET, SOCK_STREAM, NULL);
    ADDRESS.sin_addr.s_addr = inet_addr("127.0.0.1");
    ADDRESS.sin_family = AF_INET;
    ADDRESS.sin_port = htons(8085);

    sock_LISTEN = socket(AF_INET, SOCK_STREAM, NULL);   
    bind(sock_LISTEN, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS));
    listen(sock_LISTEN, SOMAXCONN);

    char msg[200];
    string MSG;

    for (;;)
    {
        cout << "\n\tSERVER: Waiting for incoming connections...";

        if (sock_CONNECTION = accept(sock_LISTEN, (SOCKADDR*)&ADDRESS, &AddressSize))
        {
            cout << "\n\tA connection was found!" << endl;
            SUCCESSFUL = recv(sock_CONNECTION, msg, sizeof(msg), NULL);
            MSG = msg;
            cout << MSG << endl;
            closesocket(sock_CONNECTION);
        }
    }
}

And this is my Java client code:

public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 8085);
            Scanner scanner = new Scanner(socket.getInputStream());
            PrintStream printStream = new PrintStream(socket.getOutputStream());
            printStream.println("Hello");
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
Duško Mirković
  • 199
  • 1
  • 10
  • 1
    Seems like you simply miss to *terminate* the string on the receiveing side. You *do* remember that `char` strings in C are really called ***null-terminated** byte strings*? (And that the term "null" here doesn't mean the null pointer symbolic constant `NULL`, but the character `'\0'`) – Some programmer dude Oct 11 '17 at 12:43
  • Oh yes I forgot about that, thank you so much. – Duško Mirković Oct 11 '17 at 12:56

0 Answers0