Reputation: 171
I'm completely new to C.
And am following this yt tutorial on sockets. However he's using the close(sock)
function. There's 2 problems:
sock
. The socket is called something else.Can you please explain. This question might seem a little too dumb. Here's the Client code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
// Create a socket
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
// Address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
printf("%d\n", connection);
char response[256];
recv(network_socket, &response, sizeof(response), 0);
printf("%s\n", response);
return 0;
}
Upvotes: 0
Views: 6539
Reputation: 95
You need to pass the socket file descriptor as the argument for close()
. Which, for your code, is network_socket
. So, close()
need to be called as close(network_socket);
Also, you need to use the <unistd.h>
header for the close()
function.
Upvotes: 3
Reputation: 34408
There's no variable called sock. The socket is called something else.
Yes, it's network_socket in your code. So you want close(network_socket);
.
I can't find the close function. The compiler is saying that the function doesn't exist
You need #include <unistd.h>
, which is where close() is declared. (If you look at man close
you'll see this at the top.)
Upvotes: 8