Thread: Help with server Socket Chat code
hey guys, writing code server recognize multiple clients. far getting error "segmentation fault" when run code. believe line of code:
i made modification original code taken off site. assume way wrote may conflict how code runs.code:for(p = ai; p != null; p = p->ai_next){
thank time
note: not homework/project assignment. want understanding of socket programming.
code:/* simple server in internet domain using tcp port number passed argument */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> void error(const char *msg) { perror(msg); exit(1); } int main(int argc, char *argv[]) { fd_set master; // master file descriptor list fd_set read_fds; // temp file descriptor list select() int fdmax; // maximum file descriptor number int sockfd, newsockfd, portno; socklen_t clilen; char buffer[256]; int yes=1; //for setsockopt struct sockaddr_in serv_addr, cli_addr; int i, j, rv; int nbytes; char remoteip[inet6_addrstrlen]; struct addrinfo hints, *ai, *p; fd_zero(&master); // clear master , temp sets fd_zero(&read_fds); if (argc < 2) { fprintf(stderr,"error, no port provided\n"); exit(1); } bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = af_inet; serv_addr.sin_addr.s_addr = inaddr_any; serv_addr.sin_port = htons(portno); for(p = ai; p != null; p = p->ai_next){ sockfd = socket(af_inet, sock_stream, 0); if (sockfd < 0){ continue; } //printf("the value sockfd %d \n", sockfd); //if (sockfd < 0) // error("error opening socket"); if (setsockopt(sockfd,sol_socket,so_reuseaddr, &yes,sizeof(int)) == -1) { error("setsockopt"); exit(1); } if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ close(sockfd); continue; } break; } if (listen(sockfd,5) == -1) { error("error on listen"); exit(3); } // add sockfd master set fd_set(sockfd, &master); // keep track of biggest file descriptor fdmax = sockfd; // far, it's 1 // main loop for(;;) { read_fds = master; // copy if (select(fdmax+1, &read_fds, null, null, null) == -1) { perror("select"); exit(4); } //printf("we @ before main loop"); for(i = 0; <= fdmax; i++) { if (fd_isset(i, &read_fds)) { // got one!! if (i == sockfd) { // handle new connections clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("error on accept"); else { fd_set(newsockfd, &master); // add master set if (newsockfd > fdmax) { // keep track of max fdmax = newsockfd; } printf("selectserver: new connection %s on " "socket %d\n", inet_ntop(af_inet, &(serv_addr.sin_addr), remoteip, inet6_addrstrlen), newsockfd); } } else { // handle data client if ((nbytes = recv(i, buffer, sizeof buffer, 0)) <= 0) { // got error or connection closed client if (nbytes == 0) { // connection closed printf("selectserver: socket %d hung up\n", i); } else { error("recv"); } close(i); // bye! fd_clr(i, &master); // remove master set } else { // got data client for(j = 0; j <= fdmax; j++) { // send everyone! if (fd_isset(j, &master)) { // except sockfd , ourselves if (j != sockfd && j != i) { if (send(j, buffer, nbytes, 0) == -1) { error("send"); } } } } } } // end handle data client } // end got new incoming connection } // end looping through file descriptors } // end for(;;)--and thought never end! return 0; }//end of main
i can see declare ai , p variable pointer struct addrinfo. can't find spot initialize them. uninitialized pointers point problems.
first make them point usefull use them.
cheers.
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk Help with server Socket Chat code
Ubuntu
Comments
Post a Comment