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


C++ Net::sendData方法代码示例

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


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

示例1: main

int main()
{
    USHORT port = DEFAULT_PORT;
    USHORT remotePort = DEFAULT_PORT;  // Remote port number
    int newPort = DEFAULT_PORT;
    int protocol;           // TCP or UDP
    char remoteIP[16];      // Remote IP address as dotted quad; nnn.nnn.nnn.nnn
    char localIP[16];       // Local IP address as dotted quad; nnn.nnn.nnn.nnn
    char netbuf[BUFSIZE];   // network receive
    char keybuf[BUFSIZE];   // keyboard input
    int error     = netNS::NET_OK; 
    int lastError = netNS::NET_OK; 
    int sizeXmit=0;         // transmit size
    int sizeRecv=0;         // receive size
    int size=0;
    Net net;                // Network communication object
    netbuf[0] = '\0';

    // ----- create server -----
    do
    {
        // display protocol menu
        cout << "----- Chat Server -----\n"
             << "\nSelect Protocol\n"
             << "    0 = UDP\n"
             << "    1 = TCP\n\n"
             << "  Choice: ";
        cin >> protocol;    // get character
    }while(protocol != 0 && protocol != 1);
    cin.ignore();

    // get port number
    //     Port numbers 0-1023 are used for well-known services.
    //     Port numbers 1024-65535 may be freely used.
    do
    {
        cout << "Enter port number (Use 0 for default 48161): ";
        cin >> newPort;
    }while(newPort < 0 || newPort > 65535);
    if(newPort != 0)
        port = newPort;
    cin.ignore();

    // create server
    error = net.createServer(port, protocol);
    if(error != netNS::NET_OK)          // if error
    {
        cout << net.getError(error) << endl;
        system("pause");
        return 1;
    }

    // display serverIP
    net.getLocalIP(localIP);
    cout << "Server IP is: " << localIP << endl;
    cout << "Server port is: " << port << endl;

    // display incoming text and send back response
    while(true)     // ***** INFINITE LOOP *****
    {
        // check for receive
        sizeRecv = BUFSIZE;     // max bytes to receive
        error = net.readData(netbuf,sizeRecv,remoteIP, remotePort);
        if(error != netNS::NET_OK)
        {
            cout << net.getError(error) << endl;
            error = net.closeSocket();     // close connection
            // re-create server
            error = net.createServer(port, protocol);
            if(error != netNS::NET_OK)          // if error
            {
                cout << net.getError(error) << endl;
                return 1;
            }
        }
        if(sizeRecv > 0)        // if characters received
        {
            // display incomming message
            cout << netbuf << endl;
        }

        // if keyboard input pending
        if (_kbhit())           
        {
            cin.getline(keybuf, BUFSIZE);    // get input
            sizeXmit += (int)cin.gcount() + 1;   // get size of input + null
            if(cin.fail())      // if failbit
            {
                cin.clear();    // clear error
                cin.ignore(INT_MAX, '\n');   // flush input
            }
        }

        // if data ready to send
        if(sizeXmit > 0)
        {
            size = sizeXmit;
            error = net.sendData(keybuf,size,remoteIP,remotePort); // send data
            if(error != netNS::NET_OK)
                cout << net.getError(error) << endl;
//.........这里部分代码省略.........
开发者ID:codebachi,项目名称:ProjectDX_Kelly,代码行数:101,代码来源:main.cpp

示例2: _tmain


//.........这里部分代码省略.........
	player1circle.SetPosition(playerA.getPosition());
	player2circle.SetPosition(playerB.getPosition());

	try
	{

		//While the connection is not closed remotely
		while(renderWindow.IsOpened() && !gameOver)
		{		
		
			Event sfmlEvent;
		
			while (renderWindow.PollEvent(sfmlEvent))
			{
				// Close window : exit
				if (sfmlEvent.Type == Event::Closed)
				{
					renderWindow.Close();
				}
				else if (sfmlEvent.Type == Event::KeyPressed)
				{

				      if (sf::Keyboard::IsKeyPressed(sf::Keyboard::W))
					  {			
						if(player == 2)
						{
							playerA.update(1);
						}

						else if(player == 1)
						{
							playerB.update(1);
						}
						network.sendData("127.0.0.1",destPort, "W");
					  }

					  if (sf::Keyboard::IsKeyPressed(sf::Keyboard::A))
					  {			
						if(player == 2)
						{
							playerA.update(2);
						}

						else if(player == 1)
						{
							playerB.update(2);
						}
						network.sendData("127.0.0.1",destPort, "A");
					  }

					  if (sf::Keyboard::IsKeyPressed(sf::Keyboard::S))
					  {			
						if(player == 2)
						{
							playerA.update(3);
						}

						else if(player == 1)
						{
							playerB.update(3);
						}
						network.sendData("127.0.0.1",destPort, "S");
					  }

					  if (sf::Keyboard::IsKeyPressed(sf::Keyboard::D))
					  {			
开发者ID:ClaudeMulhare,项目名称:Online-Gaming,代码行数:67,代码来源:SFMLStarter.cpp


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