How to send email via postfix using c sockets? - c

How to send email via postfix using c sockets?
How to create message proggramly
struct sockaddr_in addr;
char message[] = "MAIL From: vladyslav#vladyslav-virtual-machine\n \"vladyslav#vladyslav-virtual-machine\"\n\"Test mail\"\n\"This is a test email\"";
char buf[512];
//creating socket
int sock = socket(AF_INET, SOCK_DGRAM , 0);
//address parameters
addr.sin_family = AF_INET;
//connection port
addr.sin_port = htons(8);
// Inet 127.0.0.1.
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sendto(sock, message, sizeof(message), 0, (struct sockaddr *)&addr, sizeof(addr));
close(sock);

found solution
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int main(int argc, char** argv)
{
int i;
struct sockaddr_in addr;
//commands for server
char* commands[] = {"eclo localhost\n", "mail from:test#example.com\n", "rcpt to:vladyslav#vladyslav-virtual-machine\n", "data\n", "Subject: Тест\n\nТест\n", "\n.\n", "quit\n"};
//creating socket
int sock = socket(AF_INET, SOCK_STREAM , 0);
if(sock < 0)
{
perror("error with creation of socket");
return -errno;
}
//parameters
addr.sin_family = AF_INET;
//port 25
addr.sin_port = htons(25);
// Inet 127.0.0.1.
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
//connecting to server
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
{
perror("error with creation of socket");
return -errno;
}
//sending commands
for(i = 0; i < sizeof(commands) / 4; i++)
send(sock, commands[i], strlen(commands[i]), 0);
//closing connection
close(sock);
return (EXIT_SUCCESS);
}

Related

Socket Program in C++ to send a message back and forth between server and client

I am Trying to make a C/C++ Project using Socket Programming.
For this I need Client and Server to sent a index of array back and forth to each other.
So Client will next a index of array and server will sent another index of array until the game is over. (Yeah we are trying to make a game.)
I have written (copied from gfg) code for it but it's not working properly.
The Server for some reason didn't receive the second message. So, We kind of stuck in a deadlock.
Any idea what I did wrong??
Server.cpp
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *msg = "0";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
int i=0;
while(i<10)
{
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , msg , strlen(msg) , 0 );
printf("Message sent\n");
i++;
}
return 0;
}
Client.cpp
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *msg="0";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
int i=0;
while(i<10)
{
send(sock , msg , strlen(msg) , 0 );
printf("Message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
i++;
}
return 0;
}
Edit: It will be really nice if somebody can tell me how to sent an integer instead of a string.

sockets programming: sending and receiving different data to different clients in C

I have written a basic client server code in c socket programming using the TCP/IP protocol but i cant figure out how to make it connect to different clients and send/receive different data to and from them as a function to the client (meaning if its the first client send him that data and if its that client send him the other data) and so on.
This is the only results i have found were sending the same data to different clients.
Current Server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
int main() {
char server_message[100] = {0};
int server_socket = 0;
int client_socket = 0;
struct sockaddr_in server_address;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
// bind the socket to our specified IP and port
bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address));
listen(server_socket, 2);
client_socket = accept(server_socket, NULL, NULL);
printf("Please enter a massage:");
fgets(server_message, 100, stdin);
send(client_socket, server_message, sizeof(server_message), 0);
close(server_socket);
return 0;
}
By using original code from geeksforgeeks and Myst comment we can solve it.
You have one server that serves on local host 127.0.0.1, and can have multiple clients for this example i assume 5 clients are enough.
Run server once, and run many client to connect seprately to that server.
Server.c
// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
#define STRING_SIZE 100
#define BUFFER_SIZE 100
int main(int argc, char const *argv[])
{
int server_fd, new_socket[5], valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
for (int i=0;i<5;i++){
if ((new_socket[i] = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket[i], buffer, 1024);
printf("%s\n", buffer);
char send_buf[STRING_SIZE] = "hello";
char buf[BUFFER_SIZE]={0};
sprintf(buf, "%d", i);
strcat(send_buf, buf);
send(new_socket[i], send_buf, strlen(send_buf), 0);
//printf("Hello message sent\n");
}
return 0;
}
Client.c
// Client side C/C++ program to demonstrate Socket programming
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
//printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n", buffer);
return 0;
}
Run
After compiling codes with gcc client.c -o client and gcc server.c -o server
Open one terminal for server and start server by run ./server.
Now you can connect many client [up to 5] to it by running ./client.

Socket programming in c connection failed

Trying to practice socket programming in c. Been hitting an error of Connection Failedbut not sure why. I included both the server and client code. I believe the problem is with connect.
Also, i run the client first then run the server. Is that the right way to compile it?
//CLIENT
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
valread = read( sock , buffer, 1024);
printf("%s\n",buffer );
return 0;
}
//SERVER
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char *hello = "Hello from server";
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
valread = read( new_socket , buffer, 1024);
printf("%s\n",buffer );
send(new_socket , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return 0;
}

Problem sending msg to UDPClient on windows from UDPServer on linux

I have the UDPServer program running on linux and UDPClient program running on windows 10.
Note: In the UDP client program you have to add the linux server IP in the hash define.
Compilation on windows: gcc -Wall -o UDPClient UDPClient.c -lws2_32
Problem
UDPServer receives msg from UDP client and replies back. But the UDPClient on windows doesnt receive the msg from windows.
UDPServer.c (on linux)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 6200
#define MAXLINE 1024
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
int n;
socklen_t from_len = sizeof(struct sockaddr);
socklen_t to_len = sizeof(struct sockaddr);
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
//servaddr.sin_addr.s_addr = inet_addr(IP_ADDR); ;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, &from_len);
buffer[n] = '\0';
printf("Client : %s ; IP = %s ; Port = %d\n", buffer,inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
sendto(sockfd, (const char *)hello, strlen(hello), MSG_CONFIRM, (const struct sockaddr *) &cliaddr, to_len);
printf("Hello message sent.\n");
return 0;
}
// UDP client (on windows)
#include<stdio.h>
#include<winsock2.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#define PORT 6200
#define IP_ADDR "xx.xx.xx.xx" //Add the linux server IP
#define BUF_SIZE 1024
/* Compilation: gcc -Wall -o UDPClient UDPClient.c -lws2_32 */
int SetupWinSock()
{
WSADATA wsa;
//printf("SetupWinSock: Initializing socket system\n");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("SetupWinSock: Failed. Error Code : %d",WSAGetLastError());
return -1;
}
//printf("SetupWinSock: Initialization complete\n");
return 0;
}
SOCKET CreateUDPSocket()
{
SOCKET udpSocket;
if((udpSocket = socket(AF_INET , SOCK_DGRAM , IPPROTO_UDP )) == INVALID_SOCKET)
{
printf("CreateUDPSocket: Failed to create socket : %d" , WSAGetLastError());
return -1;
}
else
{
//printf("CreateUDPSocket: Socket creation successful\n");
}
return udpSocket;
}
int main() {
char buffer[BUF_SIZE];
char *hello = "Hello from client";
struct sockaddr_in servaddr, localAddr;
SOCKET udpSocket;
int n, len;
SetupWinSock();
// Creating socket file descriptor
udpSocket = CreateUDPSocket();
memset(&servaddr, 0, sizeof(servaddr));
// Server info
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = inet_addr(IP_ADDR);
// Client info
localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(PORT);
localAddr.sin_addr.s_addr = INADDR_ANY;
if ( bind(udpSocket,(const struct sockaddr *)&localAddr, sizeof(localAddr)) < 0 )
{
printf("Failed to bind\n");
return -1;
}
sendto(udpSocket, (const char *)hello, strlen(hello), 0, (const struct sockaddr *) &servaddr, sizeof(servaddr));
printf("Hello message sent.\n");
n = recvfrom(udpSocket, (char *)buffer, BUF_SIZE, 0, (struct sockaddr *) &servaddr, &len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
fflush(stdout);
closesocket(udpSocket);
WSACleanup();
while(1);
return 0;
}

How to connect to a server using only its IP?

I'm kind of new to sockets. So I setup a server and I want to connect a client to it through the internet. I don't understand what I'm doing wrong because the following example works with the loopback address (127.0.0.1) but not when someone else tries to connect to my server using my external IP address. Is it just not that simple or am I doing something wrong in my code?
Edit: (The client specifically gets stuck when it reaches connect() giving a connect: Connection timed out error)
Edit2: I tried using my broadcast address inet_addr("192.168.1.255") instead of INADDR_ANY in the server code. No change.
This is my server code:
#include <stdio.h>
#include <stdlib.h\
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(void) {
int temp;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
int numbytes;
char buf[100];
int sock_fd; // server file descriptor
int new_fd; // client file descriptor
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_fd == -1) { perror("socket"); exit(1); }
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(9000);
my_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(my_addr.sin_zero), 8);
temp = bind(sock_fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr));
if (temp == -1) { perror("bind"); exit(1); }
temp = listen(sock_fd, 5);
if (temp == -1) { perror("listen"); exit(1); }
printf("\nListening for new connections on port %d ...\n\n", 9000);
while (1) {
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sock_fd, (struct sockaddr*)&their_addr, &sin_size);
if (new_fd == -1) { perror("accept"); continue; }
printf("Got connection from IP (%s)\n", inet_ntoa(their_addr.sin_addr));
close(new_fd);
}
}
This is my client code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
int main(int argc, char *argv[]) {
int temp;
struct hostent *host_info;
struct sockaddr_in serv_addr;
char ip_addr[100];
int numbytes;
char buf[100];
int sock_fd;
inet_pton(AF_INET, argv[5], &serv_addr.sin_addr);
host_info = gethostbyaddr(&serv_addr.sin_addr, sizeof serv_addr.sin_addr, AF_INET);
if (host_info == NULL) { herror("gethostbyname"); exit(1); }
printf("\nServer: '%s' (IP: %s , Port: %d)\n\n", argv[1], inet_ntop(AF_INET, host_info->h_addr, ip_addr, 100), 9000);
sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock_fd == -1) { perror("socket"); exit(1); }
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9000);
serv_addr.sin_addr = *((struct in_addr *)host_info->h_addr);
bzero(&(serv_addr.sin_zero), 8);
temp = connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr));
if (temp == -1) { perror("connect"); exit(1); }
close(sock_fd);
exit(0);
}
Posting an answer here so I can accept my overall solution. The problem was that I had not forwarded a port on my router as #AdamRosenfield indicated, then after I did that I changed my server's address to:
my_addr.sin_addr.s_addr = inet_addr("192.168.1.2");
which is MY local IP address according to ifconfig (I am using Ubuntu).
Now my only problem is that anyone else can connect to my server except me from my own pc, but that might be a problem specific to my router according to #nos.
If anyone has an answer on how to fix that please update me (I tried connecting a client using the loopback address, my external IP, my local IP and a few more, nothing worked).
the client.c should be as below
int main(int argc, char *argv[]){
int temp;
struct hostent *host_info;
struct sockaddr_in serv_addr;
char ip_addr[100];
int numbytes;
char buf[100];
int sock_fd;
unsigned long inaddr;
//inet_pton(AF_INET, argv[5], &serv_addr.sin_addr);
//host_info = gethostbyaddr(&serv_addr.sin_addr, sizeof serv_addr.sin_addr, AF_INET);
//if (host_info == NULL) { herror("gethostbyname"); exit(1); }
printf("\nServer: '%s' (IP: %s , Port: %d)\n\n", argv[0], argv[1], 9000);
inaddr = inet_addr(argv[1]);
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd == -1) { perror("socket"); exit(1); }
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(9000);
memcpy(&serv_addr.sin_addr, &inaddr, sizeof(inaddr));
//bzero(&(serv_addr.sin_zero), 8);
temp = connect(sock_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr));
if (temp == -1) { perror("connect"); exit(1); }
close(sock_fd);
exit(0);
}
then use g++ compile it:g++ client.c -o client.
use client like this:./client yourserverIP
when set the server address in client code, you can do like this:
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//your server's ip address

Resources