my text file is IP_CONFIG.txt
"192.168.128.3" IP_CS
"192.168.128.2" IP_HMI
"192.168.128.1" IP_OBCU
"192.168.128.4" IP_ASR
"127.0.0.1" IP_RSO
"127.0.0.1" IP_RSO_D
"1901" PORT_CS
"1901" PORT_HMI
"1901" PORT_OBCU
"3567" PORT_ASR
"4444" PORT_RSO
"7777" PORT_RSO_D
My code is
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAX_DATE 100
# define BUFLEN 1024
#define ROW 11
#define COL 2
#define MAXWORDS 24
int main(int argc,char **argv)
{
//Declared variables
int sock,sock_RST; // socket name
int bytes_read; // variable for recvfrom
int addrlen; // length of address
int i=0,j=0,k=0, z=0; // counters
unsigned char ca[BUFLEN], last_OBCU_msg[BUFLEN], last_HMI_msg[BUFLEN],rcvd_ASR_msg[BUFLEN]; // Data buffers
struct sockaddr_in server_addr,HMI_addr,OBCU_addr,ASR_addr,RSO_addr,test_addr;//addresses IP PORT
struct sockaddr_in RSO_addr_d;
const char yes = 1;
int TAG_ASR = 0;
// Create a lof file
time_t now;
char the_date[MAX_DATE];
the_date[0] = '\0';
now = time(NULL);
strftime(the_date, MAX_DATE, "CS_LOG_%H_%M_%d_%m_%Y"".txt", gmtime(&now));
chdir("/home/bsnayak/CS_LOG/");
FILE *file = fopen(the_date, "a");
//Read IPS and PORTS from a text file
FILE *fp = fopen("IP_CONFIG.txt","r");
int ii=0, jj;
char *words=NULL,*word=NULL,c;
char *allwords[MAXWORDS];
while ((c = fgetc(fp))!= EOF)
{
ii++;
if (c=='\n')
{
c = ' ';
}
words = (char *)realloc(words, (ii+1) * sizeof(char));
words[ii-1]=c;
}
words[ii] = '\0';
word=strtok(words," ");
ii=0;
while(word!= NULL && ii < MAXWORDS){
//printf("%s\n",word);
allwords[ii] = malloc(strlen(word)+1);
strcpy(allwords[ii], word);
word = strtok(NULL," ");
allwords[ii][strlen(word)] = '\0';
ii++;
}
printf("\nNow printing each saved string:\n");
for (jj=0; jj<ii; jj++){
printf("String %d: %s\n", jj, allwords[jj]);
}
char * IP_CS = allwords[0];
char *IP_HMI = allwords[2];
char *IP_OBCU = allwords[4];
char *IP_ASR = allwords[6];
char *IP_RSO = allwords[8];
char *IP_RSO_D = allwords[10];
int PORT_CS = atoi(allwords[12]);
int PORT_HMI = atoi(allwords[14]);
int PORT_OBCU = atoi(allwords[16]);
int PORT_ASR = atoi(allwords[18]);
int PORT_RSO = atoi(allwords[20]);
int PORT_RSO_D = atoi(allwords[22]);
free(allwords[MAXWORDS]);
setbuf(stdout, NULL);
addrlen= sizeof(struct sockaddr_in);
// Create the Socket for all connections except RST
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("Socket Creation Error");
exit(1);
}
// Create the Socket for RST section
if ((sock_RST = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("Socket Creation Error");
exit(1);
}
// make CS socket non blocking and reusable
fcntl(sock, F_SETFL, O_NONBLOCK);
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(int)) < 0)
{
perror("Reuse option\n");
close(sock);
exit(1);
}
// make RST socket non blocking and reusable
fcntl(sock_RST, F_SETFL, O_NONBLOCK);
if (setsockopt(sock_RST, SOL_SOCKET, SO_REUSEADDR, (char*)&yes, sizeof(int)) < 0)
{
perror("Reuse option\n");
close(sock_RST);
exit(1);
}
printf("\nchecked1\n");
// Control server properties
bzero(&(server_addr.sin_zero),sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT_CS);
server_addr.sin_addr.s_addr = inet_addr(IP_CS); //inet_addr("192.168.128.3"); //
// HMI server properties
bzero(&(HMI_addr.sin_zero),sizeof(HMI_addr));
HMI_addr.sin_family = AF_INET;
HMI_addr.sin_port = htons(PORT_HMI);
HMI_addr.sin_addr.s_addr = inet_addr(IP_HMI);
// OBCU server properties
bzero(&(OBCU_addr.sin_zero),sizeof(OBCU_addr));
OBCU_addr.sin_family = AF_INET;
OBCU_addr.sin_port = htons(PORT_OBCU);
OBCU_addr.sin_addr.s_addr = inet_addr(IP_OBCU);
// ASR Server properties
bzero(&(ASR_addr.sin_zero),sizeof(ASR_addr));
ASR_addr.sin_family = AF_INET;
ASR_addr.sin_port = htons(PORT_ASR);
ASR_addr.sin_addr.s_addr = inet_addr(IP_ASR);
// RSO server properties
bzero(&(RSO_addr.sin_zero),sizeof(RSO_addr));
RSO_addr.sin_family = AF_INET;
RSO_addr.sin_port = htons(PORT_RSO);
RSO_addr.sin_addr.s_addr = inet_addr(IP_RSO);
// RSO destination properties (To which you send string content)
bzero(&(RSO_addr_d.sin_zero),sizeof(RSO_addr_d));
RSO_addr_d.sin_family = AF_INET;
RSO_addr_d.sin_port = htons(PORT_RSO_D);
RSO_addr_d.sin_addr.s_addr = inet_addr(IP_RSO_D);
// BIND Controlserver to the main socket
if (bind(sock,(struct sockaddr *)&server_addr,sizeof(struct sockaddr)) != 0)
{
perror("Bind Error");
close(sock);
exit(1);
}
printf("checked2\n");
// Bind RSO for second socket
if (bind(sock_RST,(struct sockaddr *)&RSO_addr,sizeof(struct sockaddr)) != 0)
{
perror("Bind Error");
close(sock_RST);
exit(1);
}
printf("checked3\n");
fprintf(file,"Communication Starts- Below is the Log datas\n\n");
fprintf(file,"--------------------------------------------------------");
fprintf(file,"\n\n");
// MAIN WHILE LOOP BEGIN
//
fclose(file);
return 0;
}
My objective is to get the IP & Ports from the txt files and use it in defining the socket addresses as a variable. So that i can modify the txt file and not the code.
I am getting this error
Segmentation fault (core dumped)
Kindly tell, How to solve it. Incase, Please post the updated code.
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire