gpt4 book ai didi

c - 服务器/客户端的 C 指针问题

转载 作者:行者123 更新时间:2023-11-30 15:54:22 26 4
gpt4 key购买 nike

编写服务器/客户端程序并在使用 Netbeans 构建时出现这些错误:

dbserver.c:195: warning: embedded '\0' in format
dbserver.c:211: warning: passing argument 1 of 'db_lookup' from incompatible pointer type
dbserver.c:213: warning: passing argument 1 of 'db_insert' from incompatible pointer type
dbserver.c:241: warning: passing argument 1 of 'db_delete' from incompatible pointer type
dbserver.c:257: warning: passing argument 1 of 'db_lookup' from incompatible pointer type
dbserver.c:287: warning: embedded '\0' in format

这是有问题的代码:

//------------------------------------------------------------------------------
// Static Definitions
//------------------------------------------------------------------------------

#define WORD_LENGTH 32 //size of transmission & word length (31 characters + \0)
#define DICTIONARY_SIZE 100 //size of dictionary word count



//------------------------------------------------------------------------------
// Message Structure Definition
//------------------------------------------------------------------------------

typedef struct message
{
int msg_type; //message code used to call database functions
int msg_size; //expected size of message in transit
char msg[32]; //payload capacity
} message;



//------------------------------------------------------------------------------
// Server Function & Variable Initialization
//------------------------------------------------------------------------------

char dictionary[DICTIONARY_SIZE][WORD_LENGTH]; //dictionary structure
int word_count = 0; //dictionary word counter
int db_initialization(); //create db dictionary
int db_insert(const char *word); //check for word, insert if not found
int db_delete(const char *word); //remove word from dictionary if found
int db_lookup(const char *word); //search dictionary for word
int db_function(int fd); //db function call interface
int errexit(const char *format, ...); //Khalid's error and exit
//establishes socket to listen for connections
int listen_socket_TCP(const char *service, int qlen);


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Main Function
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
char *service = "27792"; //"Magic Port" to listen on
struct sockaddr_in client_address; //IPv4 client address
unsigned int client_address_length; //IPv4 client address length
int master_socket; //TCP socket
int slave_socket; //TCP socket spawned off of Master

master_socket = listen_socket_TCP(service, WORD_LENGTH); //build database



//------------------------------------------------------------------------------
// Dictionary Code Adapted from Material provided by Khalid
//------------------------------------------------------------------------------


db_initialization(); //initialize the database
//seed the database
db_insert("hello");
db_insert("communication");
db_insert("depaul");
db_insert("cdm");

//--------------------------------------------------------------------------
// Test Database Function Operation
//--------------------------------------------------------------------------
if (db_lookup("cti"))
printf("found\n");
//end if

else
{
db_insert("cti");
printf("'cti' has been inserted\n");
} //end else

if (db_delete("cdm"))
printf("'cdm' has been successfully deleted\n");
//end if

else
printf("cannot find 'cdm'.");
//end else



//--------------------------------------------------------------------------
// Forever Loop to Listen for Connections
//--------------------------------------------------------------------------
while (1)
{
client_address_length = sizeof(client_address);
slave_socket = accept(master_socket, (struct sockaddr *)&client_address,
&client_address_length);

if (slave_socket < 0)
errexit("accept: %s\n", strerror(errno));
//end if

printf("Connecting...\n");
db_function(slave_socket);
close(slave_socket);
} //end while

}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// END MAIN FUNCTION
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Server Function Codes:
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------



//------------------------------------------------------------------------------
// Execute Database Functionality
//------------------------------------------------------------------------------

int db_function(int fd)
{
int finished = 0;
char re[2];
int x = -1;

while (!finished)
{
char buf[sizeof(message)];
int cc;
struct message *sptr;

message s;

cc = recv(fd, buf, sizeof(message), 0);
printf("Received Bytes: %i\n", cc);

sptr = (struct message *) buf;

memcpy(&s, sptr,sizeof(message));

if (s.msg_size != strlen(s.msg))
{
x = -1;
sprintf(re,"%d \0", x);
send(fd, re, 2, 0);
break;
} //end if


//----------------------------------------------------------------------
// Message Code 1 - Insert Word into Dictionary
//
// Return 1: Successfully Inserted Word
// Return 0: Word Exists in Dictionary Already
// Return -1: Failed to Add Word to Dictionary
//----------------------------------------------------------------------

if (s.msg_type == 1)
{
if ((db_lookup(&s.msg)) == 0) //search for preexisting word
{
if ((db_insert(&s.msg)) > 0) //insert word into dictionary
{
x = 1; //word added successfully
} //end insert if

else
x = -1;//unsuccessful in adding word

} //end search and insert if

else
x = 0;

printf("Adding to dictionary: %s result %i\n", s.msg, x);
} //end if message type 1



//----------------------------------------------------------------------
// Message Code 2 - Delete Word from Dictionary
//
// Return 1: Successfully Deleted Word
// Return 0: Word does not Exist in Dictionary
// Return -1: Failed to Delete Word from Dictionary
//----------------------------------------------------------------------

if (s.msg_type == 2)
{
x = db_delete(&s.msg);
printf("Deleting from dictionary: %s result %i\n", s.msg, x);
} //end if message type 2



//----------------------------------------------------------------------
// Message Code 3 - Lookup Word in Dictionary
//
// Return 1: Successfully Found Word
// Return 0: Word not FOund in Dictionary
// Return -1: Function Failed
//----------------------------------------------------------------------

if (s.msg_type == 3)
{
x = db_lookup(&s.msg);
printf("Looking up from dictionary: %s result %i\n", s.msg, x);
} //end if message type 3



//----------------------------------------------------------------------
// Message Code 4 - Close Connection
//----------------------------------------------------------------------

if (s.msg_type == 4)
{
x = 1;

printf("Closing connection: %s result %i\n", s.msg, x);

break;
} //end if message type 4


printf("Words in dictionary: {");

int c = 0;
for (c=0; c < word_count; c++)
{
printf("%s, ", dictionary[c]);
}

printf("}\n");

sprintf(re,"%d\0", x);

send(fd, re, 2, 0);
} // end while(!finished)

return 0;
} //end db_function
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
// Database Initialization
//------------------------------------------------------------------------------

int db_initialization()
{
word_count = 0;
return 1;
} //end db_initialization
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
// Insert Word into Dictionary
//------------------------------------------------------------------------------
int db_insert(const char *word)
{
strcpy(dictionary[word_count], word);
word_count++;
return word_count;
} //end db_insert
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
// Delete Word from Dictionary
//------------------------------------------------------------------------------
int db_delete(const char *word)
{
int i;
for (i=0 ; i < word_count ; i++)
{
if (strcmp(word, dictionary[i]) == 0)
{
word_count--;

strcpy(dictionary[i], dictionary[word_count]);

return 1;
} //end if

} //end for

return 0;
} //end db_delete
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------





//------------------------------------------------------------------------------
// Lookup Word in Dictionary
//------------------------------------------------------------------------------
int db_lookup(const char *word)
{
int i;
for (i=0 ; i<word_count ; i++)
{
if (strcmp(word, dictionary[i]) == 0)
return 1;
} //end for

return 0;
} //end db_lookup
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

我已经排除了预处理器指令和listen_socket_TCP函数(在另一个源文件中)。

最佳答案

首先,这些是警告,而不是错误......

无论如何,问题似乎出在您传递字符串的方式上:dbserver.c:211:警告:从不兼容的指针类型传递“db_lookup”的参数 1

db_lookup() 正在查找 const char *,例如在此处使用时:

 if (db_lookup("cti"))

你正在通过它:

   if ((db_lookup(&s.msg)) == 0) //search for preexisting word 

结构中 char 指针的地址,如果您只是传递它:

   if ((db_lookup(s.msg)) == 0) //search for preexisting word 

这应该可以解决该类型的警告。

其次,从 sprintf 中删除 \0 以消除以下两个警告:警告:格式中嵌入了“\0”

关于c - 服务器/客户端的 C 指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com