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.
The last argument to
connect
should besizeof caddr
: