需要制作一个tcp计算器,客户端出了问题

1.不删除 下面这段代码的话,会出错.
if (argc !=2){ //number of parameters wrong
perror("Input: ./server12 to use");
exit(1);
}
2.删除了之后,可以编译但运行的时候会提示Segmentation fault (core dumped);
3.修改了 servaddr.sin_addr.s_addr= inet_addr(argv[1]);改为htonl(INADDR_ANY),2的错误已经解决,但是网络连接失败..Fail to connect to the server: Connection refused
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 1024
#define SERV_PORT 10010

int main(int argc, char **argv) {/

    int sockfd;
    struct sockaddr_in servaddr;
    char recvmsg[MAXLINE];
    unsigned num1, num2, result;
    char operator, valid;
    char str1[] = "Valid", str2[] = "Invalid", valid_s[10];
    char sendmsg[MAXLINE];



if (argc !=2){ 
  perror("Input: ./server12 <IP address of the server> to use");
  exit(1);
}


if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0){
  perror("Socket initialize failed");
  exit(2);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);/
servaddr.sin_port =  htons(SERV_PORT);


if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0){
  perror("Problem in connecting to the server");
  exit(3);
}

while(1){


    printf("%s\n", "Please enter the first integer");
    while (scanf("%u", &num1) != 1){
            while(getchar() != '\n');
            printf("%s\n", "Input type illegal");
            printf("%s\n", "Please enter the first integer again");

    }

    while(getchar() != '\n');



    // get operation
    printf("%s\n", "Please enter the operation ");
    while((scanf("%c", &operator)!= 0) && (operator !=43 &&operator != 45 && operator!= 120 && operator!=47)){
            while(getchar() != '\n');
            printf("%s\n", "Operator is illegal or does not exist");
            printf("%s\n", "Please check the input");
    }

    while(getchar() != '\n');


    // get the second number to operate
    printf("%s\n", "Please enter the second integer");
    while (scanf("%u", &num2) != 1){//check input type
            while(getchar() != '\n');
            printf("%s\n", "Input type illegal");
            printf("%s\n", "Please enter the second integer again");
    }

    while(getchar() != '\n');




    memcpy(sendmsg, &operator, 1);
    memcpy(sendmsg + 1, &num1, 4);
    memcpy(sendmsg + 5, &num2, 4);


    //send message
    send(sockfd, sendmsg, MAXLINE, 0);
    printf("Message sent to the server: %u %c %u\n", num1, operator, num2);



    if (recv(sockfd, recvmsg, MAXLINE,0) == 0){ 

            perror("The server has terminated ");
            exit(4);
    }else{




    memcpy(&result, recvmsg + 9, 4);



    valid = recvmsg[13];



    if(valid == '1'){//success
      strncpy(valid_s, str1, sizeof(str1));
    }else//failed
    {
      strncpy(valid_s, str2, sizeof(str2));
    }
    printf("Result: %u %c %u = %d, %s.\n", num1, operator, num2, result, valid_s);//print the result
  }
}

exit(0);
}

3个回答

1.你说的第一点,是因为参数个数不是2个,就直接退出程序了,因此不会出错;
2.第二点是说明段错误了,引用了argv[1],因为argv[1]现在是NULL;
3.修改后是因为没用到argv[1],所以不会段错误。
你运行的时候有没有带参数?明显是没带参数出的问题。如果你在Linux系统,假设你的程序名为 tcp_cli 试着这样运行: ./tcp_cli 127.0.0.1

“Connection refused”telnet下端口看通不通, 服务起来了没有

1应该不会报错,并没有问题。由1、3可以看出2的问题是由于程序没有正确处理输入的参数造成的,你的1报错也是程序无法处理输入的原因,具体原因我不
清楚,但我这里没有问题
3、你原来的意图是连接一个你输入的服务器ip 现在你把他改为了连接本地127.0.0.1了,和输入的服务器IP没关系了,你本地没有服务端程序自然拒绝

你的程序用法应该是./server 127.0.0.1 这样的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问