sockets - How to connect to mail server in C -
i tried make connection mail server in local area network. ip of mail server 192.168.1.1. so, tried following program test that.
program:
#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> int main() { struct sockaddr_in sa; struct in_addr ip; int fd=socket(af_inet,sock_stream,0); if(inet_pton(af_inet,"192.168.1.1",&ip)==-1){ printf("unable convert ip binary\n"); perror(""); exit(1); } sa.sin_family=af_inet; sa.sin_port=25; sa.sin_addr=ip; if(connect(fd,(struct sockaddr*)&sa,sizeof(sa))==-1){ printf("unable connect server\n"); perror(""); exit(1); } else{ printf("successfully connected server...\n"); } }
output:
$ ./a.out unable connect server connection refused $
but via telnet, connected shown below.
$ telnet 192.168.1.1 25 trying 192.168.1.1... connected 192.168.1.1. escape character '^]'. 220 mail.msys.co.in esmtp postfix (debian/gnu) ^] telnet> connection closed. $
so, mistake done here. there wrong in program. request me solve problem , why occurs.
with wireshark trace can see code tries connect port 6400. try:
sa.sin_port=htons(25);
Comments
Post a Comment