Currently all I am trying to do is just fix this segmentation fault for a simple client socket connection I am trying to make. I am getting a segmentation fault everytime at the connect() line and I am not sure why. I have tried multiple different implementations of initializing everything to connect, but have had no luck getting around this seg fault.
Here is Client.C code:
/*LINUX socket initializations*/
#define DEFAULT_BUFLEN 512
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
int SendingSocket, portno;
struct hostent *server;
int iResult;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
char response[10];
#define DEFAULT_PORT "2212"
#define DEFAULT_ADDRESS "127.0.0.1"
int main(int argc, char* argv[];) {
/*
* Initializing socket
*/
struct sockaddr_in serv_addr;
portno = atoi(DEFAULT_PORT);
SendingSocket = socket(AF_INET, SOCK_STREAM, 0);
if (SendingSocket < 0)
error("ERROR opening socket");
server = gethostbyname(DEFAULT_ADDRESS);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
memset(&serv_addr, '0',sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(22041);
printf("got it\n");
in_addr_t in_addr = inet_addr("127.0.0.1");
if(INADDR_NONE == in_addr) {
perror("inet_addr() failed");
exit(0);
}
serv_addr.sin_addr.s_addr = in_addr;
if (connect(SendingSocket,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
error("ERROR connecting");
}
printf("socket is connected!");
}
/*
* END socket initialization
*/
Here is the error:
[asl@asl Bridge]$ ./socket
got it
./socket: Segmentation fault (core dumped)
Here is the output from GDB:
/home/asl/Desktop/backup-4-1-20/Bridge/socket:
Program received signal SIGSEGV, Segmentation fault.
strchrnul () at ../sysdeps/x86_64/strchrnul.S:34
34 movdqa (%rdi), %xmm0
I am currently in a CentOS 6.8 environment if that helps.
Why is this weird segfault coming from strchrnul? I can't find any information from it. I am using a fairly old OS due to certain reasons...is there a package or something that needs to be updated or is this related to my code?
EDIT: This looks like to be an issue with packages/something with my OS. This does not seem to be an issue with my code. I just ran this code and ran it successfully on my raspberry pi running raspbian. Anyone have any idea of what the issue within CentOS could be?
You are using error()
incorrectly. According to the man pages, the function is of the format void error(int status, int errnum, const char *format, ...);
, but you are just passing a string. Perhaps you intended to use the perror()
function?
Also, if it wasn't a copy error, you have an extraneous semi-colon:
int main(int argc, char* argv[];) {
^