中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual C++ > Internet
一个简单的Proxy代理服务器的源代码!
作者:wxyxl 时间:2001-10-09 10:09 出处:互联网 责编:chinaitpower
              摘要:一个简单的Proxy代理服务器的源代码!

 

#include <windows.h>
#include <stdio.h>

#define PROXY_IP "xxx.xxx.xxx.xxx"
#define PROXY_PORT 1080

#define DEST_IP "xxx.xxx.xxx.xxx"
#define DEST_PORT 8888

#define LOCAL_IP "xxx.xxx.xxx.xxx"
#define LOCAL_PORT 6666

int main()
{   
    int fd, fd_udp;   
    struct sockaddr_in name;
    WSADATA wsaData;   
    char buf[100];   
    int len;
    int i;
    if(WSAStartup(MAKEWORD( 2, 2 ), &wsaData ))       
        return 1;
    if((fd_udp = socket(AF_INET, SOCK_DGRAM, 0)) == -1)       
      return 1;   
    if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)       
      return 1;   
    memset(&name, 0, sizeof(name));   
    name.sin_family = AF_INET;   
    name.sin_addr.s_addr = inet_addr(PROXY_IP);   
    name.sin_port = htons(PROXY_PORT);   
    if(connect(fd, (struct sockaddr*)&name, sizeof(name)) != 0)       
        return 1;   
    buf[0] = 5;   
    buf[1] = 1;   
    buf[2] = 0;   
    send(fd, buf, 3, 0);   
    recv(fd, buf, 2, 0);   
    if(buf[0] != 5 || buf[1] != 0)       
        return 1;
    buf[0] = 5; /* protocol version */ 
    buf[1] = 3; /* command UDP associate */   
    buf[2] = 0; /* reserved */   
    buf[3] = 1; /* address type IP v4 */   
    len = sizeof(name);
    memset(&name, 0, sizeof(name));
    name.sin_family = AF_INET;   
    name.sin_addr.s_addr = inet_addr(LOCAL_IP);   
    name.sin_port = htons(LOCAL_PORT);
    bind(fd_udp,(struct sockaddr *)&name,len);
    *(unsigned int*)&buf[4] = inet_addr(LOCAL_IP);//name.sin_addr.s_addr;   
    *(unsigned short*)&buf[8] = htons(LOCAL_PORT);   
    send(fd, buf, 10, 0);   
    recv(fd, buf, 10, 0);   
    if(buf[0] != 5)       
      return 11;   
    memset(&name, 0, sizeof(name));   
    name.sin_family = AF_INET;   
    name.sin_addr.s_addr = *(int*)&buf[4];   
    name.sin_port = (*(short*)&buf[8]);   
    connect(fd_udp, (struct sockaddr *)&name, sizeof(name));   
    for(i = 0; i <100; i++)
    {
        buf[0] = 0; /* reserved */       
        buf[1] = 0; /* reserved */
        buf[2] = 0; /* standalone packet */       
        buf[3] = 1; /* address type IP v4 */       
        *(unsigned long*)&buf[4] = inet_addr(DEST_IP);       
        *(unsigned short*)&buf[8] = htons(DEST_PORT);       
        *(unsigned int*)&buf[10] = i;       
        send(fd_udp, buf, 14, 0);       
        recv(fd_udp, buf, 14, 0);       
        printf("udp received: %d\n", *(int*)&buf[10]);   
    }   
    closesocket(fd_udp);   
    closesocket(fd);   
    WSACleanup();   
    return 0;
}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有