geral吧 关注:5贴子:215
  • 3回复贴,共1
//console project without MFC
#include "stdafx.h"
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
LPWSADATA lpWSAData=new WSADATA();
if (WSAStartup(wVersionRequested,lpWSAData)!=0)
{
cout<<"startup fail"<<endl;
return 1;
}
cout<<"start up ok"<<endl
<<"version is: "<<lpWSAData->wVersion<<endl
<<"iMaxsockets: " <<lpWSAData->iMaxSockets<<endl
<<"iMaxUdpdg: "<<lpWSAData->iMaxUdpDg<<endl
//<<"vendorinfo: "<<lpWSAData->lpVendorInfo<<endl
<<"szdescription: "<<lpWSAData->szDescription<<endl
<<"systemstatus: "<<lpWSAData->szSystemStatus<<endl
<<"High Version: "<<lpWSAData->wHighVersion<<endl;
return 0;
}


IP属地:广东1楼2014-10-22 18:17回复
    // TCP_IP_TESE_1_VC2005.cpp : 定义控制台应用程序的入口点。
    //
    #include "stdafx.h"
    #include <WinSock2.h>
    #include <ws2tcpip.h>
    #include <iostream>
    #pragma comment(lib,"ws2_32.lib")
    using std::cout;
    using std::endl;
    int InitTCP(LPWSADATA lpWSAData);
    int ExitTCP(void);
    int _tmain(int argc, _TCHAR* argv[])
    {
    LPWSADATA lpWSAData=new WSADATA();
    if(InitTCP(lpWSAData)!=0){
    return 1;
    }
    SOCKET sk;
    sk=socket(AF_INET,SOCK_STREAM,0);
    if(sk==SOCKET_ERROR){
    cout<<"SK ERR: "<<sk<<endl;
    return 2;
    }
    sockaddr_in sock;
    sock.sin_family=AF_INET;
    sock.sin_port=htons(2055);
    sock.sin_addr.s_addr=inet_addr("localhost");
    if(connect(sk,(sockaddr*)&sock,sizeof(sock))==SOCKET_ERROR){
    cout<<"connect fail, error code = "<<WSAGetLastError()<<endl;
    return SOCKET_ERROR;
    }
    cout<<"connect ok"<<endl;
    if(bind(sk,(sockaddr *)&sock,sizeof(sock))==SOCKET_ERROR){
    cout<<"bind fail, error code = "<<WSAGetLastError()<<endl;
    return 4;
    }
    cout<<"bind ok"<<endl;
    if(listen(sk,1)==SOCKET_ERROR){
    cout<<"listen fail, error code = "<<WSAGetLastError()<<endl;
    return 5;
    }
    cout<<"listen OK"<<endl;
    sockaddr_in sock_c;
    int *addr_len= new int();
    *addr_len=(int)sizeof(sock_c);
    if(accept(sk,(sockaddr*)&sock_c,addr_len)==SOCKET_ERROR){
    cout<<"accept error, error code = "<<WSAGetLastError()<<endl;
    return 6;
    }
    cout<<"accept OK"<<endl;
    cout<<"address is: "<<sock_c.sin_addr.s_addr<<endl
    <<"port is: "<<sock_c.sin_port<<endl;
    closesocket(sk);
    ExitTCP();
    delete lpWSAData;
    return 0;
    }
    int InitTCP(LPWSADATA lpWSAData){
    WORD wVersionRequested;
    wVersionRequested = MAKEWORD(2, 2);
    int err;
    if (err=WSAStartup(wVersionRequested,lpWSAData)!=0)
    {
    cout<<"startup fail"<<endl;
    return err;
    }
    cout<<"start up ok"<<endl
    <<"version is: "<<lpWSAData->wVersion<<endl
    <<"iMaxsockets: " <<lpWSAData->iMaxSockets<<endl
    <<"iMaxUdpdg: "<<lpWSAData->iMaxUdpDg<<endl
    //<<"vendorinfo: "<<lpWSAData->lpVendorInfo<<endl
    <<"szdescription: "<<lpWSAData->szDescription<<endl
    <<"systemstatus: "<<lpWSAData->szSystemStatus<<endl
    <<"High Version: "<<lpWSAData->wHighVersion<<endl;
    if (LOBYTE(lpWSAData->wVersion) != 2 || HIBYTE(lpWSAData->wVersion) != 2){
    cout<<"Could not find a usable version of Winsock.dll"<<endl;
    WSACleanup();
    return 1;
    }
    else{
    cout<<"The Winsock 2.2 dll was found okay"<<endl;
    }
    return 0;
    }
    int ExitTCP(void){
    WSACleanup();
    return 0;
    }


    IP属地:广东2楼2014-10-24 18:35
    回复
      int test(){
      CApplication eap;
      CWorkbook wb;
      CWorkbooks wbs;
      CWorksheet ws;
      CGlobal glb;
      cout<<"begin"<<endl;
      if(CoInitialize(NULL)!=0)
      {
      cout<<"error on initialize"<<endl;
      return -1;
      }
      cout<<"com init"<<endl;
      if(!eap.CreateDispatch(_T("Excel.Application"),NULL)){
      cout<<"error on createdispatch"<<endl;
      return -2;
      }
      cout<<"patch init"<<endl;
      eap.put_Visible(TRUE);
      CString temp = _T("D:\\e1.xlsx");
      wbs = eap.get_Workbooks();
      wb=wbs.Add(_variant_t(TRUE));
      wb.SaveCopyAs(_variant_t(temp));
      wbs.Close();
      eap.Quit();
      cout<<"quit"<<endl;
      if(!eap.DetachDispatch()){
      cout<<"dispatch fail"<<endl;
      }
      cout<<"dispatch"<<endl;
      CoUninitialize();
      cout<<"com close"<<endl;
      return 0;
      }


      IP属地:广东3楼2015-02-26 13:25
      回复
        #include "stdafx.h"
        #include "CSocket_Multhread_Test_1.h"
        #include <iostream>
        #include <Windows.h>
        #include <afxsock.h>
        #ifdef _DEBUG
        #define new DEBUG_NEW
        #endif
        // The one and only application object
        CWinApp theApp;
        using namespace std;
        int cfiletest(void);
        void tfunc(LPVOID para);
        int socket_server(void);
        int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
        {
        int nRetCode = 0;
        HMODULE hModule = ::GetModuleHandle(NULL);
        if (hModule != NULL)
        {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        nRetCode = 1;
        }
        else
        {
        // TODO: code your application's behavior here.
        socket_server();
        }
        }
        else
        {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
        }
        return nRetCode;
        }
        int cfiletest(void){
        return 0;
        }
        /*
        HANDLE ht1,ht2;
        LPDWORD pw = new DWORD();
        int pass=12;
        ht1 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)tfunc,(LPVOID)&pass,0,pw);
        CloseHandle(ht1);
        Sleep(1000);
        pass=24;
        ht2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)tfunc,(LPVOID)&pass,0,pw);
        CloseHandle(ht1);
        cout<<"input a"<<endl;
        char a[32];
        cin>>a;
        cout<<"a="<<a<<endl;
        Sleep(3100);
        delete pw;
        */
        int socket_server(void){
        /*
        CSocket::Create 初始化(一般写服务器程序都不要用为好,用下面的 CSocket::Socket 初始化)
        CSocket::Socket初始化
        CSocket::SetSockOpt 设置socket选项
        CSocket::Bind 绑定地址端口
        CSocket::Connect 连接
        CSocket::Listen 监听
        CSocket::Accept 接收外部连接的socket
        CSocket::Send 发送内容
        CSocket::Receive 接收内容
        CSocket::Close 关闭(不等于delete)
        */
        ::HANDLE hd;
        LPDWORD pw = new DWORD();
        if(!AfxSocketInit()){
        cout<<"init socket fail, error code = "<<::GetLastError()<<endl;
        return -1;
        }
        CString ctext=_T("abcdefg");
        char recstr[1024];
        CSocket cs,*cserver;
        if(!cs.Create(1024,1,_T("127.0.0.1"))){
        cout<<"create fail"<<endl;
        return -6;
        }
        cout<<"create ok"<<endl;
        int listenret;
        if((listenret=cs.Listen(5))==0){
        cout<<"listen fail, error code = "<<::GetLastError()<<endl;
        return -3;
        }
        cout<<"lret = "<<listenret<<endl;
        cout<<"listen ok, ready to accept"<<endl;
        ::SOCKADDR_IN saddr_in;
        int addrlen=255;
        while(1){
        Sleep(1000);
        cserver=new CSocket();
        cout<<"ready accept"<<endl;
        if(!cs.Accept(*cserver,(SOCKADDR*) &saddr_in,&addrlen)){
        cout<<"accept fail, error code = "<<::GetLastError()<<endl;
        }else{
        cout<<"accept ok"<<endl;
        cout<<"Client address is: "<<saddr_in.sin_addr.s_addr<<endl;
        hd = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)tfunc,(LPVOID)cserver,0,pw);
        }
        Sleep(1000);
        }
        /*
        cserver.Send(ctext,ctext.GetLength()+1);
        cout<<"send ok, ready to receive"<<endl;
        cserver.Receive(recstr,1024);
        cout<<"receive: "<<recstr<<endl;
        cout<<"ready to close"<<endl;
        */
        cs.Close();
        // cserver.Close();
        cout<<"close ok."<<endl;
        return 0;
        }
        void tfunc(LPVOID para){
        /* int i;
        int *pass= (int*)para;
        int temp =*pass;
        cout<<"pass = "<<(*pass)<<endl;
        for(i=0;i<5;i++){
        cout<<"tfunc "<<temp<<" is called, i ="<<i<<endl;
        Sleep(500);
        }*/
        CSocket *nsk=(CSocket*)para;
        char buff[15]="qwert";
        nsk->Send(buff,4);
        cout<<"multhread send"<<endl;
        return;


        IP属地:广东4楼2015-03-04 08:59
        回复