socket send()函数在发送数据时失败

z3yyvxxp  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(493)

**结束。**此问题不符合堆栈溢出准则。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

6年前关门了。
改进这个问题
所以我编写了一个客户机/服务器程序,其中客户机用c++编写,服务器用java编写。当我尝试向java端发送数据时,send()函数失败并返回-1。我不知道问题出在哪里。
以下是客户方:

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

这里是服务器端:

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();
4nkexdtk

4nkexdtk1#

您使用的强制类型转换无效 send() 功能。 int num = 152; cout << "Trying to send..." << endl; const char* buff = (const char*)(num); 在上面的代码中,您必须使用 & 为了 num ,就像下面的代码一样, const char* buff = (const char*)(&num); 对于“endianness”,使用 htonl() 好多了。所以,我认为这个代码会更好

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

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

lndjwyie2#

首先检查ipv4地址(在cmd中使用ipconfig),然后在线程中执行while循环来检查每次接收到的数据。

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

别忘了从客户端以字符串的形式发送数据

相关问题