我正在创建服务器客户端连接,但出于某种原因,当我运行服务器代码时,它只会给我提示,没有任何反应。我把printf作为main函数中的第一件事,但即使这样也不会发生。什么可以在主要之前执行?我认为我没有任何可能导致问题的事情。
#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;
}