在c中创建和连接套接字:绑定和连接错误

问题描述 投票:0回答:1

这是我的作业,我一直在研究诸如geeksforgeeks之类的资源,但是我不知道如何解决绑定失败错误并阻止连接挂起。这是我的代码:

 /*
 * Assignment:
 * Write a program that optionally accepts an address and a port from the command line.
 * If there is no address/port on the command line, it should create a TCP socket and print the address 
 * (i.e. server mode). If there is an address/port, it should connect to it (i.e. client mode). 
 * Once the connections are set up, each side should enter a loop of receive, print what it received,
 * then send a message. The message should be “ping” from the client and “pong” from the server.
 */

    #include <stdio.h>
    #include <unistd.h>
    #include <netdb.h>
    #include <string.h>
    #include <stdlib.h>
    #include <arpa/inet.h>

    int main(int argc, char *argv[]) {
        char buffer[100];
        char *msg_server = "pong";
        char *msg_client = "ping";

        ///server mode
        if (argc==1) { //if no address/port was given
            struct sockaddr_in me, other;
            int sockfd, new_sockfd;  // listen on sock_fd, new connection on new_sockfd
            printf("No address was given. Creating socket\n");
            //create a TCP socket
            sockfd = socket(PF_INET, SOCK_STREAM, 0);
            if (sockfd <= 0) {
                printf("Error: Socket is not created\n");
                return -1;
            }
            memset(&me, 0, sizeof(me));
            me.sin_family = AF_INET;
            me.sin_port = htons(8000);
            me.sin_addr.s_addr = INADDR_ANY;
            printf("IP address: %s\n",inet_ntoa(me.sin_addr));
            //bind -> listen -> accept
            if(bind(sockfd, (struct sockaddr *) &me, sizeof(me)) < 0) {
                printf("Error: Bind unsuccessful\n");
                return -1;
            }
            if (listen(sockfd, 3) < 0) {
                printf("Error: Listen unsuccessful\n");
                return -1;
            }
            if ((new_sockfd = accept(sockfd, (struct sockaddr *) &other, (socklen_t *) sizeof(other))) < 0) {
                printf("Error: Accept unsuccessful\n");
                return -1;
            }
            //loop of receive, printing received, sending a message
            while (1) {
                read( new_sockfd , buffer, 1000); //receive
                printf("%s\n",buffer ); //print
                send(new_sockfd, msg_server, strlen(msg_server), 0 ); //send
            }
        }
            ///Client mode
        else //there is an address/port
        {
            int sockfd, new_sockfd, port;  // listen on sock_fd, new connection on new_sockfd
            struct sockaddr_in other;
            port = atoi(argv[2]);
            printf("Address entered: %s\nPort entered: %s\n", argv[1], argv[2]);
            sockfd = socket(PF_INET, SOCK_STREAM, 0);
            if (sockfd <= 0) {
                printf("Error: Socket not created\n");
                return -1;
            }
            memset(&other, 0, sizeof(other));
            other.sin_family = AF_INET;
            other.sin_addr.s_addr = INADDR_ANY;
            other.sin_port = htons(port);

            if(inet_pton(AF_INET, argv[1], &other.sin_addr)<=0)
            {
                printf("Invalid address.\n");
                return -1;
            }
            if (connect(sockfd, (struct sockaddr *)&other, sizeof(other)) < 0)
            {
                printf("Error: Connection is unsuccessful.\n");
                return -1;
            }
            //loop of receive, printing received, sending a message
            while (1) {
                read( new_sockfd , buffer, 1000); //receive
                printf("%s\n",buffer ); //print
                send(new_sockfd, msg_client, strlen(msg_client), 0 ); //send
            }
        }
        return 0;
    }

输出如下所示:enter image description here

我已经离开了^ C或^ Z。如果有人可以给我建议如何解决这些错误,甚至如何改善我的代码,我将非常感激。这是我第一次编码服务器和posix资料。

c networking posix windows-subsystem-for-linux unistd.h
1个回答
1
投票

客户端和服务器都在执行read 之前 send,因此他们两者]最初都在等待永远不会到达的消息。

其中一个已经

第一并发送消息。

发件人:

消息应该是从客户端“ ping”到服务器的“ pong”。

表示客户端应发送第一条消息。这意味着客户端应该先执行:send然后再执行read(相对于先执行read然后再执行send)。

但是,没有一个检查read的返回值,因此如果另一个关闭套接字,则两个都不会退出。

© www.soinside.com 2019 - 2024. All rights reserved.