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


C++ SDLNet_TCP_Send函数代码示例

本文整理汇总了C++中SDLNet_TCP_Send函数的典型用法代码示例。如果您正苦于以下问题:C++ SDLNet_TCP_Send函数的具体用法?C++ SDLNet_TCP_Send怎么用?C++ SDLNet_TCP_Send使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: putMsg

// send a string buffer over a TCP socket with error checking
// returns 0 on any errors, length sent on success
int putMsg(TCPsocket sock, char *buf)
{
	Uint32 len,result;

	if(!buf || !strlen(buf))
		return(1);

	// determine the length of the string
	len=strlen(buf)+1; // add one for the terminating NULL
	
	// change endianness to network order
	len=SDL_SwapBE32(len);

	// send the length of the string
	result=SDLNet_TCP_Send(sock,&len,sizeof(len));
	if(result<sizeof(len)) {
		if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
			printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
		return(0);
	}
	
	// revert to our local byte order
	len=SDL_SwapBE32(len);
	
	// send the buffer, with the NULL as well
	result=SDLNet_TCP_Send(sock,buf,len);
	if(result<len) {
		if(SDLNet_GetError() && strlen(SDLNet_GetError())) // sometimes blank!
			printf("SDLNet_TCP_Send: %s\n", SDLNet_GetError());
		return(0);
	}
	
	// return the length sent
	return(result);
}
开发者ID:XPaladin,项目名称:m337,代码行数:37,代码来源:tcputil.cpp

示例2: ConvertStringToVoidPtr

void TCPConnectionServer::Send( std::string str, int connectionNr  )
{
	if ( !isSocketConnected[ connectionNr ]  )
	{
		std::cout << "[email protected]" << __LINE__ << " Error! Not connected " << std::endl;
		return;
	}

	void* messageData = ConvertStringToVoidPtr(str);
	int messageSize = static_cast< int > ( str.length() );
	int bytesSent = 0;

	if ( isServer )
	{
		bytesSent = SDLNet_TCP_Send( serverSocket[connectionNr],  messageData,  messageSize);
	}
	else
	{
		bytesSent = SDLNet_TCP_Send( tcpSocket,  messageData,  messageSize);
	}

	if ( bytesSent < messageSize )
	{
		std::cout << "[email protected]" << __LINE__ << " Send failed : " << SDLNet_GetError() << std::endl;
		isSocketConnected[connectionNr] = false;
	}
}
开发者ID:olevegard,项目名称:DXBall,代码行数:27,代码来源:TCPConnectionServer.cpp

示例3: checkMouseMode3

//**************************************************************************
//*                   MODE 3 Lobby Screen                                  *
//**************************************************************************
static void checkMouseMode3(SDL_Event *event, SDL_Point *currentMouseLocation, SDL_Rect buttonPlacement[], int *select, int *mode, int modeMaxButtons[], bool *match, int *keyboardMode){
    for(int i=0; i < modeMaxButtons[3]; i++){
        if(mouseOnButton(currentMouseLocation, buttonPlacement, &i)){ // Is the mouse on button 'i' ?
            if(event->type == SDL_MOUSEBUTTONDOWN){
                *keyboardMode = ENTERING_TEXT;

                if(i == 2){                 // Ready
                    //playerReady = !playerReady;
                    SDLNet_TCP_Send(client.TCPSock, "#", strlen("#"));

                }
                else if(i == 1){            // Leave
                    isConnected = false;
                    SDLNet_TCP_Send(client.TCPSock, "-", strlen("-"));
                    SDLNet_TCP_Close(client.TCPSock);
                    SDLNet_UDP_Close(client.UDPRecvSock);
                    SDLNet_UDP_Close(client.UDPSendSock);

                    for(int i=0; i < MAX_PLAYERS; i++)
                        playerReady[i] = 0;

                    clearTextStrings(6);
                    printf("LEFT; '-' sent to server, socket closed, ready statuses cleared, textstrings cleared, mode changed\n"); //*****************************************
                    *mode = 5;
                }                           // (ELSE: Enter Chat Message Window Pressed)
            }
        }
    }
}
开发者ID:JonatanWang,项目名称:LearnMongoDB,代码行数:32,代码来源:inputhandler.c

示例4: SDLNet_Write32

void NetworkCommandBuffer::SendString ( const std::string message )
{
	char buffer[4];
	SDLNet_Write32(message.size(), buffer);
	SDLNet_TCP_Send ( socket, buffer, 4 );
	SDLNet_TCP_Send ( socket, const_cast<char*> ( message.data() ), message.size() );
}
开发者ID:Gamer2k4,项目名称:basinrogue,代码行数:7,代码来源:networkcommandbuffer.cpp

示例5: SDLNet_TCP_Accept

void NetServer::handleserver()
{
	int which;
	unsigned char data;

	TCPsocket newsock = SDLNet_TCP_Accept(tcpsock);

	if (newsock == NULL)
		return;
	
	/* Look for unconnected person slot */
    for (which = 0; which < MAXCLIENTS; ++which) {
        if (!clients[which].sock) {
			break;
		}
	}

    if (which == MAXCLIENTS) {
		/* Look for inactive person slot */
        for (which = 0; which < MAXCLIENTS; ++which) {
            if (clients[which].sock && !clients[which].active) {
				/* Kick them out.. */
				data = NET_MSG_REJECT;
				SDLNet_TCP_Send(clients[which].sock, &data, 1);
				SDLNet_TCP_DelSocket(socketset, clients[which].sock);
				SDLNet_TCP_Close(clients[which].sock);
				numclients--;

#ifdef _DEBUG
				printf("Killed inactive socket %d\n", which);
#endif
				break;
			}
		}
	}

    if (which == MAXCLIENTS) {
		/* No more room... */
		data = NET_MSG_REJECT;
		SDLNet_TCP_Send(newsock, &data, 1);
		SDLNet_TCP_Close(newsock);

#ifdef _DEBUG
		printf("Connection refused -- server full\n");
#endif
    } else {
		/* Add socket as an inactive person */
		clients[which].sock = newsock;
		clients[which].peer = *SDLNet_TCP_GetPeerAddress(newsock);
		SDLNet_TCP_AddSocket(socketset, clients[which].sock);
		numclients++;

#ifdef _DEBUG
		printf("New inactive socket %d\n", which);
#endif
	}
}
开发者ID:hu9o,项目名称:smw-next,代码行数:57,代码来源:net.cpp

示例6: HandleServer

void HandleServer(void)
{
	TCPsocket newsock;
	int which;
	unsigned char data;

	newsock = SDLNet_TCP_Accept(servsock);
	if ( newsock == NULL ) {
		return;
	}

	/* Look for unconnected person slot */
	for ( which=0; which<CHAT_MAXPEOPLE; ++which ) {
		if ( ! people[which].sock ) {
			break;
		}
	}
	if ( which == CHAT_MAXPEOPLE ) {
		/* Look for inactive person slot */
		for ( which=0; which<CHAT_MAXPEOPLE; ++which ) {
			if ( people[which].sock && ! people[which].active ) {
				/* Kick them out.. */
				data = CHAT_BYE;
				SDLNet_TCP_Send(people[which].sock, &data, 1);
				SDLNet_TCP_DelSocket(socketset,
						people[which].sock);
				SDLNet_TCP_Close(people[which].sock);
#ifdef DEBUG
	fprintf(stderr, "Killed inactive socket %d\n", which);
#endif
				break;
			}
		}
	}
	if ( which == CHAT_MAXPEOPLE ) {
		/* No more room... */
		data = CHAT_BYE;
		SDLNet_TCP_Send(newsock, &data, 1);
		SDLNet_TCP_Close(newsock);
#ifdef DEBUG
	fprintf(stderr, "Connection refused -- chat room full\n");
#endif
	} else {
		/* Add socket as an inactive person */
		people[which].sock = newsock;
		people[which].peer = *SDLNet_TCP_GetPeerAddress(newsock);
		SDLNet_TCP_AddSocket(socketset, people[which].sock);
#ifdef DEBUG
	fprintf(stderr, "New inactive socket %d\n", which);
#endif
	}
}
开发者ID:BarleyStudio,项目名称:CorsixTH_Android,代码行数:52,代码来源:chatd.c

示例7: _

bool CDataSocket::flush()
{
	if (!m_socket)
	{
		CLog::instance()->log(CLog::msgFlagNetwork, CLog::msgLvlError, _("Attempt to write to not initialized data socket.\n"));
		return true;
	}
	
	if( time(NULL) - m_lastPong > LOST_CONNECTION_TIME )
	{
		m_lastPong = time(NULL);
		CLog::instance()->log(CLog::msgFlagNetwork,CLog::msgLvlInfo,_("(%s:%d) : Ping timeout.\n"),m_remoteHost.c_str(),m_remotePort);
		m_outputBuffer.clear();
		return false;
	}

	// if it's time to send ping insert dummy packet into output buffer
	if( !m_outputBuffer.size() && (time(NULL) - m_lastPing > PING_TIME) )
		m_outputBuffer.push_back(CData());

	m_lastPing = time(NULL);
	std::vector<CData>::iterator i;
	for(i = m_outputBuffer.begin(); i != m_outputBuffer.end(); i++ )
	{
		if( (*i).length() > SOCK_BUFFER_SIZE )
		{
			CLog::instance()->log(CLog::msgLvlError,_("Data too long (%d).\n"),(*i).length());
			continue;
		};
		netPacketSize dataLength = (netPacketSize)(*i).length();
		if ( SDLNet_TCP_Send(m_socket, &dataLength, sizeof(netPacketSize)) <= 0 )
		{
			CLog::instance()->log(CLog::msgFlagNetwork,CLog::msgLvlInfo,_("(%s:%d) : Error occured while writing socket.\n"), m_remoteHost.c_str(), m_remotePort);
			m_sockErrorOccured = true;
			m_outputBuffer.clear();
			return false;
		}

		if( dataLength )
			if( SDLNet_TCP_Send(m_socket, (char*)(*i).data(), dataLength ) <= 0 )
			{
				CLog::instance()->log(CLog::msgFlagNetwork,CLog::msgLvlInfo,_("(%s:%d) : Error occured while writing socket.\n"), m_remoteHost.c_str(), m_remotePort);
				m_sockErrorOccured = true;
				m_outputBuffer.clear();
				return false;
			}
	}
	m_outputBuffer.clear();
	return true;
}
开发者ID:proton,项目名称:ireon,代码行数:50,代码来源:sock_layer.cpp

示例8: messages

void NetworkManager::NetworkCommunicator()    //make it receive paddlemanagers and ballmanagers to change positions into messages (break up server messages since it'll send ball and paddle to network... easy to write, not to read)
{                           //call this from game loop
	int len;				//	commented out code should be correct (I can't check it yet though) once networking works right
	char buffer[512];	
	//
	char str[512];
	if(!server)
	{
		//strcpy(buffer,NetworkManager::Vector3ToString(clientPaddle->getPosition()));
		len = strlen(buffer) + 1;
		if (SDLNet_TCP_Send(targetSocket, (void *)buffer, len) < len)
		{
			fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}
		if (SDLNet_TCP_Recv(targetSocket, buffer, 512) > 0)
		{
			std::string decode = std::string(buffer);
			unsigned found = decode.find_first_of("&");
			Ogre::Vector3 v = NetworkManager::StringToVector3(buffer,0);
			//hostPaddle->setPosition(v);
			v = NetworkManager::StringToVector3(buffer,found+1);
			//ball->setPosition(v);
			printf("Host say: %s\n", buffer);
		
		}
	}
	else
	{

		if (SDLNet_TCP_Recv(targetSocket, buffer, 512) > 0)
		{
			Ogre::Vector3 v = NetworkManager::StringToVector3(buffer,0);
			//clientPaddle->setPosition(v);
			printf("Client say: %s\n", buffer);
		}
		//strcpy(str, NetworkManager::Vector3ToString(hostPaddle->getPosition()));
		strcat(str, " ");
		//strcat(str, NetworkManager::Vector3ToString(ball->getPosition()));
		strcpy(buffer,str);
		//strcpy(buffer,"test reply");
		int len = strlen(buffer) + 1;
		if (SDLNet_TCP_Send(targetSocket, (void *)buffer, len) < len)
		{
			fprintf(stderr, "SDLNet_TCP_Send: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}
	}
}
开发者ID:mm6281726,项目名称:DodgeBall,代码行数:49,代码来源:NetworkManager.cpp

示例9: Exception

void IRCLobby::sendIRCLine(const std::string& line)
{
    if(SDLNet_TCP_Send(irc_server_socket,
                const_cast<char*> (line.c_str()),
                line.size()) != (int) line.size())
        throw Exception("Error when sending irc message '%s': %s",
                line.c_str(), SDLNet_GetError());
#ifndef WITHOUT_NETPANZER
    LOGGER.debug("sending irc:%s",line.c_str());
#endif
    static char lf[]="\n";
    if(SDLNet_TCP_Send(irc_server_socket,lf,1) != 1)
        throw Exception("Error when sending irc lf: %s",
                SDLNet_GetError());
}
开发者ID:BackupTheBerlios,项目名称:netpanzer-svn,代码行数:15,代码来源:IRCLobby.cpp

示例10: main_thread

void * main_thread(void * data)
{
	int flag=1,choice,n;
	int temp;
	TCPsocket client_socket,server_socket;
	if(!intialize_thread())
	{
		printf("\n Therer is some error while initializing thread");
		return 0;
	 }
	printf("\n Value of active ports is:%d",get_active_threads());

	// first server receive request from client to connect and open master server socket. To be created only once.-permanent 
	if(common_connect_server(&host_ipaddress,&server_socket,global_port,(const char *)NULL)==0)
		return (void *)1;
	while(quit) 
	{
		// Open client socket on server side for sending dynamic port address-temprary
		if((client_socket=SDLNet_TCP_Accept(server_socket)))
		{
 			// send port address to client
			pthread_mutex_lock(&mutex_variable);
			printf("\n Value of active ports:%d",get_active_threads());
			if(get_active_threads()==0)
			{
				int temp=0;
				SDLNet_TCP_Send(client_socket,&(temp),2);
				SDLNet_TCP_Close(client_socket);
				pthread_mutex_unlock(&mutex_variable);
			}
			else
				if(SDLNet_TCP_Send(client_socket,&(thread_header->client.port),2)==2)
				{
					printf("\nNew Port is send to client"); 
					// close temprary client socket 
					SDLNet_TCP_Close(client_socket);
					// opening port so that server can accept content from client in different thread;
					pthread_mutex_unlock(&mutex_variable);
					printf("\n Activating thread");
					activate_thread();
				}
		}
	}
		printf("\nEverything is OK now exiting");	
		SDLNet_TCP_Close(server_socket);
		cleanup_thread();
		return NULL;
}
开发者ID:deepakagg,项目名称:game-server,代码行数:48,代码来源:server_handle.c

示例11: throw

int Server::script_client(void* data) throw(const char*){
	Data_scriptClient* _data_cast=static_cast<Data_scriptClient*>(data);
	if(_data_cast==NULL){
		throw "Impossibile eseguire il casting dei dati nello script client!";
	}
	int bytes_rcv;
	char buffer[MAX_BYTES_BUFFER];
	std::string data_store_rcv;
	std::string data_store_snd;
	int result;
	AppProtServer client_prot;
	while(_data_cast->exit==false){
		bytes_rcv=SDLNet_TCP_Recv(_data_cast->connessione, buffer, MAX_BYTES_BUFFER);
		if(bytes_rcv<=0){
			_data_cast->exit=true;
		}else{
			data_store_rcv.append(buffer,bytes_rcv);
			result=client_prot.ElaboraRichiesta(data_store_rcv,data_store_snd);
			if(result){
				int bytes_snd=SDLNet_TCP_Send(_data_cast->connessione, data_store_snd.data(), data_store_snd.size());
				if(bytes_snd!=data_store_snd.size()){
					_data_cast->exit=true;
				}
			}
			if(result==-1){
				_data_cast->exit=true;
			}
		}
	}
	return 0;
}
开发者ID:neo333,项目名称:TesinaRc_AuthProt,代码行数:31,代码来源:Server.cpp

示例12: clientConnection

int clientConnection(TCPsocket *socketPekare)
{
    client_send_information clientSendData;
    client_recieve_information clientRecieveData;

    int len1,len2;
    len1 = sizeof(clientSendData);
    len2 = sizeof(clientRecieveData);
    char serialiseradStruct1[len1];
    char serialiseradStruct2[len2];

    while(1)
    {
        SDL_Delay(10);

        convertSend(&clientSendData);
        memcpy(&serialiseradStruct1, &clientSendData, len1);
        SDLNet_TCP_Send(*socketPekare, &serialiseradStruct1, len1);

        SDL_Delay(10);

        SDLNet_TCP_Recv(*socketPekare,&serialiseradStruct2,len2);
        memcpy(&clientRecieveData,&serialiseradStruct2,len2);
        convertRecieve(&clientRecieveData);
    }

    return 0;
}
开发者ID:pnlindgren,项目名称:Projektarbete-Internet-Grupp4,代码行数:28,代码来源:tcp_socket.c

示例13: network_tcp_send

int network_tcp_send(TCPsocket tcp, const void* data, int len) {
        int r = SDLNet_TCP_Send(tcp, data, len);
        if (r < len) {
                std::cerr << "Failed to send full data over TCP: " << SDLNet_GetError() << "\n";
        }
        return r;
}
开发者ID:TheButlah,项目名称:BasicEventEngine,代码行数:7,代码来源:network.hpp

示例14: SDLNet_TCP_Send

void MySocketConnection::write(const std::vector<unsigned char>& bytes, int length) {
	int result = SDLNet_TCP_Send(this->socket, &bytes[0], length);
	// _LOGDATA("Socket data send");
	if (result < length) {
		throw WAException(std::string(SDLNet_GetError()), WAException::SOCKET_EX, WAException::SOCKET_EX_SEND);
	}
}
开发者ID:KaSt,项目名称:mojogram,代码行数:7,代码来源:MySocketConnection.cpp

示例15: SDLNet_TCP_Send

void
ServerConnection::send(const std::string& str)
{
    if (tcpsock)
    {
        int result = SDLNet_TCP_Send(tcpsock, const_cast<char*>(str.c_str()), str.length());
        if(result < int(str.length()))
        {
            printf( "SDLNet_TCP_Send: %s\n", SDLNet_GetError() );
            // It may be good to disconnect sock because it is likely invalid now.
        }
    }
    else
    {   // Not connected, so directly procses the command without a
        // round trip through the server
        std::string tmp;
        for(std::string::size_type i = 0; i < str.length(); ++i)
        {
            if (str[i] == '\n')
            {
                process_command("client 0 " + tmp);
                tmp.clear();
            }
            else
                tmp += str[i];
        }
    }
}
开发者ID:Grumbel,项目名称:netbrush,代码行数:28,代码来源:server_connection.cpp


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