[英]Segmentation fault using the buffer recived from recv() tcpServer c
我正在嘗試在c中創建應用程序客戶端/服務器,但是在recv()之后,當我嘗試使用接收到的緩沖區時,程序給出了分段錯誤(創建了核心轉儲),我無法解決。 這是我在服務器端的代碼:
int req_socket_id;
int comunication_socket_id;
struct sockaddr_in server_add;
struct sockaddr_in client_add;
socklen_t client_add_size;
char buffer[255];
//char mess[1024];
int i, n;
//int index;
unsigned int num;
// AF_INET = famiglia di indirizzi iPv4
req_socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(req_socket_id<0)
{
printf("Socket initialization failed !!");
return -1;
}
e
memset(&server_add, 0, sizeof(server_add)); // azzeramento struttura
server_add.sin_family = AF_INET; // dominio indirizzi IP
server_add.sin_addr.s_addr = 0; // indirizzo IP
server_add.sin_port = htons(23165); // numero di porta UDP
if(bind(req_socket_id, (struct sockaddr*) &server_add, sizeof(server_add)) < 0)
{
perror("\nErrore associazione porta e socket!\n");
close(req_socket_id);
return -1;
}
if(listen(req_socket_id, 1)<0)
{
perror("\nErrore nell'ascolto!\n");
close(req_socket_id);
return -1;
}
while(1)
{
client_add_size = sizeof(client_add);
comunication_socket_id = accept(req_socket_id, (struct sockaddr*) &client_add, &client_add_size);
if(comunication_socket_id>=0)
{
//index = 0;
char tmp[100];
while(1)
{
printf("\nOk");
//n = recv(comunication_socket_id, (char*) buffer, sizeof(buffer)+1, 0);
//n = recv(comunication_socket_id, (void*) buffer, sizeof(buffer)+1, 0);
n = recv(comunication_socket_id, buffer, sizeof(buffer)+1, 0);
printf("\nReceved! n: %d", n);
printf("\nReceved: %s", buffer);
if(strcmp(buffer, "end")==0)
{
close(comunication_socket_id);
printf("\n...socket closed");
return -1;
}
[...]
這是客戶端代碼:
unsigned long start, now;
unsigned int *num = (unsigned int*)buffer;
int i, n;
TCPclient_send(buffer, strlen(buffer)+1);
printf("\nSent: |%s|\n", buffer);
start = clock(); // tempo iniziale attesa
now = clock(); // tempo attuale
while ((now - start) < TIMEOUT)
{
if ((n = TCPclient_receive(&buffer[i], sizeof(buffer)-i)) > 0)
{
i += n;
if (i >= sizeof(unsigned int))
{
// risposta completa
printf("Receved number %u.\r\n", ntohl(*num));
TCPclient_disconnect();
return 0;
}
}
now = clock();
}
printf("No answer receved!\r\n");
TCPclient_disconnect();
控制台服務器端輸出:
davide@davide-VirtualBox:~/Documenti/RubricaTCP$ ./TCPserver
Ok
Receved! n: 11
Errore di segmentazione (core dump creato) //-->segmentation fault, end of process
控制台客戶端輸出:
Insert the command (end to close): SET=A;A;A;
Sent: |SET=A;A;A;|
No answer receved!
PS:這是我第一個使用套接字的應用程序,因此我可能犯了一些愚蠢的錯誤。 我在許多主題中尋找答案,但沒有找到任何可行的方法。 非常感謝你
n = recv(comunication_socket_id, buffer, sizeof(buffer)+1, 0);
sizeof(buffer)+1
本質上是不正確的。 它應該是sizeof(buffer)
。 您正在使用不存在的內存。
printf("\nReceved! n: %d", n);
好。
printf("\nReceved: %s", buffer);
錯誤。 這應該是
printf("\nReceved: %.*s", n, buffer);
然后:
if(strcmp(buffer, "end")==0)
這也是錯誤的。 無法保證您會收到一個以空值終止的字符串或完整的命令。 這是一個字節流協議。 如果需要消息,則必須自己實施。 對於沒有讀取或接收循環的問題,編寫網絡代碼或任何I / O代碼很少是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.