i new unix socket programming. haven't found myself comfortable book or tutorial , struggling.
here program code:
#include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include <netinet/in.h> int main(){ printf("one"); int sockethandle, newsockethandle, portno; struct sockaddr_in serveraddress, clientaddress; printf("two"); portno = 5001; bzero((char *) &serveraddress, sizeof(serveraddress)); serveraddress.sin_family = af_inet; serveraddress.sin_addr.s_addr = inaddr_any; serveraddress.sin_port = htons(portno); printf("three"); //creating socket sockethandle = socket(af_inet, sock_stream, 0); if(sockethandle < 0){ perror("error : socket not created."); return -1; } printf("socket created."); //binding socket if(bind(sockethandle, (struct sockaddr *) &serveraddress, sizeof(serveraddress)) < 0){ perror("error : socket not binded."); return -1; } printf("socket binded."); //make socket listen listen(sockethandle, 5); int len = sizeof(clientaddress); //accept connection requests newsockethandle = accept(sockethandle, (struct sockaddr *) &clientaddress, &len); if(newsockethandle < 0){ perror("error : connection not accepted."); } printf("connection accepted."); return 0; }
(i tried print one
, two
, , three
debugging)
but, printf("one")
in first line doesn't work. the cursor keeps blinking (indicating program still in execution). can't clue of going wrong in above program. using bzero()
function throws warning saying
warning: incompatible implicit declaration of built-in function ‘bzero’ [enabled default]
i find socket programming difficult different websites show different code. also, please suggest tutorial on c/c++ socket programming.
make sure print newline in debug messages them show immediately.
example printf("one\n");
if don't want newlines can instead flush output fflush(stdout);
.
Comments
Post a Comment