编译一个cpp文件出错,谁能帮忙看下!
程序代码:
// 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; }