标签:des style blog http color os io ar for
简单中转代码:
1 #include "stdafx.h" 2 #include <stdio.h>; 3 #include <stdlib.h>; 4 #include <Winsock2.h>; 5 6 #pragma comment(lib, "ws2_32.lib") 7 8 #define PORT 143 9 #define RECORD "d:\\record.txt" 10 #define SERVER "imap.126.com" 11 12 int write(const char *buf, int &len) 13 { 14 char path[MAX_PATH]; 15 GetCurrentDirectory(MAX_PATH, path); 16 17 strcat(path, "\\data.txt"); 18 19 FILE *fp; 20 fp = fopen(path, "a"); 21 fwrite(buf, len, 1, fp); 22 fclose(fp); 23 24 return 0; 25 } 26 int DoProxy(int *fd ) 27 { 28 struct sockaddr_in rout; 29 int outfd,maxfd; 30 int count = 65535; 31 int n; 32 fd_set set; 33 char buf[65535]; 34 WSADATA wsaData; 35 int infd; 36 infd = *fd; 37 if(WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) 38 { 39 printf("WSAStartup Error\n"); 40 return 1; 41 } 42 rout.sin_family = AF_INET; 43 rout.sin_port = htons(PORT); 44 /* rout.sin_addr.s_addr = inet_addr("192.168.8.100");//发数据转发给192.168.8.100*/ 45 HOSTENT * hp = gethostbyname(SERVER); 46 memcpy((char *)&rout.sin_addr, (char*)hp->h_addr_list[0], hp->h_length); 47 48 if( (outfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 49 { 50 printf("socket error"); 51 return 1; 52 } 53 if( connect(outfd, (struct sockaddr *)&rout, sizeof(rout)) < 0 ) 54 { 55 printf("connect error"); 56 return 1; 57 } 58 maxfd = __max(outfd, infd); 59 while(1) 60 { 61 FD_ZERO(&set); 62 FD_SET((unsigned int)outfd, &set); 63 FD_SET((unsigned int)infd, &set); 64 if( select(maxfd + 1,&set, NULL, NULL, 0) < 0 ) 65 { 66 printf("select errorErrorcode :%d ",WSAGetLastError()); 67 return 1; 68 } 69 if( FD_ISSET(infd, &set) )//客户端有数据到来 70 { 71 memset(buf, 0, count); 72 n = recv(infd,buf,count,0); 73 if(n <= 0) 74 break; 75 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED); 76 printf("%s", buf); 77 write(buf, n); 78 if( send(outfd, (const char *)buf, n,0) != n ) 79 { 80 printf("Write Error\n"); 81 continue; 82 } 83 84 85 } 86 if( FD_ISSET(outfd, &set) )//服务端有数据到来 87 { 88 memset(buf, 0, count); 89 n = recv(outfd, (char *)buf, count,0); 90 if( n <= 0) 91 break; 92 write(buf, n); 93 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN); 94 printf("%s", buf); 95 if( send(infd, (const char *)buf, n,0) != n ) 96 { 97 printf("write error"); 98 continue; 99 } 100 101 102 } 103 }//end while 104 closesocket(infd); 105 closesocket(outfd); 106 return 0; 107 } 108 int main(int argc, char **argv) 109 { 110 111 struct sockaddr_in servaddr, clientaddr; 112 int listenfd, connfd; 113 int clientlen = 0; 114 WSADATA wsaData; 115 int ServerPort; 116 HANDLE hThread; 117 ServerPort = PORT; 118 if(WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) 119 { 120 printf("WSAStartup Error\n"); 121 return 1; 122 } 123 servaddr.sin_family = AF_INET; 124 servaddr.sin_port = htons(ServerPort); 125 servaddr.sin_addr.s_addr = INADDR_ANY; 126 listenfd = socket(AF_INET, SOCK_STREAM, 0); 127 if(listenfd < 0) 128 { 129 printf("listenfd Error\n"); 130 return 1; 131 } 132 if( bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) 133 { 134 printf("Server Bind Port[%d] Error\n",ServerPort); 135 return 1; 136 } 137 if( listen(listenfd, SOMAXCONN) < 0 ) 138 { 139 printf("listen Error\n"); 140 return 1; 141 } 142 for(;;) 143 { 144 clientlen = sizeof(struct sockaddr); 145 connfd = accept( listenfd, 146 (struct sockaddr *)&clientaddr, 147 &clientlen ); 148 if(connfd < 0) 149 { 150 printf("accept error.errcode:%d\n",WSAGetLastError()); 151 continue; 152 } 153 hThread = CreateThread( 154 NULL, 155 0, 156 (LPTHREAD_START_ROUTINE)DoProxy, 157 &connfd, 158 0, 159 NULL); 160 161 } 162 return 0; 163 }
标签:des style blog http color os io ar for
原文地址:http://www.cnblogs.com/91Kesson/p/3958069.html