sockaddr_in causing segfault?

Posted by Curlystraw on Stack Overflow See other posts from Stack Overflow or by Curlystraw
Published on 2011-01-11T19:49:06Z Indexed on 2011/01/11 19:53 UTC
Read the original article Hit count: 158

Filed under:
|
|

Working on creating a server/client system in C right now, and I'm having a little trouble with the client part. From what I've seen, I need to use sockaddr_in so I can connect to the server. However, I've been getting a segfault every time. I believe that sockaddr_in has something to do with it, as comment it and it's references later in the program fixes the segfault.

code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>

int main(int argc, char** argv)
{
int Csock;
int con;
char *data = 0;
char buf[101] = "";
struct sockaddr_in addr;

Csock = socket(AF_INET, SOCK_STREAM, 0);

addr.sin_family = AF_INET;
addr.sin_port = htons(3435);

con = connect(Csock, (struct sockaddr*) &addr, sizeof(&addr));

write(con, "Text", sizeof("Text"));
*data = read(con, buf, 100);
puts(data);
return 0;
}

sadly, I am rather new to C, so that's as much as I can figure... can anyone tell me a way to go about eliminating the segfault?

Thanks!

© Stack Overflow or respective owner

Related posts about c

    Related posts about sockets