socket客户端不在Kali工作

时间:2016-10-21 20:46:52

标签: c sockets segmentation-fault client-server

我试图创建一个简单的客户端 - 服务器程序,但总是在客户端上出现Segmentation fault。我检查了IP地址,端口,尝试了很多替代解决方案,但没有为我工作。

  

服务器代码:

    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    int main(int argc, char const *argv[])
    {
        int sock,mysock;
        struct sockaddr_in server;
        char buff[1024];

     char s[256];

    if (!gethostname(s, sizeof s))
        {
            printf ("Machine: %s\n", s);
            {
                struct hostent *host= gethostbyname(s);

                if (host  != NULL)
                {
                    struct in_addr **adr;

                    for (adr = (struct in_addr **)host->h_addr_list; *adr; adr++)
                    {
                        printf("IP : %s\n", inet_ntoa(**adr));
                    }
                }
            }
        } 


        /*creation du socket*/
        sock=socket(AF_INET,SOCK_STREAM,0);
        if (sock<0)
        {
            perror("creation de socket echouer\n");
            exit(1);
        }
        server.sin_family=AF_INET;
        server.sin_addr.s_addr=INADDR_ANY;
        server.sin_port=5000;

        if (bind(sock, (struct sockaddr *) &server,sizeof(server)) < 0) 
             {perror("ERROR on binding");
              exit(1);}

        /*listen for just 1 connection*/
        listen(sock,1);
        mysock=accept(sock,(struct sockaddr *)0,0);
        if (mysock==-1)
        {
            perror("accept failed");
            exit(1);
        }




        write(mysock,"you are connected to: my server\n",55);
        read(mysock,buff,sizeof(buff));
        printf("%s\n",buff);
        gets();
        close(sock);
        close(mysock);


        return 0;
    }
  

客户端代码:

#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char const *argv[])
{
    int sock;
    struct sockaddr_in server;
    struct hostent *hp;
    char buff[1024];

    /*creation du socket*/
    sock=socket(AF_INET,SOCK_STREAM,0);
    if (sock<0)
    {
        perror("creation de socket echouer\n");
        exit(1);
    }
    server.sin_family=AF_INET;
    hp = gethostbyname(argv[1]);
    if (hp == NULL) {
        perror("ERROR, no such host\n");
        exit(0);
    }
    printf("0\n");
    server.sin_addr.s_addr=inet_addr("127.0.1.1");
    server.sin_port=htons(5000);
printf("1\n" );
    if (connect(sock,(struct sockaddr *)&server,sizeof(server)<0))
    {
        perror("connection failed");
        exit(1);
    }printf("2\n");
    read(sock,buff,sizeof(buff));
    printf("%s\n",buff);
    write(sock,"thanks for accepting me\n",25);

    system("PAUSE");
    close(sock);
    return 0;
 }

2 个答案:

答案 0 :(得分:2)

由于这一行,您似乎在客户端上出现了分段错误:

hp = gethostbyname(argv[1]);

这里获取段错误的方法是传递NULL - 指针。因此,每次在没有命令行参数的情况下运行客户端时都会执行此操作 附:不太明白,为什么你需要它。

注意:

  • 将明显的错误修复为例如很好。斯蒂芬提到。还要初始化缓冲区,如下所示:char buff[1024] = {0};以避免麻烦。并检查函数的返回值以查看是否发生错误。
  • 来自man
  

gethostbyname *()和gethostbyaddr *()函数已过时。   应用程序应使用getaddrinfo(3)和getnameinfo(3)代替。

修改: 因此,您的客户的工作代码可以是这样的:

int main(int argc, char const *argv[]) {
    int sock;
    ssize_t bytes;
    struct sockaddr_in server;
    char buff[1024] = {0}, tosend[] = "thanks for accepting me";
    /*
    struct hostent *hp;
    if(argv[1]) {
        hp = gethostbyname(argv[1]);
        if (hp == NULL) {
            perror("gethostbyname");
            return;
        }
        // something with hp
    }
   */
    /*creation du socket*/
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("socket");
        exit(1);
    }
    memset(&server, 0, sizeof (struct sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr("127.0.1.1");
    server.sin_port = htons(5000);
    printf("1\n");
    if (connect(sock, (struct sockaddr *)&server, sizeof server) < 0) {
        perror("connect");
        exit(1);
    }
    printf("2\n");
    if ((bytes = read(sock, buff, sizeof(buff) - 1)) < 0) {
        perror("read");
        exit(1);
    } else if (! bytes)
        puts("finished");
      else
        printf("%s\n", buff);
    if (write(sock, tosend, sizeof tosend) < 0)
        perror("write");

    return 0;
 }

如果使用注释行,请在运行./client somename

等客户端时传递一些命令行参数 在服务器端

ALSO ,您至少应该更改以下内容:

  • 删除或更改gets()来电,因为它需要参数
  • 设置端口号,如htons(5000)

答案 1 :(得分:1)

可能会有更多问题,但这个问题肯定是错误的:

struct s inst = {
    .internal_buf = {0},
    .active_buf = .internal_buf, // Doesn't compile unfortunately
};