LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Segmentation fault in receiving http request from client code (http://www.linuxquestions.org/questions/linux-networking-3/segmentation-fault-in-receiving-http-request-from-client-code-863295/)

rajbir 02-17-2011 05:27 AM

Segmentation fault in receiving http request from client code
 
There is a segmentation fault error while receiving http request from client side program. Following is the code:
Code:

#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
using namespace std;
int main()
{     
        int sock, connected, bytes_recieved;       
        char recv_data[1024];       
        struct sockaddr_in server_addr,client_addr;           
        socklen_t sin_size;     
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)       
        {           
                cout<<"Socket Error Reason:"<<strerror(errno);           
                exit(1);       
        }       
        server_addr.sin_family = AF_INET;         
        server_addr.sin_port = htons(8081);       
        server_addr.sin_addr.s_addr = INADDR_ANY;     
        bzero(&(server_addr.sin_zero),8);   
        if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))== -1)       
        {           
                cout<<"Unable to bind: Reason="<<strerror(errno);         
                exit(1);     
        }   
        if (listen(sock, 5) == -1)
        {         
        cout<<"Listening error: Reason="<<strerror(errno);         
        exit(1);     
        }                       
        cout<<"\nTCPServer Waiting for client on port 8081";     
        cin.sync();       
        sin_size = sizeof(struct sockaddr_in);       
        connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);       
        cout<<"\n Server got a connection from "<<ntohs(client_addr.sin_port)<<endl;
        bytes_recieved = recv(connected,recv_data,1024,0);       
        recv_data[bytes_recieved] = '\0';       
        if((bytes_recieved==0)||(strcmp(recv_data,"\0")==0))
        {               
                close(connected);                       
        }       
        else       
                cout<<"\n RECIEVED DATA = "<<recv_data;             
        cin.sync();
        shutdown(sock,2);
        close(sock);
        return 0;
}

please help

David1357 02-18-2011 03:36 PM

Quote:

Originally Posted by rajbir (Post 4261373)
Code:

        bytes_recieved = recv(connected,recv_data,1024,0);       
        recv_data[bytes_recieved] = '\0';       
        if((bytes_recieved==0)||(strcmp(recv_data,"\0")==0))
        {               
                close(connected);                       
        }       
        else       
                cout<<"\n RECIEVED DATA = "<<recv_data;


You should verify that bytes_received is not -1 before trying to use it.


All times are GMT -5. The time now is 12:31 AM.