0

Helo,我正在尝试像这样通过它

typedef struct t_timeSliceRequest{
unsigned int processId;
unsigned int timeRequired;
int priority;
}timeSliceRequest;

struct t_timeSliceRequest request = { 1,2,1 };
sendFlag = send(socketID,(timeSliceRequest *) &request, sin_size ,0);

在服务器端

recvFlag = recv(socketID,(timeSliceRequest *) &request,sin_size,0);

但它接收垃圾,甚至recv返回-1,请帮助

这是我完整的康德

#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<arpa/inet.h>
#include<time.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>

enum priority_e{ high, normal, low };

typedef struct t_timeSliceRequest{
    unsigned int processId;
    unsigned int timeRequired;
    int priority;
}timeSliceRequest;

typedef struct t_TimeSliceResponse {
    timeSliceRequest original_req;

    // Unix time stamp of when process was started on server
    unsigned int time_started;

    // Waiting and running time till end of CPU bust
    unsigned int ttl;

} TimeSliceResponse;


int main(int argc, char ** argv){
    int socketID = 0, clientID = 0;
    char sendBuffer[1024], recvBuffer[1024];
    time_t time;
    struct sockaddr_in servAddr, clientAddr;
    struct t_timeSliceRequest request = {1,1,0};

memset(sendBuffer, '0', sizeof(sendBuffer));
memset(recvBuffer, '0', sizeof(recvBuffer));

fprintf(stdout,"\n\n --- Server starting up --- \n\n");
fflush(stdout);

socketID = socket(AF_INET, SOCK_STREAM, 0);
if(socketID == -1){
    fprintf(stderr, " Can't create Socket");
    fflush(stdout);
}

servAddr.sin_family = AF_INET;
servAddr.sin_port = htons(5000);
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);

int bindID, sin_size, recvFlag;
bindID = bind(socketID, (struct sockaddr *)&servAddr, sizeof(servAddr)); // Casting sockaddr_in on sockaddr and binding it with socket id
if(bindID!=-1){
    fprintf(stdout," Bind SucessFull");
    fflush(stdout);
    listen(socketID,5);
    fprintf(stdout, " Server Waiting for connections\n");
    fflush(stdout);
    while(1){
        sin_size = sizeof(struct sockaddr_in);
        clientID = accept(socketID, (struct sockaddr *) &clientAddr, &sin_size);
        fprintf(stdout,"\n I got a connection from (%s , %d)", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
        fflush(stdout);
        sin_size = sizeof(request);
        recvFlag = recv(socketID, &request,sin_size,0);
        perror("\n Err: ");
        fprintf(stdout, "\n recvFlag: %d", recvFlag);
        fprintf(stdout, "\n Time Slice request received:\n\tPid: %d \n\tTime Required: %d ", ntohs(request.processId), ntohs(request.timeRequired));
        fflush(stdout);
        snprintf(sendBuffer, sizeof(sendBuffer), "%.24s\n", ctime(&time));
        write(clientID, sendBuffer, strlen(sendBuffer));
        close(clientID);
        sleep(1);
    }
}else{
    fprintf(stdout, " Unable to Bind");
}
close(socketID);
return 0;
}

客户端代码是:

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

enum priority_e{ high = +1, normal = 0, low = -1};

typedef struct t_timeSliceRequest{
    unsigned int processId;
    unsigned int timeRequired;
    int priority;
}timeSliceRequest;

int main(int argc, char *argv[])
{
    int socketID = 0 /*Socket Descriptor*/, n = 0;
    char recvBuffer[1024];
    memset(recvBuffer, '0',sizeof(recvBuffer));
    struct sockaddr_in servAddr;

    struct t_timeSliceRequest request = { 1,2,high };

    if(argc!=2){
        fprintf(stderr,"\n Usage: %s <ip of server> \n",argv[0]);
        return 1;
    }

    socketID = socket(AF_INET, SOCK_STREAM, 0);
    if(socketID == -1){
        fprintf(stderr, "\n Can't create socket \n");
        return 1;
    }

    servAddr.sin_family = AF_INET;
    servAddr.sin_port = htons(5000);

    if(inet_pton(AF_INET, argv[1], &servAddr.sin_addr)==-1){
        fprintf(stderr, "\n Unable to convert given IP to Network Form \n inet_pton Error");
        return 1;
    }

    int connectFlag, sendFlag = 0;
    connectFlag = connect(socketID, (struct sockaddr *)&servAddr, sizeof(servAddr));
    if(connectFlag == -1){
       fprintf(stderr, " Connection Failed\n");
       return 1;
    }

    int sin_size = sizeof(struct t_timeSliceRequest);
    fprintf(stdout, " \n %d \n %d \n %d", request.processId, request.timeRequired, request.priority);
    sendFlag = send(socketID, &request, sin_size ,0);
    fprintf(stdout, "\nSend Flag: %d\n", sendFlag);

    n = read(socketID, recvBuffer, sizeof(recvBuffer)-1);
    recvBuffer[n] = 0;
    fprintf(stdout, "%s",recvBuffer);
    if(n < 0){
        fprintf(stderr, " Read error\n");
    }
    return 0;
}

这是完整的代码,它给出“传输端点未连接”

4

2 回答 2

3

请记住,通过网络发送这样的结构可能会导致互操作性问题:

  1. 如果源和目标具有不同的字节序,您将收到错误的数据(考虑使用htonl将数据转换为网络字节序等功能)
  2. 你的结构需要被打包,否则不同的编译器可以不同地对齐结构的变量(请参阅this以获得有关对齐变量的想法)

在任何情况下,都ENOTCONN表明在两台主机之间建立连接时出错。

于 2012-10-29T09:55:38.373 回答
0

当您的套接字未绑定到任何(端口,地址)对时,将返回传输端点未连接错误。

如果是服务器端,则应使用accept调用返回的套接字描述符。如果是客户端 - 您应该使用成功调用返回的套接字connect

顺便说一句,以你的方式发送结构是非常危险的。编译器可能会在结构成员之间插入填充字节(您的程序不可见,但它们在结构中占用空间)以符合目标平台的一些对齐规则。此外,不同的平台可能有不同的字节序,这可能会完全破坏你的结构。如果您的客户端和服务器是为不同的机器编译的,则结构布局和字节序可能不兼容。为了解决这个问题,您可以使用打包结构。将结构声明为打包的方式取决于编译器。对于 GCC,这可以通过向结构添加特殊属性来完成。

解决此问题的另一种方法是将结构的每个单独字段手动放入原始字节缓冲区。接收方应该以与最初将数据放入该缓冲区的方式完全相同的方式取出所有这些数据。这种方法可能很棘手,因为在保存多字节值(如 int、long 等)时需要考虑网络字节顺序。为此,有一组特殊的函数,例如htonl,等。htonsntohs

更新

在您的服务器中:

recvFlag = recv(socketID, &request,sin_size,0);

这里应该是

recvFlag = recv(clientID, &request,sin_size,0);

socketID是一个被动套接字,只能接受连接(不发送任何数据)。

更重要的是,accept没有检查 -1 的结果。

于 2012-10-29T09:47:47.953 回答