Skip to content

Commit

Permalink
Merge pull request markparticle#30 from mamil/leak
Browse files Browse the repository at this point in the history
fix socket leak and format code
  • Loading branch information
markparticle authored Oct 26, 2020
2 parents 3b090db + fed78d1 commit 3b37520
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions webbench-1.5/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
SCCS ID: @(#)socket.c 1.5 4/1/94
programmer: Virginia Tech Computing Center
compiler: DEC RISC C compiler (Ultrix 4.1)
environment: DEC Ultrix 4.3
environment: DEC Ultrix 4.3
description: UNIX sockets code.
***********************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
Expand All @@ -32,7 +32,7 @@ int Socket(const char *host, int clientPort)
unsigned long inaddr;
struct sockaddr_in ad;
struct hostent *hp;

memset(&ad, 0, sizeof(ad));
ad.sin_family = AF_INET;

Expand All @@ -47,12 +47,15 @@ int Socket(const char *host, int clientPort)
memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
}
ad.sin_port = htons(clientPort);

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
return sock;
if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0)
{
close(sock);
return -1;
}
return sock;
}

0 comments on commit 3b37520

Please sign in to comment.