Example #1
0
void SocketThread::start()
{

    char* json = SocketManager::getInstance()->msgVector.front();
    
    TCPSocket *ts = SocketManager::getInstance()->ts;
    
    ts->Create(AF_INET,SOCK_STREAM,0);
    bool iscon=ts->Connect(SocketManager::getInstance()->getAddr(),SocketManager::getInstance()->getPort());
    
    if (iscon)
    {
        int len = strlen(json);
        len = len + 5;
        ByteArray *ba = new ByteArray(len);
        ba->writeInt(strlen(json));
        ba->writeByte(0);
        ba->writeString(json);
        ts->Send(ba->getByteArray(), len);
    } else {
        printf("TCPSOCKET ERROR : connect failed!");
    }
    
    char* buff;
    ts->Recv(buff);

    resposeMsg(buff);
}
Example #2
0
int main(int argc, char * *argv)
{
	console = new Console();

	/* Begin your application here. */
	int cc_err;

	TCPSocket * sock = new TCPSocket();
	console->WriteLine("Connecting...");
	cc_err = sock->Connect("www.example.com", 80);
	if (cc_err == CC_ERR_WOULD_BLOCK)
		console->WriteLine("Non-blocking sockets are enabled. Waiting for connection...");
	while (sock->State() == SOCKET_STATE_CONNECTING) ;
	if (sock->State() != SOCKET_STATE_CONNECTED) {
		console->WriteLine("Connection failed.");
		return 1;
	} else
		console->WriteLine("Connection seems OK.");
	console->Write("Requesting http://www.example.com/... ");
	cc_err = sock->Send("GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n");
	if (cc_err < 1)
		console->WriteLine("Failed.");
	else
		console->WriteLine("OK");

	while (sock->State() == SOCKET_STATE_CONNECTED) {
		string in;
		cc_err = sock->Read(in);
		if (cc_err == 0) {
			console->WriteLine(in);
			break;
		} else {
			if (cc_err != CC_ERR_WOULD_BLOCK)
				console->WriteLine("Read error %d (%s)", cc_err, GetErrorDescription(cc_err));
			System::ThreadSleep(10);
		}
	}

	sock->Close();

	delete sock;

	/* End your application here. */

#ifdef TARGET_OS_WINDOWS
	system("pause");
#endif

	delete console;
	return 0;
}
int main(int argc, char** argv)
{
	// use non-default port if provided
	if (argc > 1)
		*(const_cast<unsigned short*>(&SERVER_PORT)) = atoi(argv[1]);
	
	if (!Init())
	{
		printf("Failed to initialize.\n");
		return 1;
	}
	
	// prepare for peer to peer
	PeerAuthClientState aPeerAuthClientState;
	unsigned short aRequestBufLen = 0;
	const void* aRequestBufP = NULL;

	// create Auth1Request
	Error aError = aPeerAuthClientState.Init(gIdentity, &aRequestBufP, &aRequestBufLen);
	printf("PeerAuthClientState::Init: %s.\n", WONErrorToString(aError));
	if (aError != Error_Success) return aError;

	// connect to server
	IPSocket::Address aServerAddress("localhost", SERVER_PORT);
	TCPSocket aServerCommSocket;
	aError = aServerCommSocket.Open(aServerAddress);
	printf("Connecting to server at %s: %s.\n", aServerAddress.GetAddressString().c_str(), WONErrorToString(aError));
	if (aError != Error_Success) return aError;

	// send Auth1Request
	aServerCommSocket.Send(2, &aRequestBufLen);
	aServerCommSocket.Send(aRequestBufLen, aRequestBufP);
	printf("Sent request.\n");

	// receive server response (Auth1Challenge1 or Auth1Complete)
	unsigned short aChallenge1BufLen = 0;
	aServerCommSocket.Recv(2, &aChallenge1BufLen);
	void* aChallenge1BufP = new unsigned char[aChallenge1BufLen];
	aServerCommSocket.Recv(aChallenge1BufLen, aChallenge1BufP);
	printf("Received server->client challenge.\n");

	// process server response:
	//  if Auth1Complete, we're done
	//  if Auth1Challenge1, generate Auth1Challenge2
	unsigned short aChallenge2BufLen = 0;
	const void* aChallenge2BufP = NULL;
	aPeerAuthClientState.RecvChallenge1(aChallenge1BufP, aChallenge1BufLen,
	                                    &aChallenge2BufP, &aChallenge2BufLen);
	printf("PeerAuthClientState::RecvChallenge1: %s.\n", WONErrorToString(aError));
	if (aError != Error_Success) return aError;

	// send Auth1Challenge2
	aServerCommSocket.Send(2, &aChallenge2BufLen);
	aServerCommSocket.Send(aChallenge2BufLen, aChallenge2BufP);
	printf("Sent client->server challenge.\n");

	// receive Auth1Complete
	unsigned short aResultBufLen = 0;
	aServerCommSocket.Recv(2, &aResultBufLen);
	void* aResultBufP = new unsigned char[aResultBufLen];
	aServerCommSocket.Recv(aResultBufLen, aResultBufP);	
	printf("Received result.\n");

	// process Auth1Complete
	aError = aPeerAuthClientState.RecvResult(aResultBufP, aResultBufLen);
	printf("PeerAuthClientState::RecvResult: %s.\n", WONErrorToString(aError));
	return aError;
}
Example #4
0
int main( int argc, char *argv[] )
{
#ifdef TEST_TCP_SOCKETS

  StartWinSock();

  TCPSocket listener;
  TCPSocket client;

  char *localIP = NULL;
  GetLocalIP( localIP );

  listener.SetIP( localIP );
  listener.SetPortNumber( 8000 );

  listener.Init();
  
  listener.Listen();

  while( !listener.Accept( client ) );
  
  while( true )
  {
    std::string message;
    Prompt( message, "Enter a message" );

    NetworkMessage netMessage;
    netMessage.msg_.Assign( message.c_str(), message.size() );

    client.Send( netMessage );
  }

  client.Shutdown();
  client.Close();

  listener.Shutdown();
  listener.Close();

  CloseWinSock();

#endif

  /*
  GameControl controller;

  try
  {
    controller.Init();

    try
    {
      controller.Run();
    }
    catch( BaseErrExcep e )
    {
      e.Print();
    }

    controller.Close();
  }
  catch( BaseErrExcep e )
  {
    e.Print();
  }
  */

    FileServer fileServer;

    try
    {
        fileServer.Init();

        try
        {
            fileServer.Run();
        }
        catch(BaseErrExcep& e)
        {
            e.Print();
        }

        fileServer.Close();
    }
    catch(BaseErrExcep& e)
    {
        e.Print();
    }
 
   
  printf( "\n Server is closing \n" );

  return WaitForInput();
}