//for socket(), send() etc.
#include //for sockaddr_in
#include //for inet_aton()
#include //for close()
char send_bufer[1024] = "Message from client to server";
char rcv_bufer[1024];
main() {
int sd;
struct sockaddr_in addr_p;
sd = socket(AF_INET, SOCK_STREAM, 0);
addr_p.sin_family = AF_INET;
addr_p.sin_port = htons(80);
addr_p.sin_addr.s_addr = inet_aton("50.16.8.1", &addr_p.sin_addr);
connect(sd, (struct sockaddr*) &addr_p, sizeof(addr_p));
int send_bytes = send(sd, rcv_bufer, sizeof(rcv_bufer), 0);
int rcv_bytes = recv(sd, send_bufer, sizeof(send_bufer), 0);
close(sd);
}
UDP Server Example
#include //for socket(), send() etc.
#include //for sockaddr_in
#include //for close()
char rcv_bufer[1024];
main() {
int sd;
struct sockaddr_in addr_p, client_addr_p;
sd = socket(AF_INET, SOCK_DGRAM, 0);
addr_p.sin_family = AF_INET;
addr_p.sin_port = htons(80);
addr_p.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sd, (struct sockaddr*) &addr_p, sizeof(addr_p));
int len = sizeof(client_addr_p);
int send_bytes = recvfrom(sd, rcv_bufer, sizeof(rcv_bufer), 0,
(struct sockaddr*)&client_addr_p, &len);
close(sd);
}
UDP Client Example
#include //for socket(), send() etc.
#include //for sockaddr_in
#include //for inet_aton()
#include //for close()
char send_bufer[1024] = "Message from client to server";
main() {
int sd;
struct sockaddr_in addr_p;
sd = socket(AF_INET, SOCK_DGRAM, 0);
addr_p.sin_family = AF_INET;
addr_p.sin_port = htons(80);
addr_p.sin_addr.s_addr = inet_aton("50.16.8.1", &addr_p.sin_addr);
int recvd_bytes = sendto(sd, send_bufer, sizeof(send_bufer), 0,
(struct sockaddr*) &addr_p, sizeof(addr_p));
close(sd);
}