资源简介
VC++端口转发程序源码
代码片段和文件信息
// MPortServer.cpp : Defines the entry point for the console application.
//
#include “stdafx.h“
#include
#include
#pragma comment(lib “ws2_32.lib“)
#define MAXCONN 100
int g_nAcceptCount = 0;
typedef struct _SOCKINFO
{
int MapPort;
char MapIp[50];
SOCKET LisSocket;
}SOCKINFO*PSOCKINFO;
typedef struct _SOCKS
{
SOCKET MapSocket;
SOCKET LisSocket;
}SOCKS*PSOCKS;
bool InitSocket()
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(22) &wsaData))
{
printf(“ERROR:WSAStartup Error.\r\n“);
return false;
}
return true;
}
bool SendData(SOCKET s char *data int len)
{
char * p = data;
int i = 0;
int k = len;
int ret = 0;
if(len <= 0) return TRUE;
while(1)
{
ret = send(spk0);
if(ret == 0 || ret == SOCKET_ERROR)
{
return FALSE;
}
i += ret;
p += ret;
k -= ret;
if(i >= len) break;
}
return TRUE;
}
DWORD WINAPI TranslateData(LPVOID pvParam)
{
SOCKS *LisAndMapSocket = (SOCKS*)pvParam;
FD_SET fdread;
FD_ZERO(&fdread);
FD_SET(LisAndMapSocket->LisSocket &fdread);
FD_SET(LisAndMapSocket->MapSocket &fdread);
char buff[8192];
int nRecvnSend;
while(select(0 &fdread NULL NULL NULL) != SOCKET_ERROR)
{
ZeroMemory(buff sizeof(buff));
if (FD_ISSET(LisAndMapSocket->LisSocket &fdread))
{
nRecv = recv(LisAndMapSocket->LisSocket buff sizeof(buff) 0);
nSend = SendData(LisAndMapSocket->MapSocket buff nRecv);
if (nRecv == SOCKET_ERROR || nSend == false)
{
printf(“ERROR:Translate listen socket data error.\r\n“);
break;
}
}
else if (FD_ISSET(LisAndMapSocket->MapSocket &fdread))
{
nRecv = recv(LisAndMapSocket->MapSocket buff sizeof(buff) 0);
nSend = SendData(LisAndMapSocket->LisSocket buff nRecv);
if (nRecv == SOCKET_ERROR || nSend == false)
{
printf(“ERROR:Translate map socket data error.\r\n“);
break;
}
}
FD_ZERO(&fdread);
FD_SET(LisAndMapSocket->LisSocket &fdread);
FD_SET(LisAndMapSocket->MapSocket &fdread);
}
closesocket(LisAndMapSocket->MapSocket);
closesocket(LisAndMapSocket->LisSocket);
g_nAcceptCount--;
return 0;
}
DWORD LisAccept(SOCKINFO si)
{
DWORD dwResult = 0;
SOCKS LisAndMapSocket; //连接成功socket和监听socket
SOCKINFO sockinfo = si;
sockaddr_in sinmap;
sinmap.sin_family = AF_INET;
sinmap.sin_port = htons(sockinfo.MapPort);
sinmap.sin_addr.S_un.S_addr = inet_addr(sockinfo.MapIp);
//如果监听socket没有关闭,则一直accept
while(sockinfo.LisSocket != INVALID_SOCKET)
{
SOCKET mapSocket = socket(AF_INET SOCK_STREAM IPPROTO_TCP);
SOCKET sAccept = accept(sockinfo.LisSocket NULL NULL);
if (sAccept == INVALID_SOCKET)
break;
LisAndMapSocket.LisSocket = sAccept;
//如果客户端连接没有断开则一直尝试连接要映射的端口
do
{
if (connect(mapSocket (SOCKADDR*)&sinmap sizeof(sinmap)) == SOCKET_ERROR)
{
printf(“ERROR:Connect map computer error.\r\n“);
if (sAccept != INVALID_SOCKET)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4596 2009-02-18 07:33 MPortServer.dsp
文件 530 2009-02-18 07:33 MPortServer.dsw
文件 256 2009-02-18 18:30 MPortServer.plg
文件 4211 2009-02-18 18:30 MPortServer.cpp
文件 50176 2009-02-18 18:30 MPortServer.ncb
文件 53760 2009-02-18 18:30 MPortServer.opt
文件 1238 2009-02-18 07:33 ReadMe.txt
文件 298 2009-02-18 07:33 StdAfx.cpp
文件 667 2009-02-18 07:33 StdAfx.h
----------- --------- ---------- ----- ----
115732 9
- 上一篇:超级玛丽c语言源代码
- 下一篇:计算机图形学——球的光照模型算法
评论
共有 条评论