Here is an example of a basic socket program in C for macOS (formerly known as OS X):
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
int main(int argc, char *argv[]) {
// Create a socket
int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
printf("Could not create socket");
}
// Prepare the sockaddr_in structure
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr("127.0.0.1"); // server IP address
server.sin_family = AF_INET;
server.sin_port = htons(8888); // server port
// Connect to remote server
if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
printf("Connect error");
return 1;
}
// Send some data
char *message = "Hello server!";
if (send(socket_desc, message, strlen(message), 0) < 0) {
printf("Send failed");
return 1;
}
printf("Data sent\n");
return 0;
}
This program creates a TCP socket, connects to a remote server (in this case, at IP address 127.0.0.1 and port 8888), and sends a message to it. You can compile and run this program on macOS using the following commands:
$ gcc -o client client.c
$ ./client
Note that you will need a server program running at the specified IP address and port for this client to connect to.
Here is an example of a basic socket program in C for macOS (formerly known as OS X):
This program creates a TCP socket, connects to a remote server (in this case, at IP address 127.0.0.1 and port 8888), and sends a message to it. You can compile and run this program on macOS using the following commands:
Note that you will need a server program running at the specified IP address and port for this client to connect to.