#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0

    C socket programming i/o - Slump is having problems


    A simple example is the program have a socket. My goal is to communicate with Nginx. Read and reply to incoming requests.

    There are some problems, however.

    1) through a Web browser localhost: 9000 as the time the page is entered. Particularly Updates page is displayed again particularly refresh again. Imbalance.

    2) I did with Nginx proxy_pass requests from the port 127.0.0.1:900 transfers. However, the first time the "Hello World" script, but now is not coming.

    Socket.c

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "unistd.h"
    #include "errno.h"
    #include "string.h"
    #include "sys/socket.h"
    #include "netinet/in.h"
    #include "arpa/inet.h"
    #include "sys/types.h"
    
    int main(void)
    {
        int listenfd = 0;
        int connfd = 0;
    
        struct sockaddr_in serv_addr;
    
        char sendBuff[1025];
        int numrv;
    
        listenfd = socket(AF_INET,SOCK_STREAM,0);
    
        printf("socket retrieve success\n");
    
        memset(&serv_addr,'0',sizeof(serv_addr));
        memset(sendBuff,'0',sizeof(sendBuff));
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        serv_addr.sin_port = htons(9000);    
    
        bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
    
        if(listen(listenfd,10) == -1)
        {
            printf("Failed to listen\n");
            return -1;
        }
    
        while(1)
        {
            // accept awaiting request
            connfd = accept(listenfd,(struct sockaddr*)NULL,NULL);
    
            // send write data
            strcpy(sendBuff,"Message from server");
            write(connfd,sendBuff,strlen(sendBuff));
    
            close(connfd);    
            sleep(1);
        }
    
        return 0;
    Code:
    worker_processes  1;
    
    pid /usr/local/logs/nginx.pid;
    
    events
    {
        worker_connections 1024;
    }
    
    http
    {
        include mime.types;
        default_type application/octet-stream;
        sendfile on;
        keepalive_timeout 65;
        gzip on;
    
        error_page 500 502 503 504 /50x.html;
        error_page 404 /404.html;
    
        error_log  /usr/local/logs/nginx.error.log;
        access_log /usr/local/logs/access.log;
    
        upstream dev
        {
            server 127.0.0.1:9000;
        }
    
        server
        {
            listen 80;
            root /users/batuhangoksu/bookinget/wwwroot;
            index index.html index.htm;
            try_files $uri/index.html $uri.html $uri @dev;
            charset utf-8;
    
            location @dev
            {
                proxy_set_header  Request          $request;
                proxy_set_header  X-Real-IP        $remote_addr;
                proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header  Host             $http_host;
                proxy_set_header  Referer          $http_referer;
                proxy_set_header  Content-Type     $content_type;
                proxy_redirect    off;
                proxy_pass        http://dev;
            }
        }            
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Did bind work? I guess that is implied by listen having not failed, but you should test bind's return value for success.

    Did accept work? We don't know because you do not test its return value but rather blithely use it assuming that it succeeded.

    Even more important, do you ever even return from the first accept call? A simple printf after the accept would provide that information. Actually knowing is much better than guessing.

    I assume that the second listing is the script that you were talking about, but which has nothing at all to do with your program as presented.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    Nginx is exactly what I need to do to be able to communicate with.

    Is it simply can show on the example of the above code?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Code:
            connfd = accept(listenfd,(struct sockaddr*)NULL,NULL);
            if (connfd == -1)
                printf("accept failed: %s\n", strerror(errno));
            else    
            {
                printf("client connected.\n");  // why guess when you can actually know?
                // since connfd is good, use it
    What was so complicated about the idea of verifying that a client has connected?
    Nginx has absolutely nothing to do with it!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    Thank you dwise1_aol

    The web browser "localhost" on the terminal screen when I try to communicate by typing via Nginx

    Code:
    socket retrieve success
    client connected.
    Writes constantly loading screen appears, but the web browser

    "localhost: 9000" entered the terminal screen by typing

    4 time writing this message, but the web browser will display "Message from server" message shows.

    Code:
    client connected.
    client connected.
    client connected.
    client connected.
    Last code

    Code:
    #include "stdio.h"
    #include "stdlib.h"
    #include "unistd.h"
    #include "errno.h"
    #include "string.h"
    #include "sys/socket.h"
    #include "netinet/in.h"
    #include "arpa/inet.h"
    #include "sys/types.h"
    
    int main(void)
    {
    	int listenfd = 0;
    	int connfd = 0;
    
    	struct sockaddr_in serv_addr;
    
    	char sendBuff[1025];
    	int numrv;
    
    	listenfd = socket(AF_INET,SOCK_STREAM,0);
    
    	printf("socket retrieve success\n");
    
    	memset(&serv_addr,'0',sizeof(serv_addr));
    	memset(sendBuff,'0',sizeof(sendBuff));
    
    	serv_addr.sin_family = AF_INET;
    	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    	serv_addr.sin_port = htons(9000);    
    
    	bind(listenfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
    
    	if(listen(listenfd,10) == -1)
    	{
    		printf("Failed to listen\n");
    		return -1;
    	}
    
    	while(1)
    	{
    		// accept awaiting request
    		connfd = accept(listenfd,(struct sockaddr*)NULL,NULL);
    		
    		if(connfd == -1)
    		{
    			printf("accept failed: %s\n", strerror(errno));
    		}
    		else    
    		{
    			printf("client connected.\n");  
    			// why guess when you can actually know?
    			// since connfd is good, use it
    		}
    
    		// send write data
    		strcpy(sendBuff,"Message from server");
    		write(connfd,sendBuff,strlen(sendBuff));
    
    		close(connfd);    
    		sleep(1);
    	}
    
    	return 0;
    }
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Basic, basic programming principle: if you don't know that a socket is good, don't use it! Your while loop violated that most basic programming principle:
    Code:
    	while(1)
    	{
    		// accept awaiting request
    		connfd = accept(listenfd,(struct sockaddr*)NULL,NULL);
    		
    		if(connfd == -1)
    		{
    			printf("accept failed: %s\n", strerror(errno));
    		}
    		else    
    		{
    			printf("client connected.\n");  
    			// why guess when you can actually know?
    			// since connfd is good, use it
    		}
    
    		// send write data
    		strcpy(sendBuff,"Message from server");
    		write(connfd,sendBuff,strlen(sendBuff));
    
    		close(connfd);    
    		sleep(1);
    	}
    That should instead be:
    Code:
    	while(1)
    	{
    		// accept awaiting request
    		connfd = accept(listenfd,(struct sockaddr*)NULL,NULL);
    		
    		if(connfd == -1)
    		{
    			printf("accept failed: %s\n", strerror(errno));
    		}
    		else    
    		{
    			printf("client connected.\n");  
    			// why guess when you can actually know?
    			// since connfd is good, use it
    
    			// send write data
    			strcpy(sendBuff,"Message from server");
    			write(connfd,sendBuff,strlen(sendBuff));
    
    			close(connfd);    
    		}
    		sleep(1);
    	}
    Besides that, I do not understand what the nature of your problem is.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    Thank you,

    Let me tell you what I wanna do.

    An application will listen on port 9000. I will act according to requests from out of this port. My application will run in the background just to listen to port 9000. Nginx on port 9000 for incoming requests to transfer to the front. If my application will read these requests.

    By arrangement with the code that you've tried the last.
    localhost: 9000 writing when I opened the page immediately.
    8 seconds after the page is entered with Nginx. Home comes too late

    localhost: 9000 is coming right page would generate.
    localhost would generate more than 5-8 seconds or more time goes by.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    5
    Rep Power
    0
    One such example is more than I did, but I've failed, accept the connection. The screen does not print the message

    The other issue, although one user connection.

    6 user writes as if there is a connection message

    Code:
    I got a connection from (127.0.0.1) 
    I got a connection from (127.0.0.1) 
    I got a connection from (127.0.0.1) 
    I got a connection from (127.0.0.1) 
    I got a connection from (127.0.0.1) 
    I got a connection from (127.0.0.1)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    int sock, connected, bytes_recieved , true = 1;
    	
    char send_data[1024];
    char recv_data[1024];
    
    struct sockaddr_in server_addr,client_addr;
    int sin_size;
    
    int main(void)
    {
    	if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    	{
    		perror("Socket");
    		exit(1);
    	}
    
    	if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
    	{
    		perror("Setsockopt");
    		exit(1);
    	}
    
    	server_addr.sin_family = AF_INET;
    	server_addr.sin_port = htons(9000);
    	server_addr.sin_addr.s_addr = INADDR_ANY;
    	bzero(&(server_addr.sin_zero),10);
    
    	if(bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1) 
    	{
    		perror("Unable to bind");
    		exit(1);
    	}
    
    	if(listen(sock, 5) == -1)
    	{
    		perror("Listen");
    		exit(1);
    	}
    
    	fflush(stdout);
    
    	while(1)
    	{
    		sin_size = sizeof(struct send_data);
    		connected = accept(sock,(struct sockaddr *)&client_addr,&sin_size);
    
    		if(connected == -1)
    		{
    			printf("accept failed: %s \n", strerror(errno));
    		}
    		else
    		{
    			printf("I got a connection from (%s) \n",inet_ntoa(client_addr.sin_addr));
    
    			strcpy(send_data,"Merhaba DŁnya");
    			write(connected,send_data,strlen(send_data));
    			close(connected);    
    		}
    	}
    
    	close(sock);
    	return 0;
    }
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Your question is somewhat specialized. The best I could offer has been to change your code so that it would positively report a successful connection or an error while accepting a connection. Now that you know that the connection is succeeding, that means that your problem is somewhere else. I cannot help you further.

    I suggest that you change the title of the thread so that it includes the key word, Nginx. That way, it will be better able to draw the attention of somebody who is familiar with that proxy server. Or you could start a new thread with Nginx in its title.

    Also, since this is a specialized topic, there should be a forum or newsgroup that is dedicated to Nginx. Posting there would be more likely to receive an answer that would be useful for you. This forum deals more in general knowledge of C/C++/C#, even though we each bring our own specialized knowledge as well.

    I hope you find help on your problem.

IMN logo majestic logo threadwatch logo seochat tools logo