本文整理汇总了C++中SDLNet_TCP_AddSocket函数的典型用法代码示例。如果您正苦于以下问题:C++ SDLNet_TCP_AddSocket函数的具体用法?C++ SDLNet_TCP_AddSocket怎么用?C++ SDLNet_TCP_AddSocket使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SDLNet_TCP_AddSocket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LOG_MSG
TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) {
#ifdef NATIVESOCKETS
nativetcpstruct=0;
#endif
sendbuffer=0;
isopen = false;
if(!SDLNetInited) {
if(SDLNet_Init()==-1) {
LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError());
return;
}
SDLNetInited = true;
}
mysock=0;
listensocketset=0;
IPaddress openip;
//Ancient versions of SDL_net had this as char*. People still appear to be using this one.
if (!SDLNet_ResolveHost(&openip,const_cast<char*>(destination),port)) {
listensocketset = SDLNet_AllocSocketSet(1);
if(!listensocketset) return;
mysock = SDLNet_TCP_Open(&openip);
if(!mysock) return;
SDLNet_TCP_AddSocket(listensocketset, mysock);
isopen=true;
}
}
示例2: while
bool TCPConnectionServer::StartServer( )
{
if (!isServer )
{
std::cout << "[email protected]" << __LINE__ << " Not in server mode, can't start server" << std::endl;
return false;
}
bool quit = false;
while ( !quit )
{
if ( AcceptConnection() )
{
std::cout << "[email protected]" << __LINE__ << "Accepted connection...\n";
if ( SetServerSocket() )
{
SDLNet_TCP_AddSocket( socketSet, serverSocket[ serverSocket.size() - 1] );
quit = true;
return true;
}
}
SDL_Delay( 20 );
}
return false;
}
示例3: printf
bool NetClient::connecttoserver()
{
//if(SDLNet_ResolveHost(&ip, game_values.hostaddress, 12521) == -1)
//if(SDLNet_ResolveHost(&ip, "10.115.8.222", 12521) == -1)
//if(SDLNet_ResolveHost(&ip, "10.115.5.65", 12521) == -1)
if(SDLNet_ResolveHost(&ip, "192.168.0.2", 12521) == -1)
//if(SDLNet_ResolveHost(&ip, "127.0.0.1", 12521) == -1)
{
printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
return false;
}
tcpsock = SDLNet_TCP_Open(&ip);
if(!tcpsock) {
printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
return false;
}
/* Allocate the socket set for polling the network */
socketset = SDLNet_AllocSocketSet(1);
if (socketset == NULL) {
printf("Couldn't create socket set: %s\n", SDLNet_GetError());
return false;
}
SDLNet_TCP_AddSocket(socketset, tcpsock);
return true;
}
示例4: while
int game_host::waitForClient_test()
{
cout << "waiting for first player to connect...\n";
while(!(player1sd = SDLNet_TCP_Accept(sd))) //wait for first connection, with 50ms delay to keep CPU down
SDL_Delay(50);
string ipMess = "";
while(SDLNet_UDP_Recv(p1UDPsock, UDPpack1) == 0)
SDL_Delay(5);
SDLNet_UDP_Bind(p1UDPsock,10,&(UDPpack1->address));
cout << this->recieveMessagep1() << "\n"; //on successful connect, client sends a message
SDLNet_TCP_AddSocket(socketset, player1sd); //could error check here
this->player1ip = SDLNet_TCP_GetPeerAddress(player1sd);
//SDLNet_ResolveHost(&(UDPpack1->address), cl.c_str(), port);
//player2ip->host = SDLNet_TCP_GetPeerAddress(player1sd)->host;
//player2ip->port = port;
//UDPpack1->address = *player1ip;
sendtoP1_test("1SIG:START");
cout << "client connected, continuing...\n";
return 1;
}
示例5: setup_server
/* Setup TCP Server, and wait for a single
* client to connect */
int setup_server(unsigned port) {
#if !defined(PSP) && !defined(EMSCRIPTEN) && !defined(DREAMCAST) && !defined(THREE_DS)
is_server = 1;
log_message(LOG_INFO, "Starting server on port %u\n",port);
//SDL_INIT(SDL_INIT_EVERYTHING);
SDLNet_Init();
IPaddress ip;
SDLNet_ResolveHost(&ip, NULL, port);
server = SDLNet_TCP_Open(&ip);
log_message(LOG_INFO, "Waiting for client to connect\n");
while (client == NULL) {
client = SDLNet_TCP_Accept(server);
SDL_Delay(1000);
}
const char * message = "Welcome to GB server";
SDLNet_TCP_Send(client, message, strlen(message) + 1);
log_message(LOG_INFO, "Client successfully connected\n");
socketset = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(socketset, client);
connection_up = 1;
return 1;
#endif
return 0;
}
示例6: new_connection
void new_connection(TCPsocket sock) {
TCPsocket tmp;
struct socket_node *socket;
IPaddress *ip;
/* accept the connection temporarily */
tmp = SDLNet_TCP_Accept(sock);
if (!tmp)
do_error(SDLNet_GetError());
/* are we full or game already started? */
if (get_active_players() + 1 > NUM_PLAYERS || current_turn)
SDLNet_TCP_Close(tmp);
else {
/* nope! */
socket = new_socket(SOCKET_CLIENT);
socket->sock = tmp;
if (SDLNet_TCP_AddSocket(sockset, tmp) < 0)
do_error(SDLNet_GetError());
ip = SDLNet_TCP_GetPeerAddress(tmp);
if (!ip)
do_error(SDLNet_GetError());
if (SDLNet_ResolveIP(ip))
strcpy(socket->host, SDLNet_ResolveIP(ip));
else
sprintf(socket->host, "Unknown IP");
join_player(socket); /* add player to game */
}
}
示例7: disconnect
bool IRC::connect(const std::string &serverName, int serverPort, const std::string &nick)
{
disconnect();
IPaddress ip;
socketSet = SDLNet_AllocSocketSet(1);
if (SDLNet_ResolveHost(&ip, (char *)serverName.c_str(), serverPort)==-1)
{
fprintf(stderr, "YOG : ResolveHost: %s\n", SDLNet_GetError());
return false;
}
socket = SDLNet_TCP_Open(&ip);
if (!socket)
{
fprintf(stderr, "YOG : TCP_Open: %s\n", SDLNet_GetError());
return false;
}
SDLNet_TCP_AddSocket(socketSet, socket);
// Here we change the nick on yog for the IRC
// changing from nick = "nick" to YOGnick = "YOGnick"
this->nick = "[YOG]" + nick;
sendString("USER " + this->nick + " undef undef " + PACKAGE_TARNAME + "-" + PACKAGE_VERSION);
sendString("NICK " + this->nick);
return true;
}
示例8: accept_connections
void accept_connections()
{
if (SDLNet_SocketReady(serversock))
{
TCPsocket client = SDLNet_TCP_Accept(serversock);
if (client)
{
clients.push_back(new ClientConnection(clients.size()+1, client));
std::stringstream out;
out << "version 0" << std::endl;
out << "your_id " << clients.size() << std::endl;
std::string str = out.str();
SDLNet_TCP_Send(client, const_cast<char*>(str.c_str()), str.length()+1);
IPaddress* ip = SDLNet_TCP_GetPeerAddress(client);
const char* host;
if((host = SDLNet_ResolveIP(ip)) != 0)
std::cout << "# Got client connection from " << host << " " << ip->port << std::endl;
else
std::cout << "# Got client connection from " << ip->host << " " << ip->port << std::endl;
int numused = SDLNet_TCP_AddSocket(socketset, client);
if (numused == -1) {
printf("SDLNet_AddSocket: %s\n", SDLNet_GetError());
// perhaps you need to restart the set and make it bigger...
}
else
{
std::cout << "# Sockets used: " << numused << std::endl;
}
}
}
}
示例9: strcpy
int TCP_RECEIVEOBJ::connect()
{
sock=0;
if(SDLNet_ResolveHost(&ip, host, PORT) == -1)
{
strcpy(szdata, "SDLNet_ResolveHost: "); strcat(szdata, SDLNet_GetError());
report_error(szdata); return(FALSE);
}
sock = SDLNet_TCP_Open(&ip);
if(!sock)
{
strcpy(szdata, "SDLNet_TCP_Open: "); strcat(szdata, SDLNet_GetError());
report_error(szdata);return(FALSE);
}
set = SDLNet_AllocSocketSet(1);
if(!set)
{
strcpy(szdata,"SDLNet_AllocSocketSet: "); strcat(szdata, SDLNet_GetError());
report_error(szdata);return(FALSE);
}
if (SDLNet_TCP_AddSocket(set, sock) == -1)
{
strcpy(szdata,"SDLNet_TCP_AddSocket: ");strcat(szdata, SDLNet_GetError());
report_error(szdata);return(FALSE);
}
if (!sock) return(FALSE);
cout<<"connected.";
return(TRUE);
}
示例10: ScopedMutexLock
void Connection::makeSet(int max){
ScopedMutexLock(pimpl_->dataAccess);
// remove from old group
if( pimpl_->group && pimpl_->userSocket ){
SDLNet_TCP_DelSocket(pimpl_->group, pimpl_->userSocket);
}
// if internal set was used, destroy it
if( pimpl_->createdSet ) {
SDLNet_FreeSocketSet(pimpl_->group);
pimpl_->group = NULL;
pimpl_->createdSet = false;
}
//create new set
pimpl_->group = SDLNet_AllocSocketSet(max);
if( pimpl_->group == NULL ){
std::cerr << __FILE__ << " " << __LINE__ << ": " << "SDLNet_AllocSocketSet(" << max << "): " << SDLNet_GetError() << std::endl;
exit(EXIT_FAILURE);
} else {
// add self to new group
if( pimpl_->userSocket ){
SDLNet_TCP_AddSocket(pimpl_->group, pimpl_->userSocket);
}
pimpl_->createdSet = 1;
}
}
示例11: fprintf
/**************************************
* CLIENT
*************************************/
int BomberNetClient::connectClient(SDL_Surface * vout_buf) {
errorCode = 0;
IPaddress serverIP;
fprintf(stderr, "%s\n", GameConfig::Instance().getIpString());
viewer = new ClientViewer(vout_buf);
startKeystateThread();
SDLNet_ResolveHost(&serverIP, GameConfig::Instance().getIpString(), GameConfig::Instance().getPortValue());
if (serverIP.host == INADDR_NONE) {
fprintf(stderr, "Couldn't resolve hostname\n");
cleanup();
} else {
/* If we fail, it's okay, the GUI shows the problem */
tcpsock = SDLNet_TCP_Open(&serverIP);
if (tcpsock == NULL) {
fprintf(stderr, "Connect failed\n");
cleanup();
return 1;
} else {
fprintf(stderr, "Connection established !\n");
socketset = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(socketset, tcpsock);
alive = true;
net_thread = SDL_CreateThread(net_thread_main, "tcp client thread", tcpsock);
return 0;
}
}
return 2;
}
示例12: printf
void
ServerConnection::connect(const char* hostname, Uint16 port)
{
IPaddress ip;
if(SDLNet_ResolveHost(&ip, hostname, port) == -1)
{
printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
exit(1);
}
tcpsock = SDLNet_TCP_Open(&ip);
if(!tcpsock)
{
printf("SDLNet_TCP_Open: %s %s:%d\n", SDLNet_GetError(), hostname, port);
exit(2);
}
else
{
std::string line = "client_version 1\n";
SDLNet_TCP_Send(tcpsock, const_cast<char*>(line.c_str()), line.length());
socketset = SDLNet_AllocSocketSet(1);
SDLNet_TCP_AddSocket(socketset, tcpsock);
}
}
示例13: setup_client
/* Setup TCP Client, and attempt to connect
* to the server */
int setup_client(unsigned port) {
#if !defined(PSP) && !defined(EMSCRIPTEN) && !defined(DREAMCAST) && !defined(THREE_DS)
is_client = 1;
log_message(LOG_INFO, "Attempting to connect to server on port %u\n",port);
//SDL_INIT(SDL_INIT_EVERYTHING);
SDLNet_Init();
IPaddress ip;
//TODO, for now always connect to localhost, fix for any specified ip in
//the future
SDLNet_ResolveHost(&ip, "localhost", port);
client = SDLNet_TCP_Open(&ip);
socketset = SDLNet_AllocSocketSet(1);
char buf[100];
int i = SDLNet_TCP_Recv(client, buf, 100);
for (int j = 0; j < i; j++) {
printf("%c",buf[j]);
}
printf("\n");
SDLNet_TCP_AddSocket(socketset, client);
connection_up = 1;
return 1;
#endif
return 0;
}
示例14: SDLNet_AllocSocketSet
bool NetServer::startserver()
{
socketset = SDLNet_AllocSocketSet(MAXCLIENTS + 1);
if (socketset == NULL) {
printf("Couldn't create socket set: %s\n", SDLNet_GetError());
return false;
}
SDLNet_ResolveHost(&ip, NULL, 12521);
printf("Server IP: %x:%d\n", ip.host, ip.port);
tcpsock = SDLNet_TCP_Open(&ip);
if (tcpsock == NULL) {
cleanup();
printf("Couldn't create server socket: %s\n", SDLNet_GetError());
return false;
}
SDLNet_TCP_AddSocket(socketset, tcpsock);
return true;
}
示例15: error
Socket::Socket(TCPsocket sock) : error(false), sock(sock) {
set = SDLNet_AllocSocketSet(1);
if (set == NULL)
error = true;
else
SDLNet_TCP_AddSocket(set, sock);
}