![]() |
#2
asdcxz19822012-07-01 15:15
--------------------Configuration: sinlcx - Win32 Debug--------------------
Compiling... sinlcx.cpp c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(69) : error C2065: 'SOCKET' : undeclared identifier c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(69) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(69) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(73) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(89) : error C2146: syntax error : missing ')' before identifier 'hSocket' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(89) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(96) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(101) : error C2146: syntax error : missing ')' before identifier 'sListenSocket' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(101) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(109) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(131) : error C2146: syntax error : missing ')' before identifier 'hSocket' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(131) : error C2165: 'left-side modifier' : cannot modify pointers to data c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(131) : error C2071: 'LPFN_TRANSMITFILE' : illegal storage class c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(138) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(146) : error C2146: syntax error : missing ')' before identifier 'sListenSocket' c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(146) : error C2165: 'left-side modifier' : cannot modify pointers to data c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(146) : error C2071: 'LPFN_ACCEPTEX' : illegal storage class c:\users\john\desktop\vc6.0green\vc98\include\mswsock.h(154) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(85) : error C2378: 'SOCKET' : redefinition; symbol cannot be overloaded with a typedef c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(101) : error C2146: syntax error : missing ';' before identifier 'fd_array' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(101) : error C2501: 'SOCKET' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(101) : error C2501: 'fd_array' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(104) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1422) : error C2143: syntax error : missing ';' before '__stdcall' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1422) : error C2501: 'SOCKET' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1422) : error C2370: 'SOCKET' : redefinition; different storage class c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1424) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1424) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1424) : error C2501: 'accept' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1427) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1445) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1445) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1445) : error C2491: 'bind' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1448) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1467) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1467) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1467) : error C2491: 'closesocket' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1467) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1483) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1483) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1483) : error C2491: 'connect' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1486) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1504) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1504) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1504) : error C2491: 'ioctlsocket' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1507) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1525) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1525) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1525) : error C2491: 'getpeername' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1528) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1546) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1546) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1546) : error C2491: 'getsockname' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1549) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1567) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1567) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1567) : error C2491: 'getsockopt' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1572) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1660) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1660) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1660) : error C2491: 'listen' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1662) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1713) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1713) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1713) : error C2491: 'recv' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1717) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1736) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1736) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1736) : error C2491: 'recvfrom' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1742) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1788) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1788) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1788) : error C2491: 'send' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1792) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1811) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1811) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1811) : error C2491: 'sendto' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1817) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1838) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1838) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1838) : error C2491: 'setsockopt' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1843) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1863) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1863) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1863) : error C2491: 'shutdown' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1865) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1880) : error C2143: syntax error : missing ';' before '__stdcall' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1880) : error C2501: 'SOCKET' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(1880) : error C2370: 'SOCKET' : redefinition; different storage class c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2349) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2349) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2349) : error C2491: 'WSAAsyncSelect' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2353) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2399) : error C2143: syntax error : missing ';' before '__stdcall' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2399) : error C2501: 'SOCKET' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2399) : error C2370: 'SOCKET' : redefinition; different storage class c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2401) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2401) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2401) : error C2501: 'WSAAccept' : missing storage-class or type specifiers c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2406) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2443) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2443) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2443) : error C2491: 'WSAConnect' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2450) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2489) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2489) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2489) : error C2491: 'WSADuplicateSocketA' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2492) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2497) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2497) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2497) : error C2491: 'WSADuplicateSocketW' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2500) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2535) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2535) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2535) : error C2491: 'WSAEnumNetworkEvents' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2538) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2602) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2602) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2602) : error C2491: 'WSAEventSelect' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2605) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2623) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2623) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2623) : error C2491: 'WSAGetOverlappedResult' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2628) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2648) : error C2146: syntax error : missing ')' before identifier 's' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2648) : warning C4229: anachronism used : modifiers on data are ignored c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2648) : error C2491: 'WSAGetQOSByName' : definition of dllimport data not allowed c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2651) : error C2059: syntax error : ')' c:\users\john\desktop\vc6.0green\vc98\include\winsock2.h(2651) : fatal error C1003: error count exceeds 100; stopping compilation 执行 cl.exe 时出错. sinlcx.exe - 1 error(s), 0 warning(s) |

// SinLcx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
//#pragma comment(linker,"/entry:SinmainCRTStartup")
//#pragma comment(linker, "/OPT:NOWIN98")
//#pragma comment (linker, "/ALIGN:512")
/*
sinlcx -slave LocalIp LocalPort RemoteIp RemotePort
sinlcx -listen LocalPort1 LocalPort2
sinlcx -trans localport remoteip remoteport
*/
class CLock //简单的封装了下关键段
{
public:
CLock()
{
InitializeCriticalSection(&cs);
}
~CLock()
{
DeleteCriticalSection(&cs);
}
public:
void Lock()
{
EnterCriticalSection(&cs);
}
void UnLock()
{
LeaveCriticalSection(&cs);
}
// BOOL isLock()
// {
// return !TryEnterCriticalSection(&cs);
// }
private:
CRITICAL_SECTION cs;
};
BOOL Lcxclosesocket(SOCKET &);
struct SinSocket
{
SinSocket()
{
m_socket = INVALID_SOCKET;
m_UserData = 0;
memset(&addr,0,sizeof(sockaddr_in));
}
// operator SOCKET()
// {
// return m_socket;
// }
// operator LPVOID()
// {
// return this;
// }
// operator int()
// {
// return (int)pUserData;
// }
// operator sockaddr_in()
// {
// return addr;
// }
// private:
SOCKET m_socket;
int m_UserData;
sockaddr_in addr;
};
enum LcxMode
{
LCX_LISTEN = 0x2B, //-listen port 1 port2
LCX_SLAVE, //-slave ip1 port1 ip2 port2
LCX_TRANS, //-trans localport remoteip remoteport
UNKNOWN
};
BOOL bIsExit = FALSE;
BYTE mode = UNKNOWN;
volatile long m_ClientCount = 0;
SinSocket LcxSocket[2];
int
LcxPrintf(const char *format,...)
{
static CLock m_lock;
int bytesFormat = 0;
DWORD bytesWritten = 0;
char buff[1024];
m_lock.Lock();
va_list arg_list;
static HANDLE hConsoleOutput = NULL;
if (hConsoleOutput == NULL)
{
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
}
va_start(arg_list, format);
bytesFormat = _vsnprintf(buff, sizeof(buff), format, arg_list);
va_end(arg_list);
WriteConsole(hConsoleOutput, buff, bytesFormat, &bytesWritten, NULL);
m_lock.UnLock();
return bytesFormat;
}
BOOL Lcxclosesocket(SOCKET &m_Socket)
{
static CLock lock;
lock.Lock();
if(m_Socket == INVALID_SOCKET ||
m_Socket == SOCKET_ERROR) //客户端已经退出了
{
lock.UnLock();
return FALSE;
}
else
{
LcxPrintf("Try to close socket %d...\r\n",m_Socket);
LINGER ling;
ling.l_linger = 0;
ling.l_onoff = 1;
setsockopt(m_Socket, SOL_SOCKET, SO_LINGER, (char*)&ling, sizeof(LINGER));
CancelIo((HANDLE)m_Socket);
closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
}
lock.UnLock();
return TRUE;
}
void LcxTransData(SOCKET s1,SOCKET s2,sockaddr_in addr1,sockaddr_in addr2)
{
BYTE buff[8192] ;
int RecvBytes = 0;
int SendBytes = 0;
LcxPrintf("tran %d %d \r\n",s1,s2);
while(1)
{
RecvBytes = recv(s1,(char *)buff,8192,0);
if(RecvBytes <= 0 )
break;
SendBytes = send(s2,(char *)buff,RecvBytes,0);
if(SendBytes <= 0 )
break;
LcxPrintf("* %s:%d --> %s%d %u bytes...\r\n",
inet_ntoa(addr1.sin_addr),
ntohs(addr1.sin_port),
inet_ntoa(addr1.sin_addr),
ntohs(addr2.sin_port),
SendBytes);
}
}
SOCKET lcxlisten(short port,sockaddr_in *paddr)
{
SOCKET S_listen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(S_listen == INVALID_SOCKET)
return INVALID_SOCKET;
paddr->sin_family = AF_INET;
paddr->sin_addr.S_un.S_addr = INADDR_ANY;
paddr->sin_port = htons(port);
if(bind(S_listen,(sockaddr *)paddr,sizeof(sockaddr)) == SOCKET_ERROR)
{
Lcxclosesocket(S_listen);
return INVALID_SOCKET;
}
listen(S_listen,5);
LcxPrintf("Listen on port %d OK ...\r\n",port);
SOCKET S_Client = INVALID_SOCKET;
ZeroMemory(paddr,sizeof(sockaddr_in));
int Len = sizeof(sockaddr);
S_Client = accept(S_listen,(sockaddr *)paddr,&Len);
if(S_Client == INVALID_SOCKET)
{
}
Lcxclosesocket(S_listen);
LcxPrintf("A Client Connected ,from %s:%d socket %d\r\n",inet_ntoa(paddr->sin_addr),ntohs(paddr->sin_port),S_Client);
return S_Client;
}
SOCKET LcxConnect(char *ip,short port,sockaddr_in *pAddr)
{
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
hostent *HostEnts=gethostbyname(ip);
pAddr->sin_port = htons(port);
pAddr->sin_family = AF_INET;
pAddr->sin_addr = *((LPIN_ADDR)*HostEnts->h_addr_list);
if(connect(s,(sockaddr *)pAddr,sizeof(sockaddr)) == SOCKET_ERROR)
{
Lcxclosesocket(s);
}
else
LcxPrintf("connect %s : %d OK...\r\n",ip,port);
return s;
}
BOOL lcxslave(char *ip1,short port1,char *ip2,short port2)
{
return TRUE;
}
HANDLE LcxCreatethread(LPTHREAD_START_ROUTINE func,LPVOID lparam)
{
return CreateThread(NULL,0,func,lparam,0,NULL);
}
void LcxWaitThreadExit()
{
while(m_ClientCount)
if(!SwitchToThread())
Sleep(10);
}
void LcxUsage()
{
LcxPrintf(\
"\t\t\t\tSinLcx\r\n"
"\t\t\tBy:sincoder\r\n"
"\t\t\tQQ:1220145498\r\n"
"\t\t\t使用方法和大家熟悉的 lcx 一样 \r\n"
"Usage:\r\n"
"\tsinlcx -listen LocalPort1 LocalPort2\n"
"\tsinlcx -slave Ip1 Port1 Ip2 Port2\n"
"\tsinlcx -trans localport remoteip remoteport");
}
BOOL WINAPI HandlerRoutine(
DWORD dwCtrlType
)
{
switch(dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
{
LcxPrintf("# I am going to exit ....\r\n");
Lcxclosesocket(LcxSocket[0].m_socket); //如果正在监听 那么不能关闭 。。。。
Lcxclosesocket(LcxSocket[1].m_socket);
Sleep(1000);
}
break;
default:
break;
}
return 0;
}
DWORD WINAPI LcxThreadContext(LPVOID lparam)
{
InterlockedIncrement(&m_ClientCount);
SinSocket *pSocket = (SinSocket *)lparam;
SinSocket *pOtherSocket = pSocket->m_UserData == 0 ? &LcxSocket[1] : &LcxSocket[0];
switch(mode)
{
case LCX_LISTEN:
{
pSocket->m_socket = lcxlisten(pSocket->addr.sin_port,&pSocket->addr);
if( pSocket->m_socket == INVALID_SOCKET)
{
LcxPrintf("listen on port %d error ....\r\n",htons(pSocket->addr.sin_port));
break;
}
while(pOtherSocket->m_socket == INVALID_SOCKET) //如果对方还没连接
if(!SwitchToThread())
Sleep(10);
LcxTransData(\
pSocket->m_socket,
pOtherSocket->m_socket,
pSocket->addr,
pOtherSocket->addr);
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
}
break;
case LCX_SLAVE:
{
LcxTransData(\
pSocket->m_socket,
pOtherSocket->m_socket,
pSocket->addr,
pOtherSocket->addr);
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
}
break;
case LCX_TRANS:
{
if(pSocket->m_socket == INVALID_SOCKET)
{
pSocket->m_socket = lcxlisten(pSocket->addr.sin_port,&pSocket->addr);
if( pSocket->m_socket == INVALID_SOCKET)
{
LcxPrintf("listen on port %d error ....\r\n",ntohs(pSocket->addr.sin_port));
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
break;
}
}
while(pOtherSocket->m_socket == INVALID_SOCKET) //如果对方还没连接
if(!SwitchToThread())
Sleep(10);
LcxTransData(\
pSocket->m_socket,
pOtherSocket->m_socket,
pSocket->addr,
pOtherSocket->addr);
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
}
break;
default:
break;
}
LcxPrintf("Thread %d exiting.,...\r\n",GetCurrentThreadId());
InterlockedDecrement(&m_ClientCount);
return 0;
}
int main(int argc, char* argv[])
{
if(argc<2)
{
LcxUsage();
return 0;
}
WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
SetConsoleCtrlHandler(HandlerRoutine,TRUE);
if(strcmp(argv[1],"-listen")==0)
{
if(argc < 4)
{
LcxUsage();
goto LcxExit;
}
mode = LCX_LISTEN;
while(1)
{
LcxSocket[0].m_UserData = 0;
LcxSocket[1].m_UserData = 1;
LcxSocket[0].addr.sin_port = atoi(argv[2]);
LcxSocket[1].addr.sin_port = atoi(argv[3]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[0]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[1]);
Sleep(1000); //等待线程启动 。。。
LcxWaitThreadExit();
LcxPrintf("Two Thread exited ...\r\n");
}
}else
if(strcmp(argv[1],"-slave")==0)
{
if(argc <6)
{
LcxUsage();
goto LcxExit;
}
while(1)
{
mode = LCX_SLAVE;
LcxSocket[0].m_UserData = 0;
LcxSocket[1].m_UserData = 1;
while(1)
{
LcxSocket[0].m_socket = LcxConnect(argv[2],atoi(argv[3]),&LcxSocket[0].addr);
if(LcxSocket[0].m_socket == INVALID_SOCKET)
{
LcxPrintf("Connect %s : %d error...\r\n",argv[2],atoi(argv[3]));
Sleep(5000);
}
else
break;
}
while(1)
{
LcxSocket[1].m_socket = LcxConnect(argv[4],atoi(argv[5]),&LcxSocket[1].addr);
if(LcxSocket[1].m_socket == INVALID_SOCKET)
{
LcxPrintf("Connect %s : %d error...\r\n",argv[4],atoi(argv[5]));
Sleep(5000);
}
else
break;
}
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[0]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[1]);
Sleep(1000); //等待线程启动 。。。
LcxWaitThreadExit();
Sleep(2000);
}
}
else
if (strcmp(argv[1],"-trans")==0)
{
if(argc <5)
{
LcxUsage();
goto LcxExit;
}
while(1)
{
mode = LCX_TRANS;
LcxSocket[0].m_UserData = 0;
LcxSocket[1].m_UserData = 1;
while(1)
{
LcxSocket[0].m_socket = LcxConnect(argv[3],atoi(argv[4]),&LcxSocket[0].addr);
if(LcxSocket[0].m_socket == INVALID_SOCKET)
{
LcxPrintf("Connect %s : %d error...\r\n",argv[3],atoi(argv[4]));
Sleep(5000);
}
else
break;
}
LcxSocket[1].addr.sin_port = atoi(argv[2]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[0]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[1]);
Sleep(1000); //等待线程启动 。。。
LcxWaitThreadExit();
Sleep(2000);
}
}
else
LcxUsage();
LcxExit:
WSACleanup();
return 0;
}
//
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"ws2_32.lib")
//#pragma comment(linker,"/entry:SinmainCRTStartup")
//#pragma comment(linker, "/OPT:NOWIN98")
//#pragma comment (linker, "/ALIGN:512")
/*
sinlcx -slave LocalIp LocalPort RemoteIp RemotePort
sinlcx -listen LocalPort1 LocalPort2
sinlcx -trans localport remoteip remoteport
*/
class CLock //简单的封装了下关键段
{
public:
CLock()
{
InitializeCriticalSection(&cs);
}
~CLock()
{
DeleteCriticalSection(&cs);
}
public:
void Lock()
{
EnterCriticalSection(&cs);
}
void UnLock()
{
LeaveCriticalSection(&cs);
}
// BOOL isLock()
// {
// return !TryEnterCriticalSection(&cs);
// }
private:
CRITICAL_SECTION cs;
};
BOOL Lcxclosesocket(SOCKET &);
struct SinSocket
{
SinSocket()
{
m_socket = INVALID_SOCKET;
m_UserData = 0;
memset(&addr,0,sizeof(sockaddr_in));
}
// operator SOCKET()
// {
// return m_socket;
// }
// operator LPVOID()
// {
// return this;
// }
// operator int()
// {
// return (int)pUserData;
// }
// operator sockaddr_in()
// {
// return addr;
// }
// private:
SOCKET m_socket;
int m_UserData;
sockaddr_in addr;
};
enum LcxMode
{
LCX_LISTEN = 0x2B, //-listen port 1 port2
LCX_SLAVE, //-slave ip1 port1 ip2 port2
LCX_TRANS, //-trans localport remoteip remoteport
UNKNOWN
};
BOOL bIsExit = FALSE;
BYTE mode = UNKNOWN;
volatile long m_ClientCount = 0;
SinSocket LcxSocket[2];
int
LcxPrintf(const char *format,...)
{
static CLock m_lock;
int bytesFormat = 0;
DWORD bytesWritten = 0;
char buff[1024];
m_lock.Lock();
va_list arg_list;
static HANDLE hConsoleOutput = NULL;
if (hConsoleOutput == NULL)
{
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
}
va_start(arg_list, format);
bytesFormat = _vsnprintf(buff, sizeof(buff), format, arg_list);
va_end(arg_list);
WriteConsole(hConsoleOutput, buff, bytesFormat, &bytesWritten, NULL);
m_lock.UnLock();
return bytesFormat;
}
BOOL Lcxclosesocket(SOCKET &m_Socket)
{
static CLock lock;
lock.Lock();
if(m_Socket == INVALID_SOCKET ||
m_Socket == SOCKET_ERROR) //客户端已经退出了
{
lock.UnLock();
return FALSE;
}
else
{
LcxPrintf("Try to close socket %d...\r\n",m_Socket);
LINGER ling;
ling.l_linger = 0;
ling.l_onoff = 1;
setsockopt(m_Socket, SOL_SOCKET, SO_LINGER, (char*)&ling, sizeof(LINGER));
CancelIo((HANDLE)m_Socket);
closesocket(m_Socket);
m_Socket = INVALID_SOCKET;
}
lock.UnLock();
return TRUE;
}
void LcxTransData(SOCKET s1,SOCKET s2,sockaddr_in addr1,sockaddr_in addr2)
{
BYTE buff[8192] ;
int RecvBytes = 0;
int SendBytes = 0;
LcxPrintf("tran %d %d \r\n",s1,s2);
while(1)
{
RecvBytes = recv(s1,(char *)buff,8192,0);
if(RecvBytes <= 0 )
break;
SendBytes = send(s2,(char *)buff,RecvBytes,0);
if(SendBytes <= 0 )
break;
LcxPrintf("* %s:%d --> %s%d %u bytes...\r\n",
inet_ntoa(addr1.sin_addr),
ntohs(addr1.sin_port),
inet_ntoa(addr1.sin_addr),
ntohs(addr2.sin_port),
SendBytes);
}
}
SOCKET lcxlisten(short port,sockaddr_in *paddr)
{
SOCKET S_listen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(S_listen == INVALID_SOCKET)
return INVALID_SOCKET;
paddr->sin_family = AF_INET;
paddr->sin_addr.S_un.S_addr = INADDR_ANY;
paddr->sin_port = htons(port);
if(bind(S_listen,(sockaddr *)paddr,sizeof(sockaddr)) == SOCKET_ERROR)
{
Lcxclosesocket(S_listen);
return INVALID_SOCKET;
}
listen(S_listen,5);
LcxPrintf("Listen on port %d OK ...\r\n",port);
SOCKET S_Client = INVALID_SOCKET;
ZeroMemory(paddr,sizeof(sockaddr_in));
int Len = sizeof(sockaddr);
S_Client = accept(S_listen,(sockaddr *)paddr,&Len);
if(S_Client == INVALID_SOCKET)
{
}
Lcxclosesocket(S_listen);
LcxPrintf("A Client Connected ,from %s:%d socket %d\r\n",inet_ntoa(paddr->sin_addr),ntohs(paddr->sin_port),S_Client);
return S_Client;
}
SOCKET LcxConnect(char *ip,short port,sockaddr_in *pAddr)
{
SOCKET s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
hostent *HostEnts=gethostbyname(ip);
pAddr->sin_port = htons(port);
pAddr->sin_family = AF_INET;
pAddr->sin_addr = *((LPIN_ADDR)*HostEnts->h_addr_list);
if(connect(s,(sockaddr *)pAddr,sizeof(sockaddr)) == SOCKET_ERROR)
{
Lcxclosesocket(s);
}
else
LcxPrintf("connect %s : %d OK...\r\n",ip,port);
return s;
}
BOOL lcxslave(char *ip1,short port1,char *ip2,short port2)
{
return TRUE;
}
HANDLE LcxCreatethread(LPTHREAD_START_ROUTINE func,LPVOID lparam)
{
return CreateThread(NULL,0,func,lparam,0,NULL);
}
void LcxWaitThreadExit()
{
while(m_ClientCount)
if(!SwitchToThread())
Sleep(10);
}
void LcxUsage()
{
LcxPrintf(\
"\t\t\t\tSinLcx\r\n"
"\t\t\tBy:sincoder\r\n"
"\t\t\tQQ:1220145498\r\n"
"\t\t\t使用方法和大家熟悉的 lcx 一样 \r\n"
"Usage:\r\n"
"\tsinlcx -listen LocalPort1 LocalPort2\n"
"\tsinlcx -slave Ip1 Port1 Ip2 Port2\n"
"\tsinlcx -trans localport remoteip remoteport");
}
BOOL WINAPI HandlerRoutine(
DWORD dwCtrlType
)
{
switch(dwCtrlType)
{
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
{
LcxPrintf("# I am going to exit ....\r\n");
Lcxclosesocket(LcxSocket[0].m_socket); //如果正在监听 那么不能关闭 。。。。
Lcxclosesocket(LcxSocket[1].m_socket);
Sleep(1000);
}
break;
default:
break;
}
return 0;
}
DWORD WINAPI LcxThreadContext(LPVOID lparam)
{
InterlockedIncrement(&m_ClientCount);
SinSocket *pSocket = (SinSocket *)lparam;
SinSocket *pOtherSocket = pSocket->m_UserData == 0 ? &LcxSocket[1] : &LcxSocket[0];
switch(mode)
{
case LCX_LISTEN:
{
pSocket->m_socket = lcxlisten(pSocket->addr.sin_port,&pSocket->addr);
if( pSocket->m_socket == INVALID_SOCKET)
{
LcxPrintf("listen on port %d error ....\r\n",htons(pSocket->addr.sin_port));
break;
}
while(pOtherSocket->m_socket == INVALID_SOCKET) //如果对方还没连接
if(!SwitchToThread())
Sleep(10);
LcxTransData(\
pSocket->m_socket,
pOtherSocket->m_socket,
pSocket->addr,
pOtherSocket->addr);
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
}
break;
case LCX_SLAVE:
{
LcxTransData(\
pSocket->m_socket,
pOtherSocket->m_socket,
pSocket->addr,
pOtherSocket->addr);
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
}
break;
case LCX_TRANS:
{
if(pSocket->m_socket == INVALID_SOCKET)
{
pSocket->m_socket = lcxlisten(pSocket->addr.sin_port,&pSocket->addr);
if( pSocket->m_socket == INVALID_SOCKET)
{
LcxPrintf("listen on port %d error ....\r\n",ntohs(pSocket->addr.sin_port));
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
break;
}
}
while(pOtherSocket->m_socket == INVALID_SOCKET) //如果对方还没连接
if(!SwitchToThread())
Sleep(10);
LcxTransData(\
pSocket->m_socket,
pOtherSocket->m_socket,
pSocket->addr,
pOtherSocket->addr);
Lcxclosesocket(LcxSocket[0].m_socket);
Lcxclosesocket(LcxSocket[1].m_socket);
}
break;
default:
break;
}
LcxPrintf("Thread %d exiting.,...\r\n",GetCurrentThreadId());
InterlockedDecrement(&m_ClientCount);
return 0;
}
int main(int argc, char* argv[])
{
if(argc<2)
{
LcxUsage();
return 0;
}
WSADATA wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
SetConsoleCtrlHandler(HandlerRoutine,TRUE);
if(strcmp(argv[1],"-listen")==0)
{
if(argc < 4)
{
LcxUsage();
goto LcxExit;
}
mode = LCX_LISTEN;
while(1)
{
LcxSocket[0].m_UserData = 0;
LcxSocket[1].m_UserData = 1;
LcxSocket[0].addr.sin_port = atoi(argv[2]);
LcxSocket[1].addr.sin_port = atoi(argv[3]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[0]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[1]);
Sleep(1000); //等待线程启动 。。。
LcxWaitThreadExit();
LcxPrintf("Two Thread exited ...\r\n");
}
}else
if(strcmp(argv[1],"-slave")==0)
{
if(argc <6)
{
LcxUsage();
goto LcxExit;
}
while(1)
{
mode = LCX_SLAVE;
LcxSocket[0].m_UserData = 0;
LcxSocket[1].m_UserData = 1;
while(1)
{
LcxSocket[0].m_socket = LcxConnect(argv[2],atoi(argv[3]),&LcxSocket[0].addr);
if(LcxSocket[0].m_socket == INVALID_SOCKET)
{
LcxPrintf("Connect %s : %d error...\r\n",argv[2],atoi(argv[3]));
Sleep(5000);
}
else
break;
}
while(1)
{
LcxSocket[1].m_socket = LcxConnect(argv[4],atoi(argv[5]),&LcxSocket[1].addr);
if(LcxSocket[1].m_socket == INVALID_SOCKET)
{
LcxPrintf("Connect %s : %d error...\r\n",argv[4],atoi(argv[5]));
Sleep(5000);
}
else
break;
}
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[0]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[1]);
Sleep(1000); //等待线程启动 。。。
LcxWaitThreadExit();
Sleep(2000);
}
}
else
if (strcmp(argv[1],"-trans")==0)
{
if(argc <5)
{
LcxUsage();
goto LcxExit;
}
while(1)
{
mode = LCX_TRANS;
LcxSocket[0].m_UserData = 0;
LcxSocket[1].m_UserData = 1;
while(1)
{
LcxSocket[0].m_socket = LcxConnect(argv[3],atoi(argv[4]),&LcxSocket[0].addr);
if(LcxSocket[0].m_socket == INVALID_SOCKET)
{
LcxPrintf("Connect %s : %d error...\r\n",argv[3],atoi(argv[4]));
Sleep(5000);
}
else
break;
}
LcxSocket[1].addr.sin_port = atoi(argv[2]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[0]);
LcxCreatethread(LcxThreadContext,(LPVOID)&LcxSocket[1]);
Sleep(1000); //等待线程启动 。。。
LcxWaitThreadExit();
Sleep(2000);
}
}
else
LcxUsage();
LcxExit:
WSACleanup();
return 0;
}