Hi I have been reading that guide about linux sockets and I have tried to make a simple program that opens a connection, sends a test message and then closes the connection. Problem is it doesn't want to connect, no errors or anything it just displays the "Could not connect" message. Here is my code:
Code:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
int sockfd;
int port;
struct sockaddr_in their_address;
/* They forgot to use some arguments */
if (argc != 3)
{
printf("Usage: client ip port\n");
exit(1);
}
/* Set the IP address from the argument they gave */
if (their_address.sin_addr.s_addr = inet_addr(argv[1]) == -1)
{
printf("Think the I.P. address is iffy :o\n");
exit(1);
}
/* Start the socket file descriptor */
if (sockfd = socket(AF_INET, SOCK_STREAM, 0) == -1)
{
printf("Couldn't open the socket!\n");
exit(1);
}
/* Set the port from the argument */
sscanf(argv[2], "%d", &port);
their_address.sin_port = htons(port);
/* Sort out the rest of the structure */
their_address.sin_family = AF_INET;
memset(&(their_address.sin_zero), '\0', 8);
/* Open the conection to the host */
if (connect(sockfd, (struct sockaddr *)&their_address, sizeof(struct sockaddr)) == -1)
{
printf("Could not connect\n");
exit(1);
}
/* Send a simple test message */
send(sockfd, "test", 4, 0);
/* Tidy up before we go */
close(sockfd);
}