Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // printf
- #include <stdlib.h> // exit
- #include <unistd.h> // close
- #include <errno.h> // errno
- #include <string.h> // strerror
- #include <netinet/in.h> // sockaddr_in
- #include <arpa/inet.h> // inet_ntop
- #include <netdb.h> // gethostbyname
- // Useful constants
- #define SERVER_HOSTNAME "localhost"
- #define SERVER_PORT 4567
- #define DATA_BUFFER_SIZE 256
- int main() {
- printf("Starting TCP Client\n\n");
- // Create the socket on which we will communicate PF_INET -> Internet Protocol
- // Family, SOCK_STREAM -> TCP, Last arg is for socket flags, which we do not
- // set.
- // A socket is an abstract communication device within the kernel and,
- // much like a file, the integer descriptor used by the process is simply an
- // index to the kernel's socket object that is associated with this process
- // as state in the Process Control Block.
- int socket_descriptor = socket(PF_INET, SOCK_STREAM, 0);
- if(socket_descriptor < 0){
- perror("Socket: ");
- exit(0);
- }
- // This will hold the socket address of the server to which we will connect.
- // This will include the IP address and port, as defined in the IP version 4.
- struct sockaddr_in server_address;
- // Set up the internet address structure.
- server_address.sin_family = AF_INET; // Internet (IPv4) address family
- // Since the network protocol does not necessarily use the same byte endianess
- // as the host CPU, htons() ensures it is converted correctly.
- server_address.sin_port = htons(SERVER_PORT); // host-to-network-short
- // Resolve the hostname to an IP address, ultimately via a DNS look-up over
- // the network if it cannot be satisfied from the local cache.
- struct hostent *hostinfo;
- hostinfo = gethostbyname(SERVER_HOSTNAME);
- if (hostinfo == NULL) {
- fprintf(stderr, "Unknown host %s.\n", SERVER_HOSTNAME);
- exit(1);
- }
- // Set the address to that returned from the lookup. Note the pointer
- // manipulation that is happening here: we cast the h_addr pointer to a
- // pointer to an in_addr (for holding IPv4 addresses), then we get the value
- // pointed to and store it in sin_addr, so the address has been copied.
- server_address.sin_addr = *(struct in_addr *) hostinfo->h_addr;
- // Connect the socket to the destination host.
- int result = connect(
- socket_descriptor, // The socket we are using to communicate with
- (struct sockaddr*) &server_address, // The server's IPv4 address cast to
- // a generic address.
- sizeof(struct sockaddr_in)); // The size of our address.
- // Check if connect failed.
- if (result != 0) {
- perror("Could not connect socket.\n");
- exit(1);
- }
- // Send some data.
- char* message = "Hello from the Client";
- int no_bytes_sent = send(
- socket_descriptor,
- message,
- (size_t) strlen(message)+1, // Plus one to include terminaing zero.
- 0); // We don't use the flags.
- // Check that we sent what we expeected to send.
- if (no_bytes_sent != strlen(message)+1) {
- perror("ERROR: Failed to send data\n");
- exit(1);
- }
- // Read the server's result.
- char incoming_data[DATA_BUFFER_SIZE];
- int no_incoming_bytes = recv(
- socket_descriptor,
- &incoming_data,
- DATA_BUFFER_SIZE,
- 0); // Again, we don't use the flags.
- // Check we received >= 0 bytes.
- if (no_incoming_bytes < 0) {
- perror("Receive error\n");
- exit(1);
- }
- printf("Received: '%s'\n", incoming_data);
- // close the Socket.
- close(socket_descriptor);
- // Did we forget to free anything?
- // - I'll leave that as an exercise ;)
- return 0;
- }
Add Comment
Please, Sign In to add comment