Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 程序不';t进入主功能?_C_Main - Fatal编程技术网

C 程序不';t进入主功能?

C 程序不';t进入主功能?,c,main,C,Main,我正在尝试创建一个服务器-客户机连接,但由于某种原因,当我运行服务器代码时,它只会给我提示,什么也没有发生。我将printf作为主函数中的第一件事,但即使这样也没有发生。在主审之前可以执行什么?我想我没有任何可能引起问题的东西 #include "md5.h" #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #incl

我正在尝试创建一个服务器-客户机连接,但由于某种原因,当我运行服务器代码时,它只会给我提示,什么也没有发生。我将printf作为主函数中的第一件事,但即使这样也没有发生。在主审之前可以执行什么?我想我没有任何可能引起问题的东西

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

void *handle_connection (void *);                               //gets a connection identifier and handles that incoming connection
int verify_password (char s[]);                                 // verifies if the input password is correct

int main(int argc, char *argv[])
{
    pthread_t connected;
    pthread_attr_t attributes;
    int listenfd = 0, conn_id = 0;                          // the listening socket identifier and current connection identifier
        //A: Creates the master socket 
    listenfd = socket (PF_INET, SOCK_STREAM, 0);   
    //B: the struct variable that keeps the socket address
    struct sockaddr_in sad;                                 /* structure to hold an IP address */ 
    struct sockaddr_in cad;
    struct hostent *ptrh;                                   /* pointer to a host table entry */ 
    socklen_t alen;
    //B: initialization of socket address variable
    memset ((char *)&sad, 0, sizeof(sad));                  /* clear sockaddr structure */ 
    sad.sin_family = AF_INET;                               /* set family to Internet */ 
    sad.sin_addr.s_addr = INADDR_ANY;                       /* set the local IP address */ 
    sad.sin_port = htons((u_short) 5000);                   /* set the port number */ 
    //C: binds the socket to its address
    bind (listenfd, (struct sockaddr *)&sad, sizeof(sad));
        //D: starts listening on that socket
    listen (listenfd, 100000);

        while (1)                                               // listens for incoming connections until program exits manually
        {
        int conn_id = 0;        
        //E: Wait for a connection request and accept it
        conn_id = accept (listenfd, (struct sockaddr *)&cad, &alen);
        //F: if the connection has been established it creates a thread that handles the connection       
        if (conn_id != -1)
        {
            pthread_attr_init (&attributes);
            pthread_attr_setdetachstate (&attributes, PTHREAD_CREATE_DETACHED);

            pthread_create (&connected, &attributes, (void *) handle_connection, &conn_id); 
        } 
        }
}

void *handle_connection(void *arg)
{
    int conn_id = *(int *) arg;
    char recv_buff[1024];                                   // the buffer that is used for keeping the data read from the socket
    int n = 0;                                              // number of characters read from socket
    //G: read from connection into the buffer and checks if recieving data was successful
    if (n = recv (conn_id, recv_buff, 1024, 0) <= 0)
    {
        printf ("Error Reading");
    }
    //H: verifies if the password is correct and sends T if the password is correct and F otherwise 
    if (verify_password (recv_buff) == 1)
    {
        if (send (conn_id, "T", 1, 0) < 1) {
            printf ("Error Sending");
        }
    } else {
        if (send (conn_id, "F", 1, 0) < 1) {
            printf ("Error Sending");
        }
    }
        //I: closes the connection
    close (conn_id);    
}

int verify_password(char s[])
{
    char md5_hashed_password[33] ="24af484e92082ad450a63a69f924f10a"; // The password of server hashed by MD5
    char md5_hashed_text[33];                                         // Keeps the MD5 hashed text of input text
    size_t len = strlen(s);
    md5 ((uint8_t*)s, len, md5_hashed_text);                          //Finds the MD5 hashed of input text
    if (strncmp (md5_hashed_text, md5_hashed_password, 32) == 0)            //If the hashed value of the input value and the 
        return 1;                             // password is same the password is accepted
    return 0;
}
#包括“md5.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
void*手柄连接(void*)//获取连接标识符并处理该传入连接
int验证_密码(字符s[]);//验证输入密码是否正确
int main(int argc,char*argv[])
{
pthread_t已连接;
pthread_attr_t属性;
int listenfd=0,conn_id=0;//侦听套接字标识符和当前连接标识符
//A:创建主套接字
listenfd=套接字(PF_INET,SOCK_STREAM,0);
//B:保存套接字地址的结构变量
sad中的struct sockaddr_;/*用于保存IP地址的结构*/
cad中的结构sockaddr_;
struct hostent*ptrh;/*指向主机表项的指针*/
socklen_t alen;
//B:套接字地址变量的初始化
memset((char*)&sad,0,sizeof(sad));/*清除sockaddr结构*/
sad.sin_family=AF_INET;/*将家庭设置为Internet*/
sad.sin_addr.s_addr=INADDR_ANY;/*设置本地IP地址*/
sad.sin_port=htons((u_short)5000);/*设置端口号*/
//C:将套接字绑定到其地址
绑定(listenfd,(struct sockaddr*)&sad,sizeof(sad));
//D:开始监听那个插座
倾听(已听,100000);
while(1)//侦听传入连接,直到程序手动退出
{
int conn_id=0;
//E:等待连接请求并接受它
conn_id=accept(listenfd,(struct sockaddr*)和cad,&alen);
//F:如果连接已经建立,它将创建一个线程来处理连接
如果(连接id!=-1)
{
pthread_attr_init(&attributes);
pthread_attr_setdetachstate(&attributes,pthread_CREATE_detachd);
pthread_创建(&connected,&attributes,(void*)句柄_连接,&conn_id);
} 
}
}
void*handle\u连接(void*arg)
{
int conn_id=*(int*)arg;
char recv_buff[1024];//用于保存从套接字读取的数据的缓冲区
int n=0;//从套接字读取的字符数
//G:从连接读取到缓冲区,并检查接收数据是否成功

如果(n=recv(conn_id,recv_buff,1024,0)编译好了吗?把它扔进gdb,在main上断开。很可能你会到达那里。@Ed Heal是的,没有错误,没有警告。这行看起来可疑
listenfd(listenfd,100000);
我的编译器中有很多警告“n=recv(conn_id,recv_buff,1024,0)