C socket program as web server to run in cloud, not receiving any response from browser -


i'm trying develop web server program using c++ suppose run on cloud machine running amazon machine. wrote code in c, won't receive response web browser when ip address , port number entered in address bar. however, receives response localhost. here's source code:

#include<stdio.h> #include<string.h>   #include<sys/socket.h> #include<arpa/inet.h>  #include<unistd.h>  int main(int argc , char *argv[]) { int socket_desc , new_socket , c; struct sockaddr_in server , client; char *message;  //create socket socket_desc = socket(af_inet , sock_stream , 0); if (socket_desc == -1) {     printf("could not create socket"); }   server.sin_family = af_inet; server.sin_addr.s_addr = inaddr_any; server.sin_port = htons( 8080 );  //bind if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0) {     puts("bind failed");     return 1; } puts("bind done");  listen(socket_desc , 3);  puts("waiting incoming connections..."); c = sizeof(struct sockaddr_in); while( (new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) ) {     puts("connection accepted");      //reply client     message = "hello client , have received connection. have go now, bye\n";     write(new_socket , message , strlen(message)); }  if (new_socket<0) {     perror("accept failed");     return 1; }  return 0; } 

is there logical problem in source code?

just tested code on centos 6.4 machine. worked... had "connection accepted"

"http://localhost:8080"  

and machine on network, entered

"http://192.168.2.22:8080"  

and got "connection accepted" on linux console. also, both ip address , "localhost" worked machine running program. i'm pretty sure it's network issue.

but try changing code bit heck of it...

try this:

change:

socket_desc = socket(af_inet , sock_stream , 0); 

to:

socket_desc = socket(af_inet , sock_stream , ipproto_tcp); 

add:

close(new_socket); 

after:

write(new_socket , message , strlen(message)); 

and once again, heck of it... add:

memset(&server, 0, sizeof(server)); 

before use server struct.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -