Thread: socket programming

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    1

    Cool socket programming

    hi
    i 'm new to socket programming trying to write a server which sends message(by getting a string using fgets in a while loop ) to clients its sends the first messge but didnot send the next message also it shows bind error if i run it again from the same terminal
    client connects using telnet
    please help
    Code:
    #include<unistd.h>
    int main()
    {  int sock,new;
       struct sockaddr_in server,client;
       int len=sizeof(struct sockaddr);
       char msg[50]="first tcp sockets programme\n";
       if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
          { perror("socket:");
            exit(-1);
          }
       //now bind the server port
       server.sin_family=AF_INET;
       server.sin_port  =htons(9000);
       server.sin_addr.s_addr=INADDR_ANY;
       bzero(&server.sin_zero, 8);
       if ((bind(sock,(struct sockaddr *)&server,sizeof(struct sockaddr) ))==-1)
          {perror("bind error:");
           exit(-1);
          }
      listen(sock,2);
      // listen seldom  gines error
      while(1)
         {if((new=accept(sock,(struct sockaddr *)&client,&len))==-1)
         {perror("accept:");
          exit(-1);
         }
      //sending message     
         fgets(msg ,20,stdin);
    send(new,msg,strlen(msg),0);
         }
     close(new);//describe the client socket 
     close(sock);
    }
    35,6 63&#37;
    Last edited by Salem; 07-23-2008 at 10:52 AM. Reason: Added code tags

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Oh you forgot code? You may as well of left it out because no one is going to be able to help you unless you learn to stylize your code and use the forums code tags. The object of programming isn't to use the least amount of lines as possible so don't do this:
    Code:
    while(1)
    {if((new=accept(sock,(struct sockaddr *)&client,&len))==-1)
    {perror("accept:");
    exit(-1);
    }

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    #include<unistd.h>
    #include <WinSock2.h>
    int main()
    {  int sock,new;
       struct sockaddr_in server,client;
       int len=sizeof(struct sockaddr);
       char msg[]="first tcp sockets programme\n";
       if (sock=socket(AF_INET,SOCK_STREAM,0))
          { perror("socket:");
            exit(-1);
          }
       //now bind the server port
       server.sin_family=AF_INET;
       server.sin_port  =htons(9000);
       server.sin_addr.s_addr=INADDR_ANY;
       bzero(&server.sin_zero, 8);
       if (bind(sock,(struct sockaddr *)&server,sizeof(struct sockaddr) ))
          {perror("bind error:");
           exit(-1);
          }
      listen(sock,2);
      // listen seldom  gines error
      while(1)
         {if(new=accept(sock,(struct sockaddr *)&client,&len))
         {perror("accept:");
          exit(-1);
         }
      //sending message     
         fgets(msg ,20,stdin);
    send(new,msg,strlen(msg),0);
         }
     closesocket(new);//describe the client socket 
     closesocket(sock);
    }
    as for the formatting, pick one style and stick with it, it doesnt really matter too much how you format, but mixing styles makes your code look sloppy.

    Code:
       if(condition)
          { statement();
          }
    is very unusual, although syntactically correct.
    Last edited by abachler; 07-25-2008 at 08:12 AM.

  4. #4
    FOSS Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Quote Originally Posted by sunil View Post
    Code:
      while(1)
         {if((new=accept(sock,(struct sockaddr *)&client,&len))==-1)
         {perror("accept:");
          exit(-1);
         }
      //sending message     
         fgets(msg ,20,stdin);
    send(new,msg,strlen(msg),0);
         }
     close(new);//describe the client socket
    close(new); has to be in the while loop, otherwise new will be assigned a new socket, but the old one will not be closed, thus creating more and more sockets.
    this is also the problem why a client is just sent one message. what your code does is:
    1. assign new the socket of the first client
    2. get a message from stdin
    3. send that message via new
    4. assign new the next client

    Obviously, this can not work. You should either close new in the loop (this way, the clients need to reconnect after every sent message), or use threads/fork() to keep each client connection alive.

    The bind error is absolutely fine. It's because after closing a socket it still hangs around until the kernel clears it. While that socket is still around and you run the programm again.. you can't bind to that address because it's already bound. setsockopt() it the call you need to prevent this. Just set SO_REUSEADDR on that socket.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket programming question, closing sockets...
    By ursula in forum Networking/Device Communication
    Replies: 2
    Last Post: 05-31-2009, 05:17 PM
  2. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  3. when to close a socket
    By Wisefool in forum Networking/Device Communication
    Replies: 5
    Last Post: 11-02-2003, 10:33 AM
  4. problem closing socket
    By Wisefool in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-29-2003, 12:19 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM