0

In C, I am trying to connect to a server using the following function:

int clientConnect() {

//1. Set up connection address and port
char *ipAddress = SMSA_DEFAULT_IP;
int port = SMSA_DEFAULT_PORT;

//1a. Set up a sockaddr_in struct as described in lecture
struct sockaddr_in caddr;
caddr.sin_family = AF_INET;
caddr.sin_addr.s_addr = htonl(SMSA_DEFAULT_IP);
caddr.sin_port = htons( port );

//2. Create a socket
server_socket = socket(PF_INET, SOCK_STREAM, 0);

if(server_socket == -1)
{
    logMessage(1, "There was an error creating a socket in clientConnect()");
    return -1;
}

//3. Connect
if( connect(server_socket, (const struct sockaddr *)&caddr, sizeof(struct sockaddr) ) == -1)
{
    logMessage(1, "There was an error connecting in clientConnect()");\
    return -1;
}

logMessage(2, "The connection was made!");
return 0;}

When this function is called, I get a message in my log There was an error connecting in clientConnect(). This is my first time doing network programming with C so I suspect I am missing something.

Mike
  • 2,514
  • 2
  • 23
  • 37
  • 5
    Check the value of `errno` (or use `perror()`) if a system call (like `connect()`) fails. - There could be many reasons: Wrong IP address, server not responding, ... – Martin R Dec 08 '13 at 21:54
  • 3
    There's a moral here. When you get an error, log the *error,* not just some text of your own devising. Your own text can't tell you anything except that something went wrong. The actual error can. – user207421 Dec 08 '13 at 22:16
  • @Martin R Thank you for the errno tip, very helpful – Mike Dec 10 '13 at 20:47
  • 1
    @EJP You are very right! +1 A good lesson for a beginner C programmer to learn. Thank you both for you constructive comments – Mike Dec 10 '13 at 20:47

1 Answers1

0

The last argument to connect should be sizeof caddr:

connect(server_socket, (const struct sockaddr *)&caddr, sizeof caddr);
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • This is of course the correct code. But I *think* that `struct sockaddr` and `struct sockaddr_in` have the same size (at least on OS X), so this would not explain the error. - Perhaps I am wrong. – Martin R Dec 08 '13 at 22:41
  • @MartinR If this fixes the problem they must be different, QED. – user207421 Dec 09 '13 at 05:29
  • @MartinR There is no requirement for `sockaddr` and `sockaddr_in` to be of the same size, this is why socket addresses are always passed as a pair of `sockaddr*` and `socklen_t`. – Maxim Egorushkin Dec 09 '13 at 15:06
  • 1
    Yes, you are right, and I already acknowledged that your code is the correct way to do it. However (and I just verified it), on Linux, Solaris, OS X and AIX both struct sockaddr and struct sockaddr_in have a size of 16 bytes. Which means that your code is correct but (probably) does not solve the particular problem in the question. - Unfortunately, there is no feedback from the OP at all. – Martin R Dec 09 '13 at 15:18
  • 1
    @MartinR You are right, they still need for some silly reason `sockaddr` and `sockaddr_in` to be of the same size... Not sure how it handles `sockaddr_un` though... – Maxim Egorushkin Dec 09 '13 at 15:21