I'm trying to duplicate a close_wait issue on RHEL and found two parts of a sample on this site, but I lack the skill to get it working.
The second part is where I tried to add a main and compile, but it fails. I'm not even sure that's how it was intended to run but I don't see a function call in the first part so I assumed I need a "client" and a "server" binary.
Am hoping to get advice on how to make these pieces run.
This is the first part and it compiles OK.
Code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
int main()
{
int sock, connected, bytes_recieved , true = 1;
char recv_data[254];
struct sockaddr_in server_addr,client_addr;
int sin_size;
//FILE *f;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1) {
perror("Setsockopt");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero),8);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) {
perror("Unable to bind");
exit(1);
}
if (listen(sock, 5) == -1) {
perror("Listen");
exit(1);
}
fflush(stdout);
while(1)
{
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
//f = fopen("/var/log/keys.log","w");
//printf("received conn");
while(bytes_recieved = recv(connected, recv_data,254,0))
{
recv_data[bytes_recieved] = '\0';
//printf("%s", recv_data);
openlog("keys", LOG_PID, LOG_AUTHPRIV);
syslog(LOG_INFO, "%s", recv_data);
fflush(stdout);
close(sock);
}
}
//fclose(f);
return 0;
}
Part 2.
Code:
int sck_log(char *message) {
int sock;
char send_data[254];
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname("10.25.14.110");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(LOG_PORT);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
syslog(LOG_INFO, "Unable to start socket");
exit(1);
}
else
{
send(sock,message,strlen(message),0);
return 0;
}
return 0;
}
Here's the original thread:
C Sockets issue
Thanks,
Gary