当前位置: 首页>>代码示例>>C++>>正文


C++ ServerSocket::getLocalPort方法代码示例

本文整理汇总了C++中ServerSocket::getLocalPort方法的典型用法代码示例。如果您正苦于以下问题:C++ ServerSocket::getLocalPort方法的具体用法?C++ ServerSocket::getLocalPort怎么用?C++ ServerSocket::getLocalPort使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServerSocket的用法示例。


在下文中一共展示了ServerSocket::getLocalPort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main() {
    /*test of socket assistant*/
    SocketAssistant sa;
    string addr = sa.getAddrByName("www.syr.edu");
    cout << "address is " << addr << endl;
    string hostname = sa.getNameByAddr(addr);
    cout << "host name is " << hostname << endl;
    addr = sa.getAddrByName(hostname);
    cout << "address is " << addr << endl;


    /*test of socket and server socket*/

    Socket client;
    ServerSocket server;
    server.Bind(3491, "127.0.0.1");
    client.Connect("127.0.0.1", 3491);
    Socket commuSock = server.Accept();
    /*client communicates with commuSock*/
    string sendMsg = "msg sent";
    string respMsg = "msg recevd";
    string sigClient = "from client";
    string sigServer = "from server";
    char rbuf[100];
    int sentBytes = client.Send((sendMsg + " " + sigClient).c_str(), 7);
    cout << "sent bytes is " << sentBytes << endl;
    int recvBytes; 
    sentBytes = client.Send((sendMsg + " " + sigClient).c_str(), 50, 0, 30);
    cout << "sent bytes is " << sentBytes << endl;
    recvBytes = commuSock.Receive(rbuf, 50, 0, 30);
    rbuf[recvBytes] = '\0';
    cout << "recevd bytes is " << recvBytes << "\n message is "
            << rbuf << endl;
    cout << "client connecting to " << client.getRemoteIP() << " and the port is "
            << client.getRemotePort() << endl;
    cout  << "  the local port is "
            << server.getLocalPort() << " local ip is " << server.getLocalIP() << endl;

    client.Close();
    commuSock.Close();
    
    
    client.Connect("127.0.0.1", 3491);
    commuSock = server.Accept();
    commuSock.Send((respMsg + " " + sigServer).c_str(), 50);
    client.Receive(rbuf, 50);
    cout << "\n message is " << rbuf << endl;

    cout << "client connecting to " << client.getRemoteIP() << " and the port is "
            << client.getRemotePort() << endl;
    cout << " and the local port is "
            << server.getLocalPort() << " local ip is " << server.getLocalIP() << endl;
    client.Close();
    commuSock.Close();

    client.Connect("128.230.171.184", 80);
    cout << client.getRemoteIP() << endl;
    cout << client.getRemotePort() << endl;
    client.Close();
    cout<<"open the browse type 127.0.0.1:3491 to call the socket\n";
    Socket ns = server.Accept();
    cout << ns.getRemoteIP() << endl;
    cout << ns.getRemotePort() << endl;
    cout<<"server ip address is : "<<server.getLocalIP()<<"\n";
    cout<<"server monitori port is : "<<server.getLocalPort()<<"\n";
    /*   commuSock = server.Accept();
         char webSockBuf[1024];
         int recSize = commuSock.Receive(webSockBuf,1023);
         webSockBuf[recSize] = '\0';  
         cout<<"receive size is "<<recSize<<" "<<webSockBuf<<endl;*/
    /* For the purpose of testing post method of tiny web server*/
    /*
       Socket postSock;
       char* msgBody;
       int size = ResourceRetrieve("/3rd_front.jpg",&msgBody);
       std::cout<<size<<endl;
       postSock.Connect("localhost",3490);
       std::string postHeader= "POST /Post/post.jpg HTTP/1.1\r\n"
       "Host: localhost:3490\r\n"
       "Accept-Encoding: gzip, deflate\r\n"
       "Content-Type: image/jpeg\r\n"
       "Content-Length: 21292\r\n\r\n";
       postSock.Send(postHeader.c_str(),postHeader.size());
       postSock.Send(msgBody,size);
     */

    return 0;
}
开发者ID:tashia,项目名称:Tiny-Web-Server,代码行数:88,代码来源:Socket.cpp


注:本文中的ServerSocket::getLocalPort方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。