C ++客户端无法与Java服务器通信

时间:2015-03-11 11:22:47

标签: java c++

我用Java编写的客户端能够用Java与服务器通信。 C ++客户端不使用用java编写的服务器。有人可以帮我解决这个问题。我提供了所有3个代码(Java服务器,Java客户端,C ++客户端)

C ++客户端:

#include <iostream>
#include <conio.h>
#include <winsock2.h>
#include<string>



int main(int argc, char* argv[])
{
   WSAData version;        //We need to check the version.
    WORD mkword=MAKEWORD(2,2);
    int what=WSAStartup(mkword,&version);
    if(what!=0){
    std::cout<<"This version is not supported! - \n"<<WSAGetLastError()<<std::endl;
    }
    else{
    std::cout<<"Good - Everything fine!\n"<<std::endl;
    }

    SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(u_sock==INVALID_SOCKET)
    std::cout<<"Creating socket fail\n";

    else
    std::cout<<"It was okay to create the socket\n";

    //Socket address information
    sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=inet_addr("192.168.45.88");
    addr.sin_port=htons(15000);
    std::cout<<"Successfully provided the address"<<std::endl;    
    /*==========Addressing finished==========*/

    //Now we connect
    int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
    std::cout<<"conn value:"<<conn<<std::endl;
    if(conn==SOCKET_ERROR){
    std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
    closesocket(u_sock);
    WSACleanup();
    }
     std::cout<<"Successfully connected to server"<<std::endl;

     //Send some message to remote host
    char* mymsg="Hello Server...How are you?";
    char vect[512]={0};

    int smsg=send(u_sock,mymsg,sizeof(mymsg),0);
    if(smsg==SOCKET_ERROR){
    std::cout<<"Error: "<<WSAGetLastError()<<std::endl;
    WSACleanup();
    }

    int get=recv(u_sock,vect,512,0);
    if(get==SOCKET_ERROR){
    std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
    }
    std::cout<<vect<<std::endl;
    closesocket(u_sock);

    getch();

    return 0;

}

Java Server:

import java.net.*;

import java.io.*;



public class EchoServer {

    public static void main(String[] args) throws IOException {



       /* if (args.length != 1) {

        System.err.println("Usage: java EchoServer <port number>");

        System.exit(1);

    }*/

    int portNumber = 15000;

    try (

        ServerSocket serverSocket =  new ServerSocket(portNumber);

        Socket clientSocket = serverSocket.accept();     

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);                   

        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    ) {

        String inputLine;

        while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
        out.println(inputLine);

        }

    } catch (IOException e) {

        System.out.println("Exception caught when trying to listen on port "

        + portNumber + " or listening for a connection");

        System.out.println(e.getMessage());

    }

    }

}

Java客户端:

import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {

    if (args.length != 0) {
        System.err.println(
        "Usage: java EchoClient <host name> <port number>");
        System.exit(1);
    }

    String hostName = "192.168.45.88";
    int portNumber = 15000;

    try (
        Socket echoSocket = new Socket(hostName, portNumber);
        PrintWriter out =
        new PrintWriter(echoSocket.getOutputStream(), true);
        BufferedReader in =
        new BufferedReader(
            new InputStreamReader(echoSocket.getInputStream()));
        BufferedReader stdIn =
        new BufferedReader(
            new InputStreamReader(System.in))
    ) {
        String userInput;
        while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
        }
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host " + hostName);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to " +
        hostName);
        System.exit(1);
    }
    }
}

2 个答案:

答案 0 :(得分:1)

少数事情:

1)当使用winsock套接字发送数据时,sizeof(mymsg)是错误的,它应该是strlen(mymsg)+1

2)我之前编写过Java-Server-C ++ -Client,主要是处理ASCII与UNICODE。在Java端接收,确保它从ASCII字符构建字符串作为编码。另一方面,当向winsock服务器发送数据时 - 将其作为二进制数据发送,否则winsock服务器无法完成recv()函数。

答案 1 :(得分:0)

好的。我会为此付出一些努力。有点令人耳目一新的事情。 ^^ 我修改了你的代码,因此使用了消息长度模式。 c客户端向服务器发送消息,java客户端对其进行解码并将其发送回客户端。因此,您既可以编码也可以运行编码方向。请尝试理解代码中发生的情况,否则您将再次遇到问题。 ByteOrder Stuff在java端完成,所以你可以摆弄你的封闭式C服务器。

C客户:

int main(int argc, char* argv[])
{

    WSAData version;        //We need to check the version.
    WORD mkword=MAKEWORD(2,2);
    int what=WSAStartup(mkword,&version);
    if(what!=0){
    std::cout<<"This version is not supported! - \n"<<WSAGetLastError()<<std::endl;
    }
    else{
    std::cout<<"Good - Everything fine!\n"<<std::endl;
    }

    SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(u_sock==INVALID_SOCKET)
    std::cout<<"Creating socket fail\n";

    else
    std::cout<<"It was okay to create the socket\n";

    //Socket address information
    sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    addr.sin_port=htons(15000);
    std::cout<<"Successfully provided the address"<<std::endl;    
    /*==========Addressing finished==========*/

    //Now we connect
    int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
    std::cout<<"conn value:"<<conn<<std::endl;
    if(conn==SOCKET_ERROR){
    std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
    closesocket(u_sock);
    WSACleanup();
    }
     std::cout<<"Successfully connected to server"<<std::endl;

     //Send some message to remote host
    char* mymsg="Hello Server...How are you?";
    int length = strlen(mymsg);
    //Cast the integer to char and send it
    int smsg=send(u_sock,reinterpret_cast<char*>(&length), sizeof(int),0);
    //Send the actual message
    smsg=send(u_sock,mymsg,strlen(mymsg),0);


    int newlength;
    //Receive exactly 4 bytes for the length. If not the right length is received, repeat.
    int get = 0;
    while((get+=recv(u_sock,(reinterpret_cast<char*>(&newlength))+get,4,0)) < 4) {}
    std::cout<<"Length: " << newlength << std::endl;
    //Create new char array with newlength + 1 so we have a zero terminated string.
    char* newMsg = new char[newlength+1];
    memset(newMsg,0,newlength+1);
    get = 0;
    //Receive the string. If not the right length is received, repeat.
    while((get+=recv(u_sock,newMsg+get,newlength,0)) < newlength) {}
    std::cout<<"Message: " << newMsg << std::endl;

    closesocket(u_sock);


    int i;
    std::cin >> i;
    return 0;
}

Java Server:

public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.#     
        int portNumber = 15000;

        try (

            ServerSocket serverSocket =  new ServerSocket(portNumber);

            Socket clientSocket = serverSocket.accept();     


            OutputStream os = clientSocket.getOutputStream();
            PrintWriter out = new PrintWriter(os, true);               
            //BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            InputStream is = clientSocket.getInputStream();

        ) {

            //RECV
            //Create ByteBuffer for length integer
            ByteBuffer bLength = ByteBuffer.allocate(4);
            //C is usually Little_Endian
            bLength.order(ByteOrder.LITTLE_ENDIAN);
            //Read 4 bytes
            is.read(bLength.array(), 0, 4);
            //Convert the length
            int length = bLength.getInt();
            System.out.println("Length: "+length);


            //Allocate ByteBuffer for message
            ByteBuffer bMessage = ByteBuffer.allocate(length);
            bMessage.order(ByteOrder.LITTLE_ENDIAN);
            is.read(bMessage.array(), 0, length);
            //Convert the message to string
            String msg = new String( bMessage.array() );
            System.out.println(msg);


            //SEND
            //Create ByteBuffer with length
            ByteBuffer bLengthNew = ByteBuffer.allocate(4);
            bLengthNew.order(ByteOrder.LITTLE_ENDIAN);
            bLengthNew.putInt(msg.length());

            //Write the length bytebuffer to the outputstream
            os.write(bLengthNew.array());

            //Write the message to the outputstream. (Don't use println)
            out.print(msg);
            //Flush it. (It automatically gets flushed on a \n, but we dont want that.
            out.flush();

        } catch (IOException e) {

            System.out.println("Exception caught when trying to listen on port "

            + portNumber + " or listening for a connection");

            System.out.println(e.getMessage());

        }



    }