Skip to content

maint: Remove global struct #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions netex.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ get_tcp_client_sockfd(void)
}

int
get_tcp_server_sockfd(void)
get_tcp_server_sockfd(int sockfd)
{
struct sockaddr_in servaddr;

conn_inf.sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (conn_inf.sockfd == -1)
if (sockfd == -1)
{
perror("socket");
return -1;
Expand All @@ -121,20 +120,20 @@ get_tcp_server_sockfd(void)

// Binding newly created socket to given IP and verification
if ((bind
(conn_inf.sockfd, (struct sockaddr *) &servaddr,
(sockfd, (struct sockaddr *) &servaddr,
sizeof(servaddr))) != 0)
{
perror("bind");
close(conn_inf.sockfd);
close(sockfd);
return -1;
}

printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(conn_inf.sockfd, 5)) != 0)
if ((listen(sockfd, 5)) != 0)
{
perror("listen");
close(conn_inf.sockfd);
close(sockfd);
return -1;
}
else
Expand Down
2 changes: 1 addition & 1 deletion netex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern conn_info conn_inf;

int get_tcp_client_sockfd(void);

int get_tcp_server_sockfd(void);
int get_tcp_server_sockfd(int sockfd);

int get_udp_server_sockfd(void);

Expand Down
3 changes: 2 additions & 1 deletion tcp_chat_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ main(int argc, char *argv[])
{
parse_server_opts(argc, argv);

if (get_tcp_server_sockfd() < 0)
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (get_tcp_server_sockfd(sockfd) < 0)
{
fputs("Error\n", stderr);
return -1;
Expand Down
7 changes: 4 additions & 3 deletions tcp_file_transfer_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,16 @@ recv_file(void)
static int
accept_connection(void)
{
get_tcp_server_sockfd();
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
get_tcp_server_sockfd(sockfd);

struct sockaddr_in cli;
socklen_t len = sizeof(cli);

// Accept the data packet from client and verification
conn_inf.connfd = accept(conn_inf.sockfd, (struct sockaddr *) &cli, &len);
conn_inf.connfd = accept(sockfd, (struct sockaddr *) &cli, &len);
// sockfd only needed if more connections are desired
if (close(conn_inf.sockfd))
if (close(sockfd))
perror("close() failed");

if (conn_inf.connfd < 0)
Expand Down
3 changes: 2 additions & 1 deletion tcp_multiple_connections.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ main(int argc, char *argv[])
struct sockaddr_storage remoteaddr; // Client address
socklen_t addrlen;

if (get_tcp_server_sockfd() < 0)
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (get_tcp_server_sockfd(sockfd) < 0)
{
fputs("Error\n", stderr);
return -1;
Expand Down