前言:局域网中启动一个服务器,客户端要想访问服务器要必须知道服务器的IP地址和端口号,在客户端登陆界面设计要输入端口号和IP,这样即麻烦,界面也不美观,通过互联网的上一个邮件可以解决这个问题,服务器启动以后,程序自动将服务器的IP和打开监听端口发到互联网上的一个邮箱,客户端启动后,客户端代码自动从那个邮箱读取服务器发上去的IP和端口号,客户端根据这个IP和端口号与服务器连接。
------------------------------------
smtp.c ------------------------------------
/*------------------------------------------------------------------------------------
注释: 通过程序对互联网上的一个邮箱进行操作,先删除里面的邮件,然后再发一封电子邮件向这个邮箱,内容是IP和PORT。客户端便可以通过邮箱从这个邮件读IP和端口号。
下面一条命令是向指定邮箱发一封邮件。
[root@localhost pop3]# echo a|mutt -s 'myqq:12.12.12.12:8000:' mailboxname@126.com
[root@localhost pop3]# gcc -o ser smtp.c
-------------------------------------------------------------------------------------*/
/* GNU GPLv2 only or any later version. */
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char *POPMessage[] = {
"USER mailboxname ",
"PASS 123456 ",
"UIDL ",
"DELE 1 ",
"QUIT ",
NULL
};
char buf[1024];
int i, n;
int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12;
int sockfd, new_fd, iLength;
struct sockaddr_in server_addr;
struct sockaddr_in servaddr;
//struct sockaddr_in client_addr;
//int sin_size;
int iMsg = 0;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(" socket error");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(110);
server_addr.sin_addr.s_addr = inet_addr("123.125.50.23");
bzero(&(server_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
perror(" connect error");
exit(1);
}
iLength = recv(sockfd, buf, sizeof(buf), 0);
buf[iLength] = ' ';
printf("received:%s ", buf);
do {
send(sockfd, POPMessage[iMsg], strlen(POPMessage[iMsg]), 0);
printf("发送信息:%s", POPMessage[iMsg]);
memset(buf, ' ', 1024);
iLength = recv(sockfd, buf, sizeof(buf), 0);
buf[iLength] = ' ';
if (iMsg == 2) {
if (strncmp(buf, "+OK 0 0", strlen("+OK 0 0")) == 0)
break;
}
iMsg++;
printf("接收信息:%s ", buf);
} while (POPMessage[iMsg]);
close(sockfd);
char kkk[80] = "echo aaa|mutt -s 'myqq:127.123.123.123:8000:' mailboxname@126.com";
system(kkk);
}
/*------------------------------------------------------------------------------------
执行过程如下:
[root@localhost pop3]# ./ser
received:+OK Welcome to coremail Mail Pop3 Server (126coms[f7aa0ed0270c139a0807240dd700906bs])
发送信息:USER mailboxname
接收信息:+OK core mail
发送信息:PASS 123456
接收信息:+OK 1 message(s) [987 byte(s)]
发送信息:UIDL
接收信息:+OK 1 987
1 1tbi2x+vsEr07KvcQgAAs8
.
发送信息:DELE 1
接收信息:+OK core mail
发送信息:QUIT
接收信息:+OK core mail
[root@localhost pop3]#
-------------------------------------------------------------------------------------*/
------------------------------------
pop3.c ------------------------------------
/*-------------------------------------------------------------------------------------------
注释: 通过程序对互联网上的一个邮箱进行操作,读取邮箱里面的内容,本例读的内容是邮件标题中的IP和PORT
[root@localhost pop3]# gcc -o pop3 pop3.c
--------------------------------------------------------------------------------------------*/
/* GNU GPLv2 only or any later version. */
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char *POPMessage[] = {
"USER mailboxname ",
"PASS 123456 ",
NULL
};
char buf[5000];
int i, n;
int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12;
int sockfd, new_fd, iLength;
struct sockaddr_in server_addr;
struct sockaddr_in servaddr;
//struct sockaddr_in client_addr;
//int sin_size;
int iMsg = 0;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror(" socket error");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(110);
server_addr.sin_addr.s_addr = inet_addr("123.125.50.23");
bzero(&(server_addr.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) {
perror(" connect error");
exit(1);
}
iLength = recv(sockfd, buf, sizeof(buf), 0);
buf[iLength] = ' ';
printf("received:%s ", buf);
do {
send(sockfd, POPMessage[iMsg], strlen(POPMessage[iMsg]), 0);
printf("发送帐号:%s", POPMessage[iMsg]);
memset(buf, ' ', 1024);
iLength = recv(sockfd, buf, sizeof(buf), 0);
buf[iLength] = ' ';
iMsg++;
printf("接收信息:%s ", buf);
} while (POPMessage[iMsg]);
send(sockfd, "RETR 1 ", strlen("RETR 1 "), 0);
memset(buf, ' ', 5000);
//开始接收邮件
num1 = num2 = num3 = 0;
n = 0;
int k = 0, j, x = 0;
char a[2], *token, b = 20;
for (i = 0; i < 5000; i++) {
n++;
recv(sockfd, a, 1, 0);
if (strcmp(a, "S") == 0)
num1 = n;
if (strcmp(a, "u") == 0)
num2 = n;
if (strcmp(a, "b") == 0)
num3 = n;
if (strcmp(a, "j") == 0)
num4 = n;
if (strcmp(a, "e") == 0)
num5 = n;
if (strcmp(a, "c") == 0)
num6 = n;
if (strcmp(a, "t") == 0)
num7 = n;
if (strcmp(a, ":") == 0)
num8 = n;
if (strcmp(a, "m") == 0)
num9 = n;
if (strcmp(a, "y") == 0)
num10 = n;
if (strcmp(a, "q") == 0)
num11 = n;
if (strcmp(a, "q") == 0)
num12 = n;
if (((num12 - num1) == 11) && ((num10 - num9) == 1) && ((num8 - num7) == 1) && ((num6 - num5) == 1) && ((num4 - num3) == 1) && ((num2 - num1) == 1)) {
for (j = 0; j < 45; j++) {
recv(sockfd, a, 1, 0);
strcat(buf, a);
x = 1;
}
}
if (x)
break;
}
token = strtok(buf, ":");
token = strtok(NULL, ":");
printf("想要的ip是: %s ", token);
token = strtok(NULL, ":");
printf("想要的port是: %s ", token);
close(sockfd);
}
/*------------------------------------------------------------------------------------
执行过程如下:
[root@localhost pop3]# ./pop3
received:+OK Welcome to coremail Mail Pop3 Server (126coms[f7aa0ed0270c139a0807240dd700906bs])
发送帐号:USER mailboxname
接收信息:+OK core mail
发送帐号:PASS 123456
接收信息:+OK 1 message(s) [989 byte(s)]
想要的ip是:
127.123.123.123
想要的port是:
8000
[root@localhost pop3]#
-------------------------------------------------------------------------------------*/