0
I am new in c and in the area of networks, went to create a code in c to put into practice the theory, however, after bindar and create the connect socket, any function like recv() or send() returns me -1 [error]. Sometimes too, just by putting a printf() or changing anything in the code I get a connection error on the client side. To simulate a client I am using netcat 7.8. If there is a possibility of someone explaining to me why of this mistake I will be grateful. If there are suggestions for code improvements and everything else please do not hesitate to talk! Thank you.
Here’s the code:
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
int main()
{
printf("\niniciando ...\n");
int server, conecta, client,x;
struct sockaddr_in endereco_servidor;
struct sockaddr_in endereco_cliente;
server = socket(AF_INET, SOCK_STREAM, 0);
endereco_servidor.sin_family = AF_INET;
endereco_servidor.sin_port = htons(30000);
endereco_servidor.sin_addr.s_addr = inet_addr("10.0.0.176");
bind(server, (struct sockaddr *)&endereco_servidor, sizeof endereco_servidor);
x = listen(server, 1);
while(1==1)
{
printf("\nENtROU WHILE\n");
client = accept(server, (struct sockaddr *)&endereco_cliente, sizeof endereco_cliente);
x = send(client, "Ola", 100, 0);
printf("%i", x);
}
return 0;
}
In the part where I use (struct sockaddr *)&address_client, what does this pointer mean? Ah, and thanks for the answer and very clear tips, they were of great help!
– Lucas Bernardes