Forum


Notice to all users

We are migrating towards a new forum system located at community.teamspeak.com, as such this forum will become read-only on January 29, 2020

Results 1 to 4 of 4
  1. #1
    Join Date
    July 2016
    Posts
    7

    Can't open port for plugin socket

    Good day.
    I am currently writing a plugin that will be the agent for controlling the TeamSpeak application. It uses the Plugin SDK API 22

    Unfortunately, my job was to try to run a socket in the middle of the plugin. It looks more like this:
    Code:
    int ts3plugin_init() {
        char appPath[PATH_BUFSIZE];
        char resourcesPath[PATH_BUFSIZE];
        char configPath[PATH_BUFSIZE];
    	char pluginPath[PATH_BUFSIZE];
    	/*
    	Socket
    	*/
    	int socket_desc;
    	struct sockaddr_in server;
    	//Create socket
    	socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    	if (socket_desc == -1)
    	{
    	    printf("ERROR: Could not create socket\n");
    	} else {
    		printf("Socket create succesfull\n");
    	}
    	     
    	//Prepare the sockaddr_in structure
    	server.sin_family = AF_INET;
    	server.sin_addr.s_addr = INADDR_ANY;
    	server.sin_port = htons( 2322 );
    	     
    	//Bind
    	if (bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) == -1) {
    	    puts("ERROR: bind failed\n");
    	} else {
    		puts("bind done\n");
    	}
        
    	/*
    	End Socket
    	*/
        /* Your plugin init code here */
        printf("PLUGIN: init\n");
    
        /* Example on how to query application, resources and configuration paths from client */
        /* Note: Console client returns empty string for app and resources path */
        ts3Functions.getAppPath(appPath, PATH_BUFSIZE);
        ts3Functions.getResourcesPath(resourcesPath, PATH_BUFSIZE);
        ts3Functions.getConfigPath(configPath, PATH_BUFSIZE);
    	ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE, pluginID);
    
    	printf("PLUGIN: App path: %s\nResources path: %s\nConfig path: %s\nPlugin path: %s\n", appPath, resourcesPath, configPath, pluginPath);
    
        return 0;  /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
    	/* -2 is a very special case and should only be used if a plugin displays a dialog (e.g. overlay) asking the user to disable
    	 * the plugin again, avoiding the show another dialog by the client telling the user the plugin failed to load.
    	 * For normal case, if a plugin really failed to load because of an error, the correct return value is 1. */
    }

    Of course all the required libraries I've defined earlier. After trying to run the plugin it gets the following messages:
    Code:
    Socket create succesfull
    ERROR: bind failed
    Of course I tried to fire an identical socket as a separate application and it worked without problems
    Here is the code application

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #define PORT 3490
    #define BACKLOG 10
    int main() {
            struct sockaddr_in server;
            struct sockaddr_in dest;
            int status,socket_fd, client_fd,num;
            socklen_t size;
    
            char buffer[10241];
            char *buff;
            //  memset(buffer,0,sizeof(buffer));
            int yes =1;
    
    
    
            if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
                fprintf(stderr, "Socket failure!!\n");
                exit(1);
            }
    
            if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
                perror("setsockopt");
                exit(1);
            }
            memset(&server, 0, sizeof(server));
            memset(&dest,0,sizeof(dest));
            server.sin_family = AF_INET;
            server.sin_port = htons(PORT);
            server.sin_addr.s_addr = INADDR_ANY;
            if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1)    { //sizeof(struct sockaddr)
                fprintf(stderr, "Binding Failure\n");
            } else {
                    fprintf(stderr, "Binding succesfull\n");
            }
    }
    I check and the port is not blocked in any way, or user by other applications

    Why I can't bind ports for plugin socket?

    //EDIT

    Sory i have bug in my code:
    It is new code with support thread:
    Code:
    void *socket_start();
    int ts3plugin_init() {
        char appPath[PATH_BUFSIZE];
        char resourcesPath[PATH_BUFSIZE];
        char configPath[PATH_BUFSIZE];
    	char pluginPath[PATH_BUFSIZE];
    	pthread_t thread1;
    	pthread_create( &thread1, NULL, &socket_start, NULL);
    	
        unsigned int error;
    	char* version;
    	error = ts3client_getClientLibVersion(&version);
    	if(error != ERROR_ok) {
    	    printf("Error querying clientlib version: %d\n", error);
    	    return;
    	}
    	printf("Client library version: %s\n", version);  /* Print version */
    	ts3client_freeMemory(version);  /* Release string */
    
        ts3Functions.getAppPath(appPath, PATH_BUFSIZE);
        ts3Functions.getResourcesPath(resourcesPath, PATH_BUFSIZE);
        ts3Functions.getConfigPath(configPath, PATH_BUFSIZE);
    	ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE, pluginID);
    
    	printf("PLUGIN: App path: %s\nResources path: %s\nConfig path: %s\nPlugin path: %s\n", appPath, resourcesPath, configPath, pluginPath);
    
        return 0;  /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
    	/* -2 is a very special case and should only be used if a plugin displays a dialog (e.g. overlay) asking the user to disable
    	 * the plugin again, avoiding the show another dialog by the client telling the user the plugin failed to load.
    	 * For normal case, if a plugin really failed to load because of an error, the correct return value is 1. */
    }
    
    void *socket_start(){
    	/*
    	Socket
    	*/
    	struct sockaddr_in server, client;
        struct sockaddr_in dest;
        int status,socket_fd, client_fd,num, read_size;
        socklen_t size;
    
        char client_message[2000];
        //  memset(buffer,0,sizeof(buffer));
        int yes =1;
        if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0))== -1) {
            fprintf(stderr, "Socket failure!!\n");
            exit(1);
        }
    
        if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
            perror("setsockopt");
            exit(1);
        }
        memset(&server, 0, sizeof(server));
        memset(&dest,0,sizeof(dest));
        server.sin_family = AF_INET;
        server.sin_port = htons(3490);
        server.sin_addr.s_addr = INADDR_ANY;
        if ((bind(socket_fd, (struct sockaddr *)&server, sizeof(struct sockaddr )))== -1)    { //sizeof(struct sockaddr)
            fprintf(stderr, "Binding Failure\n");
            return 1;
        } else {
                fprintf(stderr, "Binding succesfull\n");
        }
        
        //Listen
        listen(socket_fd , 3);
        
    	//Accept and incoming connection
        puts("Waiting for incoming connections...");
        int c = sizeof(struct sockaddr_in);
         
        //accept connection from an incoming client
        int client_sock = accept(socket_fd, (struct sockaddr *)&client, (socklen_t*)&c);
        if (client_sock < 0)
        {
            perror("accept failed");
            return 1;
        }
        puts("Connection accepted");
         
        //Receive a message from client
        while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
        {
            //Send the message back to client
            write(client_sock , client_message , strlen(client_message));
        }
         
        if(read_size == 0)
        {
            puts("Client disconnected");
            fflush(stdout);
        }
        else if(read_size == -1)
        {
            perror("recv failed");
        }
    }
    Last edited by dave1994; November 15th, 2017 at 05:57 PM.

  2. #2
    Join Date
    August 2013
    Location
    Germany
    Posts
    541
    Check if port 3490 is not in use.

  3. #3
    Join Date
    February 2012
    Location
    Germany
    Posts
    577
    You did not copy the example good enough. bind() takes a pointer to a buffer and the size of the buffer as parameters. As size (2nd parameter), you give sizeof(server) which is of type struct sockaddr_in but give as buffer (first parameter) a cast to pointer to struct sockaddr. These sizes mismatch. The example uses sizeof(struct sockaddr), which is of the correct size for the buffer.

  4. #4
    Join Date
    July 2016
    Posts
    7
    Thank you very much. Problem solved. More on the first post

    Quote Originally Posted by Bluscream View Post
    Check if port 3490 is not in use.
    Please read the first post carefully

    But i have new problem about this aplication, but i write in new post: http://forum.teamspeak.com/threads/1...377#post452377
    Last edited by dave1994; November 15th, 2017 at 06:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Help] Trying to compile and run example plugin, shows Failed to open plugin.
    By Ducky11423 in forum Client Plugins / Lua Scripts
    Replies: 4
    Last Post: April 9th, 2016, 06:47 PM
  2. ts3 overlay plugin (Failed to open plugin)
    By netmario in forum Windows
    Replies: 0
    Last Post: September 22nd, 2010, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •